X-Git-Url: http://git.asbjorn.biz/?p=swftools.git;a=blobdiff_plain;f=lib%2Fos.c;h=fddaee904c0f6f79f482c4e9a0a234d1c27a87e3;hp=c76851345ca2d9602cbdd7b5d56513cfc77cc2e0;hb=2391d7ae5d8a145a250a8b80ab8c93ba74eba030;hpb=860eb8fcabf038906e2038a914a643ce35eb6a52 diff --git a/lib/os.c b/lib/os.c index c768513..fddaee9 100755 --- a/lib/os.c +++ b/lib/os.c @@ -26,14 +26,32 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #ifdef WIN32 #include +#else +#include +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#else +#undef HAVE_STAT +#endif +#ifdef HAVE_SYS_MMAN_H +#include +#else +#undef HAVE_MMAP +#endif +#ifdef HAVE_SYS_TYPES_H +#include +#else +#undef HAVE_STAT #endif #if defined(CYGWIN) -static char seperator = '/'; +char path_seperator = '/'; #elif defined(WIN32) -static char seperator = '\\'; +char path_seperator = '\\'; #else -static char seperator = '/'; +char path_seperator = '/'; #endif #ifdef WIN32 @@ -128,14 +146,14 @@ char* concatPaths(const char*base, const char*add) int l2 = strlen(add); int pos = 0; char*n = 0; - while(l1 && base[l1-1] == seperator) + while(l1 && base[l1-1] == path_seperator) l1--; - while(pos < l2 && add[pos] == seperator) + while(pos < l2 && add[pos] == path_seperator) pos++; n = (char*)malloc(l1 + (l2-pos) + 2); memcpy(n,base,l1); - n[l1]=seperator; + n[l1]=path_seperator; strcpy(&n[l1+1],&add[pos]); return n; } @@ -192,7 +210,7 @@ char* mktempname(char*ptr) { // used to be mktemp. This does remove the warnings, but // It's not exactly an improvement. #ifdef HAVE_LRAND48 - sprintf(ptr, "%s%s%08x%08x",dir,sep,lrand48(),lrand48()); + sprintf(ptr, "%s%s%08x%08x",dir,sep,(unsigned int)lrand48(),(unsigned int)lrand48()); #else # ifdef HAVE_RAND sprintf(ptr, "%s%s%08x%08x",dir,sep,rand(),rand()); @@ -205,3 +223,54 @@ char* mktempname(char*ptr) { return ptr; } +memfile_t* memfile_open(const char*path) +{ + memfile_t*file = malloc(sizeof(memfile_t)); +#if defined(HAVE_MMAP) && defined(HAVE_STAT) + int fi = open(path, O_RDONLY); + if(fi<0) { + perror(path); + free(file); + return 0; + } + struct stat sb; + if(fstat(fi, &sb)<0) { + perror(path); + return 0; + } + file->len = sb.st_size; + file->data = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, fi, 0); +#else + FILE*fi = fopen(path, "rb"); + if(!fi) { + perror(path); + free(file); + return 0; + } + fseek(fi, 0, SEEK_END); + file->len = ftell(fi); + fseek(fi, 0, SEEK_SET); + file->data = malloc(file->len); + if(!file->data) { + fprintf(stderr, "Out of memory while allocating memory for file %s\n", path); + free(file); + return 0; + } + fread(file->data, file->len, 1, fi); + fclose(fi); +#endif + return file; +} + +void memfile_close(memfile_t*file) +{ +#if defined(HAVE_MMAP) && defined(HAVE_STAT) + munmap(file->data, file->len); +#else + free(file->data); +#endif + file->data = 0; + file->len = 0; + free(file); +} +