mkxp/binding-mruby/mrb-ext/file-helper.cpp

43 lines
1.1 KiB
C++

#include "file-helper.h"
#include <SDL_rwops.h>
char* SDL_rw_file_helper::read() {
SDL_RWops *rw = SDL_RWFromFile(filename, "rb");
if (rw == NULL) return NULL;
Sint64 res_size = SDL_RWsize(rw);
char* res = (char*)malloc(res_size + 1);
Sint64 nb_read_total = 0, nb_read = 1;
char* buf = res;
while (nb_read_total < res_size && nb_read != 0) {
nb_read = SDL_RWread(rw, buf, 1, (res_size - nb_read_total));
nb_read_total += nb_read;
buf += nb_read;
}
SDL_RWclose(rw);
if (nb_read_total != res_size) {
free(res);
return NULL;
}
res[nb_read_total] = '\0';
length = nb_read_total;
return res;
}
bool SDL_rw_file_helper::write(char * data) {
SDL_RWops *rw = SDL_RWFromFile("hello.txt", "w");
if(rw != NULL) {
size_t len = SDL_strlen(data);
if (SDL_RWwrite(rw, data, 1, len) != len) {
return false;
} else {
return true;
}
SDL_RWclose(rw);
}
return false;
}