aboutsummaryrefslogtreecommitdiff
path: root/src/tokenizer.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2017-10-18 22:52:42 +0100
committergingerBill <bill@gingerbill.org>2017-10-18 22:52:42 +0100
commit0ed34af19d20aa5ae13c2147bd0f767d68d2e965 (patch)
treee9a08b9c17280f9b072a1ba134df3f23e2c32a09 /src/tokenizer.cpp
parent71729c2855f3a13f6809e1bed92c31ca87623140 (diff)
Fix importation of empty file (issue #128)
Diffstat (limited to 'src/tokenizer.cpp')
-rw-r--r--src/tokenizer.cpp12
1 files changed, 7 insertions, 5 deletions
diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp
index 1f62206b6..7d36e2a66 100644
--- a/src/tokenizer.cpp
+++ b/src/tokenizer.cpp
@@ -433,18 +433,22 @@ TokenizerInitError init_tokenizer(Tokenizer *t, String fullpath) {
TokenizerInitError err = TokenizerInit_None;
char *c_str = gb_alloc_array(heap_allocator(), char, fullpath.len+1);
+ defer (gb_free(heap_allocator(), c_str));
+
gb_memcopy(c_str, fullpath.text, fullpath.len);
c_str[fullpath.len] = '\0';
// TODO(bill): Memory map rather than copy contents
gbFileContents fc = gb_file_read_contents(heap_allocator(), true, c_str);
gb_zero_item(t);
+
+ t->fullpath = fullpath;
+ t->line_count = 1;
+
if (fc.data != nullptr) {
t->start = cast(u8 *)fc.data;
t->line = t->read_curr = t->curr = t->start;
t->end = t->start + fc.size;
- t->fullpath = fullpath;
- t->line_count = 1;
advance_to_next_rune(t);
if (t->curr_rune == GB_RUNE_BOM) {
@@ -455,6 +459,7 @@ TokenizerInitError init_tokenizer(Tokenizer *t, String fullpath) {
} else {
gbFile f = {};
gbFileError file_err = gb_file_open(&f, c_str);
+ defer (gb_file_close(&f));
switch (file_err) {
case gbFileError_Invalid: err = TokenizerInit_Invalid; break;
@@ -465,11 +470,8 @@ TokenizerInitError init_tokenizer(Tokenizer *t, String fullpath) {
if (err == TokenizerInit_None && gb_file_size(&f) == 0) {
err = TokenizerInit_Empty;
}
-
- gb_file_close(&f);
}
- gb_free(heap_allocator(), c_str);
return err;
}