#include "g_local.h" #include "fileio.h" static int ReadFromFile(FILE *fp, char *buffer){ int i, ch; for(i = 0; (ch = fgetc(fp)) != EOF; i++){ if(ch == '\r'){ ch = fgetc(fp); if(ch != EOF && ch != '\n') ungetc(ch, fp); ch = '\n'; } if(buffer) buffer[i] = ch; } if(buffer) buffer[i] = '\0'; return (i + 1); } // return is only good thru end of level. // using fgetc() since it auto-converts CRLF pairs char *ReadTextFile(char *filename){ FILE *f = NULL; char *filestring = NULL; long int i = 0; char *q2home, *home; gi.dprintf("Opening %s..\n", filename); q2home = getenv("QUAKE2_HOME"); // quetoo home = getenv("HOME"); if(q2home != NULL) //open in q2home/vanctf f = fopen(va("%s/vanctf/%s", q2home, filename), "r"); if(f == NULL && home != NULL) //or in ~/.quake2/vanctf f = fopen(va("%s/.quake2/vanctf/%s", home, filename), "r"); if(f == NULL) //or wherever we are f = fopen(filename, "r"); if(f == NULL) return NULL; i = ReadFromFile(f, NULL); filestring = gi.TagMalloc(i, TAG_LEVEL); if(!filestring){ fclose(f); return ""; } fseek(f, 0, SEEK_SET); ReadFromFile(f, filestring); gi.dprintf("Read %s\n", filename); fclose(f); return filestring; // return new text }