comparison dowa/d_memory.c @ 64:a30944e5719e

Added vibe coded markdown to html script since it is useful for me. Updated Dowa so that it can be compiled without dirnet for windows.
author June Park <parkjune1995@gmail.com>
date Tue, 23 Dec 2025 15:18:46 -0800
parents 636eab07809d
children ecb6ee6a22c3
comparison
equal deleted inserted replaced
63:fff1b048dda6 64:a30944e5719e
472 } 472 }
473 } 473 }
474 printf("-----------\n"); 474 printf("-----------\n");
475 } 475 }
476 476
477 #ifdef DIRECTORY
477 int Dowa_HashMap_Cache_Folder(Dowa_PHashMap p_hash_map, const char *folder_path) 478 int Dowa_HashMap_Cache_Folder(Dowa_PHashMap p_hash_map, const char *folder_path)
478 { 479 {
479 if (!p_hash_map || !folder_path) 480 if (!p_hash_map || !folder_path)
480 return -1; 481 return -1;
481 482
501 { 502 {
502 size_t size = (size_t)st.st_size; 503 size_t size = (size_t)st.st_size;
503 FILE *f = fopen(fullpath, "rb"); 504 FILE *f = fopen(fullpath, "rb");
504 if (!f) { perror("fopen"); continue; } 505 if (!f) { perror("fopen"); continue; }
505 506
507 // Allocate exact file size (no extra byte for null terminator)
506 void *buf = p_hash_map->p_arena ? 508 void *buf = p_hash_map->p_arena ?
507 Dowa_Arena_Allocate(p_hash_map->p_arena, size + 1) : 509 Dowa_Arena_Allocate(p_hash_map->p_arena, size) :
508 malloc(size + 1); 510 malloc(size);
509 if (!buf) { perror("malloc"); fclose(f); closedir(dir); return -1; } 511 if (!buf) { perror("malloc"); fclose(f); closedir(dir); return -1; }
510
511 512
512 if (fread(buf, 1, size, f) != size) 513 if (fread(buf, 1, size, f) != size)
513 { 514 {
514 perror("fread"); 515 perror("fread");
515 if (!p_hash_map->p_arena) Dowa_Free(buf); 516 if (!p_hash_map->p_arena) Dowa_Free(buf);
516 fclose(f); 517 fclose(f);
517 continue; 518 continue;
518 } 519 }
519 fclose(f); 520 fclose(f);
520 521
521 ((char *)buf)[size] = '\0'; 522 // Store file data as-is without null terminator (for binary files like images)
522 Dowa_HashMap_Push_Value_With_Type(p_hash_map, entry->d_name, buf, size + 1, DOWA_HASH_MAP_TYPE_STRING); 523 Dowa_HashMap_Push_Value_With_Type(p_hash_map, entry->d_name, buf, size, DOWA_HASH_MAP_TYPE_STRING);
523 if (!p_hash_map->p_arena) 524 if (!p_hash_map->p_arena)
524 Dowa_Free(buf); // Dowa_HashMap_PushValue made its own copy 525 Dowa_Free(buf); // Dowa_HashMap_PushValue made its own copy
525 } 526 }
526 else if (S_ISDIR(st.st_mode)) 527 else if (S_ISDIR(st.st_mode))
527 { 528 {
548 } 549 }
549 } 550 }
550 closedir(dir); 551 closedir(dir);
551 return 0; 552 return 0;
552 } 553 }
554 #endif