diff options
author | Joshua Haberman <joshua@reverberate.org> | 2009-08-24 21:44:22 -0700 |
---|---|---|
committer | Joshua Haberman <joshua@reverberate.org> | 2009-08-24 21:44:22 -0700 |
commit | 040f7e6ba2e2282b80f332a031b77d7d34b4fc85 (patch) | |
tree | 1d5e273fb9fcca51f6ce299b766ee0a97ee92863 /src/upb_string.c | |
parent | a223f9af30738cf00c313fabee8de75d04fb9a1a (diff) |
Significant memory-management refactoring any Python extension.
Diffstat (limited to 'src/upb_string.c')
-rw-r--r-- | src/upb_string.c | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/src/upb_string.c b/src/upb_string.c index 7754936..54df4f1 100644 --- a/src/upb_string.c +++ b/src/upb_string.c @@ -7,19 +7,20 @@ #include <stdio.h> #include "upb_string.h" -bool upb_strreadfile(const char *filename, struct upb_string *data) { +struct upb_string *upb_strreadfile(const char *filename) { FILE *f = fopen(filename, "rb"); if(!f) return false; - if(fseek(f, 0, SEEK_END) != 0) return false; + if(fseek(f, 0, SEEK_END) != 0) goto error; long size = ftell(f); - if(size < 0) return false; - if(fseek(f, 0, SEEK_SET) != 0) return false; - data->byte_len = size; - upb_stralloc(data, data->byte_len); - if(fread(data->ptr, size, 1, f) != 1) { - free(data->ptr); - return false; - } + if(size < 0) goto error; + if(fseek(f, 0, SEEK_SET) != 0) goto error; + struct upb_string *s = upb_string_new(); + upb_string_resize(s, size); + if(fread(s->ptr, size, 1, f) != 1) goto error; fclose(f); - return true; + return s; + +error: + fclose(f); + return NULL; } |