From 2a319ed6db78abec5f16a3b069c5dcefca9f00fa Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 16 Jul 2023 23:18:00 -0400 Subject: [PATCH] Additions and fixes based off genc repo Typedef parses enum namespaced types properly (C typedefs of enums to expose to global scope). --- Readme.md | 1 + project/gen.cpp | 1 + project/gen_dep.cpp | 13 ++++++++ project/gen_dep.hpp | 73 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+) diff --git a/Readme.md b/Readme.md index 710e439..49d46e0 100644 --- a/Readme.md +++ b/Readme.md @@ -687,3 +687,4 @@ Names or Content fields are interned strings and thus showed be cached using `ge * Suffix specifiers for functions (const, override, final) * Implement the Scanner * Implement the Editor +* Support parsing full enum definitions inside a typedef. (For C patterns) diff --git a/project/gen.cpp b/project/gen.cpp index 3ac8b88..89853d1 100644 --- a/project/gen.cpp +++ b/project/gen.cpp @@ -5754,6 +5754,7 @@ namespace gen } if ( currtok.Type == TokType::Decl_Class + || currtok.Type == TokType::Decl_Enum || currtok.Type == TokType::Decl_Struct ) { name = currtok; diff --git a/project/gen_dep.cpp b/project/gen_dep.cpp index e8a4c26..e3b0d93 100644 --- a/project/gen_dep.cpp +++ b/project/gen_dep.cpp @@ -1626,6 +1626,19 @@ namespace gen return err; } + + s64 file_size( FileInfo* f ) + { + s64 size = 0; + s64 prev_offset = file_tell( f ); + + file_seek_to_end( f ); + size = file_tell( f ); + + file_seek( f, prev_offset ); + + return size; + } #pragma endregion File Handling #pragma region String diff --git a/project/gen_dep.hpp b/project/gen_dep.hpp index d211463..b89df04 100644 --- a/project/gen_dep.hpp +++ b/project/gen_dep.hpp @@ -2015,6 +2015,41 @@ namespace gen */ FileError file_open_mode( FileInfo* file, FileMode mode, char const* filename ); + /** + * Reads from a file + * @param file + * @param buffer Buffer to read to + * @param size Size to read + */ + GEN_DEF_INLINE b32 file_read( FileInfo* file, void* buffer, sw size ); + + /** + * Reads file at a specific offset + * @param file + * @param buffer Buffer to read to + * @param size Size to read + * @param offset Offset to read from + * @param bytes_read How much data we've actually read + */ + GEN_DEF_INLINE b32 file_read_at( FileInfo* file, void* buffer, sw size, s64 offset ); + + /** + * Reads file safely + * @param file + * @param buffer Buffer to read to + * @param size Size to read + * @param offset Offset to read from + * @param bytes_read How much data we've actually read + */ + GEN_DEF_INLINE b32 file_read_at_check( FileInfo* file, void* buffer, sw size, s64 offset, sw* bytes_read ); + + /** + * Returns a size of the file + * @param file + * @return File size + */ + s64 file_size( FileInfo* file ); + /** * Seeks the file cursor from the beginning of file to a specific position * @param file @@ -2022,6 +2057,12 @@ namespace gen */ GEN_DEF_INLINE s64 file_seek( FileInfo* file, s64 offset ); + /** + * Seeks the file cursor to the end of the file + * @param file + */ + GEN_DEF_INLINE s64 file_seek_to_end( FileInfo* file ); + /** * Returns the length from the beginning of the file we've read so far * @param file @@ -2069,6 +2110,18 @@ namespace gen return new_offset; } + GEN_IMPL_INLINE s64 file_seek_to_end( FileInfo* f ) + { + s64 new_offset = 0; + + if ( ! f->Ops.read_at ) + f->Ops = default_file_operations; + + f->Ops.seek( f->FD, 0, ESeekWhence_END, &new_offset ); + + return new_offset; + } + GEN_IMPL_INLINE s64 file_tell( FileInfo* f ) { s64 new_offset = 0; @@ -2081,6 +2134,26 @@ namespace gen return new_offset; } + GEN_IMPL_INLINE b32 file_read( FileInfo* f, void* buffer, sw size ) + { + s64 cur_offset = file_tell( f ); + b32 result = file_read_at( f, buffer, size, file_tell( f ) ); + file_seek( f, cur_offset + size ); + return result; + } + + GEN_IMPL_INLINE b32 file_read_at( FileInfo* f, void* buffer, sw size, s64 offset ) + { + return file_read_at_check( f, buffer, size, offset, NULL ); + } + + GEN_IMPL_INLINE b32 file_read_at_check( FileInfo* f, void* buffer, sw size, s64 offset, sw* bytes_read ) + { + if ( ! f->Ops.read_at ) + f->Ops = default_file_operations; + return f->Ops.read_at( f->FD, buffer, size, offset, bytes_read, false ); + } + GEN_IMPL_INLINE b32 file_write( FileInfo* f, void const* buffer, sw size ) { s64 cur_offset = file_tell( f );