diff --git a/source/md.c b/source/md.c index 69676b0..74f3675 100644 --- a/source/md.c +++ b/source/md.c @@ -200,7 +200,7 @@ MD_WIN32_FileIterNext(MD_Arena *arena, MD_FileIter *it) } //- convert to MD_FileInfo - MD_FileInfo result = {0}; + MD_FileInfo result = MD_ZERO_STRUCT; if (good) { if (find_data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) @@ -210,7 +210,7 @@ MD_WIN32_FileIterNext(MD_Arena *arena, MD_FileIter *it) MD_u16 *filename_base = (MD_u16*)find_data->cFileName; MD_u16 *ptr = filename_base; for (;*ptr != 0; ptr += 1); - MD_String16 filename16 = {0}; + MD_String16 filename16 = MD_ZERO_STRUCT; filename16.str = filename_base; filename16.size = (MD_u64)(ptr - filename_base); result.filename = MD_S8FromS16(arena, filename16); @@ -302,69 +302,91 @@ MD_WIN32_Release(void *ptr, MD_u64 size) //- linux "file iteration" #if MD_DEFAULT_FILE_ITER && MD_OS_LINUX -#if !defined(MD_IMPL_FileIterIncrement) -# define MD_IMPL_FileIterIncrement MD_LINUX_FileIterIncrement +#if !defined(MD_IMPL_FileIterBegin) +# define MD_IMPL_FileIterBegin MD_LINUX_FileIterBegin +#endif +#if !defined(MD_IMPL_FileIterNext) +# define MD_IMPL_FileIterNext MD_LINUX_FileIterNext +#endif +#if !defined(MD_IMPL_FileIterEnd) +# define MD_IMPL_FileIterEnd MD_LINUX_FileIterEnd #endif -typedef struct MD_LINUX_FileIter MD_LINUX_FileIter; -struct MD_LINUX_FileIter -{ - int dir_fd; - DIR *dir; -}; -MD_StaticAssert(sizeof(MD_LINUX_FileIter) <= sizeof(MD_FileIter), file_iter_size_check); +typedef struct MD_LINUX_FileIter { + int dir_fd; + DIR *dir; + +} MD_LINUX_FileIter; + +MD_StaticAssert(sizeof(MD_FileIter) >= sizeof(MD_LINUX_FileIter), file_iter_size_check); static MD_b32 -MD_LINUX_FileIterIncrement(MD_Arena *arena, MD_FileIter *opaque_it, MD_String8 path, - MD_FileInfo *out_info) +MD_LINUX_FileIterBegin(MD_FileIter *it, MD_String8 path) { - MD_b32 result = 0; - - MD_LINUX_FileIter *it = (MD_LINUX_FileIter *)opaque_it; - if(it->dir == 0) - { - it->dir = opendir((char*)path.str); - it->dir_fd = open((char *)path.str, O_PATH|O_CLOEXEC); - } - - if(it->dir != 0 && it->dir_fd != -1) - { - struct dirent *dir_entry = readdir(it->dir); - if(dir_entry) - { - out_info->filename = MD_S8Fmt(arena, "%s", dir_entry->d_name); - out_info->flags = 0; - - struct stat st; - if(fstatat(it->dir_fd, dir_entry->d_name, &st, AT_NO_AUTOMOUNT|AT_SYMLINK_NOFOLLOW) == 0) - { - if((st.st_mode & S_IFMT) == S_IFDIR) - { - out_info->flags |= MD_FileFlag_Directory; - } - out_info->file_size = st.st_size; - } - result = 1; - } - } - - if(result == 0) + MD_ArenaTemp scratch = MD_GetScratch(0, 0); + + MD_LINUX_FileIter *linux_it = (MD_LINUX_FileIter*)it; + + MD_String8 cpath = MD_S8Copy(scratch.arena, path); + + linux_it->dir = opendir((char*)cpath.str); + linux_it->dir_fd = open((char*)cpath.str, O_PATH | O_CLOEXEC); + + MD_ReleaseScratch(scratch); + + MD_b32 result = 0; + if (linux_it->dir != 0 && linux_it->dir_fd != -1) + { + result = 1; + } + + return result; +} + +static MD_FileInfo +MD_LINUX_FileIterNext(MD_Arena *arena, MD_FileIter *it) +{ + MD_LINUX_FileIter *linux_it = (MD_LINUX_FileIter*)it; + + MD_FileInfo result = MD_ZERO_STRUCT; + + struct dirent *dir_entry = readdir(linux_it->dir); + if (dir_entry) + { + result.filename = MD_S8Fmt(arena, "%s", dir_entry->d_name); + + struct stat st; + if(fstatat(linux_it->dir_fd, dir_entry->d_name, &st, AT_NO_AUTOMOUNT|AT_SYMLINK_NOFOLLOW) == 0) { - if(it->dir != 0) - { - closedir(it->dir); - it->dir = 0; - } - if(it->dir_fd != -1) - { - close(it->dir_fd); - it->dir_fd = -1; - } + if((st.st_mode & S_IFMT) == S_IFDIR) + { + result.flags |= MD_FileFlag_Directory; + } + result.file_size = st.st_size; } - - return result; + } + + return result; } +static void +MD_LINUX_FileIterEnd(MD_FileIter *it) +{ + MD_LINUX_FileIter *linux_it = (MD_LINUX_FileIter*)it; + + if(linux_it->dir != 0) + { + closedir(linux_it->dir); + linux_it->dir = 0; + } + if(linux_it->dir_fd != -1) + { + close(linux_it->dir_fd); + linux_it->dir_fd = -1; + } +} + + #endif //- linux "low level memory" @@ -659,7 +681,7 @@ MD_ArenaDefaultAbsorb(MD_ArenaDefault *arena, MD_ArenaDefault *sub_arena) # define MD_IMPL_GetScratch MD_GetScratchDefault #endif -MD_THREAD_LOCAL MD_Arena *md_thread_scratch_pool[MD_IMPL_ScratchCount] = {0, 0}; +static MD_THREAD_LOCAL MD_Arena *md_thread_scratch_pool[MD_IMPL_ScratchCount] = {0, 0}; static MD_Arena* MD_GetScratchDefault(MD_Arena **conflicts, MD_u64 count) @@ -884,7 +906,7 @@ MD_CharToForwardSlash(MD_u8 c) //~ Strings MD_FUNCTION MD_u64 -MD_CalculateCStringLength(char *cstr) +MD_CalculateCStringLength(const char *cstr) { MD_u64 i = 0; for(; cstr[i]; i += 1); @@ -986,7 +1008,6 @@ MD_S8Match(MD_String8 a, MD_String8 b, MD_MatchFlags flags) MD_FUNCTION MD_u64 MD_S8FindSubstring(MD_String8 str, MD_String8 substring, MD_u64 start_pos, MD_MatchFlags flags) { - MD_b32 found = 0; MD_u64 found_idx = str.size; for(MD_u64 i = start_pos; i < str.size; i += 1) { @@ -996,7 +1017,6 @@ MD_S8FindSubstring(MD_String8 str, MD_String8 substring, MD_u64 start_pos, MD_Ma if(MD_S8Match(substr_from_str, substring, flags)) { found_idx = i; - found = 1; if(!(flags & MD_MatchFlag_FindLast)) { break; @@ -1019,7 +1039,7 @@ MD_S8Copy(MD_Arena *arena, MD_String8 string) } MD_FUNCTION MD_String8 -MD_S8FmtV(MD_Arena *arena, char *fmt, va_list args) +MD_S8FmtV(MD_Arena *arena, const char *fmt, va_list args) { MD_String8 result = MD_ZERO_STRUCT; va_list args2; @@ -1033,7 +1053,7 @@ MD_S8FmtV(MD_Arena *arena, char *fmt, va_list args) } MD_FUNCTION MD_String8 -MD_S8Fmt(MD_Arena *arena, char *fmt, ...) +MD_S8Fmt(MD_Arena *arena, const char *fmt, ...) { va_list args; va_start(args, fmt); @@ -1054,7 +1074,7 @@ MD_S8ListPush(MD_Arena *arena, MD_String8List *list, MD_String8 string) } MD_FUNCTION void -MD_S8ListPushFmt(MD_Arena *arena, MD_String8List *list, char *fmt, ...) +MD_S8ListPushFmt(MD_Arena *arena, MD_String8List *list, const char *fmt, ...) { va_list args; va_start(args, fmt); @@ -1362,7 +1382,7 @@ MD_DecodeCodepointFromUtf8(MD_u8 *str, MD_u64 max) { if (3 <= max) { - MD_u8 cont_byte[2] = {0}; + MD_u8 cont_byte[2] = MD_ZERO_STRUCT; cont_byte[0] = str[1]; cont_byte[1] = str[2]; if (md_utf8_class[cont_byte[0] >> 3] == 0 && @@ -1380,7 +1400,7 @@ MD_DecodeCodepointFromUtf8(MD_u8 *str, MD_u64 max) { if (4 <= max) { - MD_u8 cont_byte[3] = {0}; + MD_u8 cont_byte[3] = MD_ZERO_STRUCT; cont_byte[0] = str[1]; cont_byte[1] = str[2]; cont_byte[2] = str[3]; @@ -1513,7 +1533,7 @@ MD_S16FromS8(MD_Arena *arena, MD_String8 in) } str[size] = 0; MD_ArenaPutBack(arena, 2*(cap - size)); // := 2*((cap + 1) - (size + 1)) - MD_String16 result = {0}; + MD_String16 result = MD_ZERO_STRUCT; result.str = str; result.size = size; return(result); @@ -1554,7 +1574,7 @@ MD_S32FromS8(MD_Arena *arena, MD_String8 in) } str[size] = 0; MD_ArenaPutBack(arena, 4*(cap - size)); // := 4*((cap + 1) - (size + 1)) - MD_String32 result = {0}; + MD_String32 result = MD_ZERO_STRUCT; result.str = str; result.size = size; return(result); @@ -1860,7 +1880,7 @@ MD_FUNCTION MD_String8 MD_StringFromNodeKind(MD_NodeKind kind) { // NOTE(rjf): @maintenance Must be kept in sync with MD_NodeKind enum. - static char *cstrs[MD_NodeKind_COUNT] = + static const char *cstrs[MD_NodeKind_COUNT] = { "Nil", @@ -1880,7 +1900,7 @@ MD_FUNCTION MD_String8List MD_StringListFromNodeFlags(MD_Arena *arena, MD_NodeFlags flags) { // NOTE(rjf): @maintenance Must be kept in sync with MD_NodeFlags enum. - static char *flag_cstrs[] = + static const char *flag_cstrs[] = { "HasParenLeft", "HasParenRight", @@ -1946,7 +1966,7 @@ MD_HashPtr(void *p) MD_FUNCTION MD_Map MD_MapMakeBucketCount(MD_Arena *arena, MD_u64 bucket_count) { - MD_Map result = {0}; + MD_Map result = MD_ZERO_STRUCT; result.bucket_count = bucket_count; result.buckets = MD_PushArrayZero(arena, MD_MapBucket, bucket_count); return(result); @@ -1962,7 +1982,7 @@ MD_MapMake(MD_Arena *arena) MD_FUNCTION MD_MapKey MD_MapKeyStr(MD_String8 string) { - MD_MapKey result = {0}; + MD_MapKey result = MD_ZERO_STRUCT; if (string.size != 0) { result.hash = MD_HashStr(string); @@ -1978,7 +1998,7 @@ MD_MapKeyStr(MD_String8 string) MD_FUNCTION MD_MapKey MD_MapKeyPtr(void *ptr) { - MD_MapKey result = {0}; + MD_MapKey result = MD_ZERO_STRUCT; if (ptr != 0) { result.hash = MD_HashPtr(ptr); @@ -2796,7 +2816,7 @@ MD_ParseOneNode(MD_Arena *arena, MD_String8 string, MD_u64 offset) { case MD_TokenKind_BadCharacter: { - MD_String8List bytes = {0}; + MD_String8List bytes = MD_ZERO_STRUCT; for(int i_byte = 0; i_byte < bad_token.raw_string.size; ++i_byte) { MD_u8 b = bad_token.raw_string.str[i_byte]; @@ -3021,7 +3041,7 @@ MD_CodeLocFromNode(MD_Node *node) } } MD_Node *first_tag = file_root->first_tag; - MD_CodeLoc loc = {0}; + MD_CodeLoc loc = MD_ZERO_STRUCT; if(MD_NodeIsNil(first_tag)) { loc = MD_CodeLocFromFileOffset(file_root->string, file_root->raw_string.str, node->offset); @@ -3082,7 +3102,7 @@ MD_PushTag(MD_Node *node, MD_Node *tag) MD_FUNCTION MD_Node* MD_MakeList(MD_Arena *arena) { - MD_String8 empty = {0}; + MD_String8 empty = MD_ZERO_STRUCT; MD_Node *result = MD_MakeNode(arena, MD_NodeKind_List, empty, empty, 0); return(result); } @@ -3330,7 +3350,7 @@ MD_PrintMessage(FILE *file, MD_CodeLoc code_loc, MD_MessageKind kind, MD_String8 } MD_FUNCTION void -MD_PrintMessageFmt(FILE *file, MD_CodeLoc code_loc, MD_MessageKind kind, char *fmt, ...) +MD_PrintMessageFmt(FILE *file, MD_CodeLoc code_loc, MD_MessageKind kind, const char *fmt, ...) { MD_ArenaTemp scratch = MD_GetScratch(0, 0); va_list args; @@ -3581,7 +3601,7 @@ MD_ExprParse(MD_Arena *arena, MD_ExprOprTable *op_table, MD_Node *first, MD_Node MD_Expr *expr = MD_ExprParse_TopLevel(arena, &ctx, first, opl); // fill result - MD_ExprParseResult result = {0}; + MD_ExprParseResult result = MD_ZERO_STRUCT; result.expr = expr; result.errors = ctx.errors; return(result); @@ -4212,7 +4232,7 @@ MD_FUNCTION void MD_PrintDebugDumpFromNode(FILE *file, MD_Node *node, MD_GenerateFlags flags) { MD_ArenaTemp scratch = MD_GetScratch(0, 0); - MD_String8List list = {0}; + MD_String8List list = MD_ZERO_STRUCT; MD_DebugDumpFromNode(scratch.arena, &list, node, 0, MD_S8Lit(" "), flags); MD_String8 string = MD_S8ListJoin(scratch.arena, list, 0); @@ -4418,7 +4438,7 @@ MD_FUNCTION MD_FileInfo MD_FileIterNext(MD_Arena *arena, MD_FileIter *it) { #if !defined(MD_IMPL_FileIterNext) - MD_FileInfo result = {0}; + MD_FileInfo result = MD_ZERO_STRUCT; return(result); #else return(MD_IMPL_FileIterNext(arena, it)); diff --git a/source/md.h b/source/md.h index ff44627..da79928 100644 --- a/source/md.h +++ b/source/md.h @@ -330,7 +330,11 @@ //~ Basic Utilities #define MD_Assert(c) if (!(c)) { *(volatile MD_u64 *)0 = 0; } -#define MD_StaticAssert(c,label) MD_u8 MD_static_assert_##label[(c)?(1):(-1)] +#if MD_CPP_VERSION != 0 && MD_CPP_VERSION != 98 // anything C++11 and up +# define MD_StaticAssert(c,label) static_assert(c, #label) +#else +# define MD_StaticAssert(c,label) MD_u8 MD_static_assert_##label[(c)?(1):(-1)] +#endif #define MD_ArrayCount(a) (sizeof(a) / sizeof((a)[0])) #define MD_Min(a,b) (((a)<(b))?(a):(b)) @@ -963,7 +967,7 @@ MD_FUNCTION MD_u8 MD_CharToForwardSlash(MD_u8 c); //~ Strings -MD_FUNCTION MD_u64 MD_CalculateCStringLength(char *cstr); +MD_FUNCTION MD_u64 MD_CalculateCStringLength(const char *cstr); MD_FUNCTION MD_String8 MD_S8(MD_u8 *str, MD_u64 size); #define MD_S8CString(s) MD_S8((MD_u8 *)(s), MD_CalculateCStringLength(s)) @@ -997,16 +1001,16 @@ MD_FUNCTION MD_u64 MD_S8FindSubstring(MD_String8 str, MD_String8 substri MD_u64 start_pos, MD_MatchFlags flags); MD_FUNCTION MD_String8 MD_S8Copy(MD_Arena *arena, MD_String8 string); -MD_FUNCTION MD_String8 MD_S8FmtV(MD_Arena *arena, char *fmt, va_list args); +MD_FUNCTION MD_String8 MD_S8FmtV(MD_Arena *arena, const char *fmt, va_list args); -MD_FUNCTION MD_String8 MD_S8Fmt(MD_Arena *arena, char *fmt, ...); +MD_FUNCTION MD_String8 MD_S8Fmt(MD_Arena *arena, const char *fmt, ...); #define MD_S8VArg(s) (int)(s).size, (s).str MD_FUNCTION void MD_S8ListPush(MD_Arena *arena, MD_String8List *list, MD_String8 string); MD_FUNCTION void MD_S8ListPushFmt(MD_Arena *arena, MD_String8List *list, - char *fmt, ...); + const char *fmt, ...); MD_FUNCTION void MD_S8ListConcat(MD_String8List *list, MD_String8List *to_push); MD_FUNCTION MD_String8List MD_S8Split(MD_Arena *arena, MD_String8 string, int splitter_count, @@ -1166,7 +1170,7 @@ MD_FUNCTION MD_String8 MD_FormatMessage(MD_Arena *arena, MD_CodeLoc loc, MD_Mess MD_FUNCTION void MD_PrintMessage(FILE *file, MD_CodeLoc loc, MD_MessageKind kind, MD_String8 string); MD_FUNCTION void MD_PrintMessageFmt(FILE *file, MD_CodeLoc code_loc, MD_MessageKind kind, - char *fmt, ...); + const char *fmt, ...); #define MD_PrintGenNoteCComment(f) fprintf((f), "// generated by %s:%d\n", __FILE__, __LINE__) #endif