ali@0: #include ali@0: #include ali@5: #include ali@5: #include ali@5: #include ali@5: #include ali@0: ali@0: /* ali@0: * Read a file into memory (which should be freed with mem_free when no ali@0: * longer required). Returns FALSE on error and outputs a suitable error ali@0: * message to stderr. ali@0: */ ali@0: boolean file_get_contents(const char *filename,char **contents,size_t *length) ali@0: { ali@0: FILE *fp; ali@0: size_t n; ali@0: char *buffer; ali@0: String *string; ali@0: fp=fopen(filename,"rb"); ali@0: if (!fp) ali@0: { ali@0: perror(filename); ali@0: return FALSE; ali@0: } ali@0: buffer=mem_new(char,1024); ali@0: string=string_new(NULL); ali@0: do ali@0: { ali@0: n=fread(buffer,1,1024,fp); ali@0: if (n<0) ali@0: { ali@0: perror(filename); ali@0: string_free(string,TRUE); ali@0: mem_free(buffer); ali@0: free(fp); ali@0: return FALSE; ali@0: } ali@0: string_append_len(string,buffer,n); ali@0: } while(n); ali@0: mem_free(buffer); ali@0: if (length) ali@0: *length=string->len; ali@0: *contents=string_free(string,FALSE); ali@0: fclose(fp); ali@0: return TRUE; ali@0: }