00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "textfile.h"
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <limits.h>
00028 #include "compat.h"
00029
00030 char *textFileRead(const char *fn)
00031 {
00032 FILE *fp;
00033 char *content = NULL;
00034
00035 int count=0;
00036
00037 if (fn != NULL)
00038 {
00039 fp = fopen(fn,"rt");
00040
00041 if (fp != NULL)
00042 {
00043 fseek(fp, 0, SEEK_END);
00044 count = ftell(fp);
00045 rewind(fp);
00046 if (count > 0)
00047 {
00048 content = (char *) malloc(count + 1);
00049 count = fread(content, 1, count, fp);
00050 content[count] = '\0';
00051 }
00052 fclose(fp);
00053 }
00054 }
00055 return content;
00056 }
00057
00058 int textFileWrite(const char *fn, char *s)
00059 {
00060 FILE *fp;
00061 int status = 0;
00062
00063 if (fn != NULL)
00064 {
00065 fp = fopen(fn,"w");
00066 if (fp != NULL)
00067 {
00068 if (fwrite(s,sizeof(char),strlen(s),fp) == strlen(s))
00069 status = 1;
00070 fclose(fp);
00071 }
00072 }
00073 return(status);
00074 }
00075
00076
00077 char *dataFileRead(const char *fn)
00078 {
00079 char *ret;
00080 int max_path_len=0;
00081 const char *paths[] = {
00082 ".",
00083 "..",
00084 DATADIR,
00085 "/usr/share/lightspark",
00086 };
00087 for(uint32_t i=0;i<sizeof(paths)/sizeof(const char*);i++)
00088 max_path_len=imax(strlen(paths[i]),max_path_len);
00089 max_path_len+=strlen(fn)+2;
00090 char* buf=new char[max_path_len];
00091
00092 for (unsigned int i = 0; i < sizeof(paths)/sizeof(paths[0]); i++)
00093 {
00094 snprintf(buf, max_path_len, "%s/%s", paths[i], fn);
00095 ret = textFileRead(buf);
00096 if (ret)
00097 {
00098 delete[] buf;
00099 return ret;
00100 }
00101 }
00102
00103 delete[] buf;
00104 return ret;
00105 }
00106
00107