summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Kolesa <d.kolesa@osg.samsung.com>2015-09-29 14:11:52 +0100
committerDaniel Kolesa <d.kolesa@osg.samsung.com>2015-09-29 14:24:44 +0100
commit1853051618c97d0d187d398ea25a0dcf09a5adac (patch)
treef975290b7cda71ac8c1296b64a9bcf6dc3bb8096
parent97cb6c5ca9466fde47c7519134f7e25275137be9 (diff)
eolian generator: refactoring of file reads
Diffstat (limited to '')
-rw-r--r--src/bin/eolian/main.c95
1 files changed, 46 insertions, 49 deletions
diff --git a/src/bin/eolian/main.c b/src/bin/eolian/main.c
index ebb845cc54..76de4681ca 100644
--- a/src/bin/eolian/main.c
+++ b/src/bin/eolian/main.c
@@ -48,48 +48,47 @@ _filename_get(const char *path)
48} 48}
49 49
50static Eina_Bool 50static Eina_Bool
51_read_file(char *filename, Eina_Strbuf *buffer) 51_read_file(char *filename, Eina_Strbuf **buf)
52{ 52{
53 Eina_Bool ret = EINA_FALSE;
54 long file_size = 0;
55 eina_strbuf_reset(buffer);
56
57 FILE *fd = fopen(filename, "rb"); 53 FILE *fd = fopen(filename, "rb");
58 if (fd) 54 if (!fd)
59 { 55 {
60 fseek(fd, 0, SEEK_END); 56 *buf = eina_strbuf_new();
61 file_size = ftell(fd); 57 return EINA_TRUE;
62 if (file_size <= 0) 58 }
63 { 59
64 fprintf(stderr, "eolian: could not get length of '%s'\n", filename); 60 fseek(fd, 0, SEEK_END);
65 goto end; 61 long file_size = ftell(fd);
66 } 62 if (file_size <= 0)
67 fseek(fd, 0, SEEK_SET); 63 {
68 char *content = malloc(file_size + 1); 64 fprintf(stderr, "eolian: could not get length of '%s'\n", filename);
69 if (!content) 65 fclose(fd);
70 { 66 return EINA_FALSE;
71 fprintf(stderr, "eolian: could not allocate memory for '%s'\n", filename); 67 }
72 goto end; 68 fseek(fd, 0, SEEK_SET);
73 }
74 long actual_size = (long)fread(content, 1, file_size, fd);
75 if (actual_size != file_size)
76 {
77 fprintf(stderr, "eolian: could not read %ld bytes from '%s' (read %ld bytes)\n",
78 file_size, filename, actual_size);
79 free(content);
80 goto end;
81 }
82 69
83 content[file_size] = '\0'; 70 char *content = malloc(file_size + 1);
71 if (!content)
72 {
73 fprintf(stderr, "eolian: could not allocate memory for '%s'\n", filename);
74 fclose(fd);
75 return EINA_FALSE;
76 }
84 77
85 eina_strbuf_append(buffer, content); 78 long actual_size = (long)fread(content, 1, file_size, fd);
79 if (actual_size != file_size)
80 {
81 fprintf(stderr, "eolian: could not read %ld bytes from '%s' (read %ld bytes)\n",
82 file_size, filename, actual_size);
86 free(content); 83 free(content);
84 fclose(fd);
85 return EINA_FALSE;
87 } 86 }
88 87
89 ret = EINA_TRUE; 88 content[file_size] = '\0';
90end: 89 fclose(fd);
91 if (fd) fclose(fd); 90 *buf = eina_strbuf_manage_new_length(content, file_size);
92 return ret; 91 return EINA_TRUE;
93} 92}
94 93
95static Eina_Bool 94static Eina_Bool
@@ -225,25 +224,23 @@ end:
225static Eina_Bool 224static Eina_Bool
226_generate_impl_c_file(char *filename, const char *eo_filename) 225_generate_impl_c_file(char *filename, const char *eo_filename)
227{ 226{
228 Eina_Bool ret = EINA_FALSE;
229 Eina_Strbuf *buffer = eina_strbuf_new();
230
231 const Eolian_Class *class = eolian_class_get_by_file(eo_filename); 227 const Eolian_Class *class = eolian_class_get_by_file(eo_filename);
232 if (class) 228 if (!class)
233 { 229 return EINA_FALSE;
234 if (!_read_file(filename, buffer)) goto end;
235 230
236 if (!impl_source_generate(class, buffer)) 231 Eina_Strbuf *buffer = NULL;
237 { 232 if (!_read_file(filename, &buffer))
238 fprintf(stderr, "eolian: could not generate source for '%s'\n", 233 return EINA_FALSE;
239 eolian_class_name_get(class));
240 goto end;
241 }
242 234
243 if (_write_file(filename, buffer, EINA_FALSE)) 235 if (!impl_source_generate(class, buffer))
244 ret = EINA_TRUE; 236 {
237 fprintf(stderr, "eolian: could not generate source for '%s'\n",
238 eolian_class_name_get(class));
239 eina_strbuf_free(buffer);
240 return EINA_FALSE;
245 } 241 }
246end: 242
243 Eina_Bool ret = _write_file(filename, buffer, EINA_FALSE);
247 eina_strbuf_free(buffer); 244 eina_strbuf_free(buffer);
248 return ret; 245 return ret;
249} 246}