Skip to content

Commit e2416ed

Browse files
committed
nob_read_entire_file
1 parent 66aca5d commit e2416ed

2 files changed

Lines changed: 65 additions & 4 deletions

File tree

nob.h

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2679,18 +2679,68 @@ NOBDEF bool nob_rename(const char *old_path, const char *new_path)
26792679

26802680
NOBDEF bool nob_read_entire_file(const char *path, Nob_String_Builder *sb)
26812681
{
2682+
#ifdef _WIN32
2683+
bool result;
2684+
HANDLE file;
2685+
DWORD chunk_size;
2686+
size_t mark;
2687+
wchar_t *wide_path;
2688+
DWORD err;
2689+
size_t new_count;
2690+
BOOL b;
2691+
DWORD read;
2692+
2693+
result = true;
2694+
file = INVALID_HANDLE_VALUE;
2695+
chunk_size = 64 * 1024;
2696+
mark = nob_temp_save();
2697+
wide_path = nob__unicode_utf8_to_unicode_utf16_temp(path);
2698+
file = CreateFileW(wide_path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2699+
err = GetLastError();
2700+
nob_temp_rewind(mark);
2701+
if (file == INVALID_HANDLE_VALUE)
2702+
{
2703+
nob_log(NOB_ERROR, "Could not open file %s for reading: %s\n", path, nob_win32_error_message(err));
2704+
nob_return_defer(false);
2705+
}
2706+
for (;;)
2707+
{
2708+
new_count = sb->count + chunk_size;
2709+
if (new_count > sb->capacity)
2710+
{
2711+
sb->items = NOB_DECLTYPE_CAST(sb->items)NOB_REALLOC(sb->items, 2 * new_count);
2712+
NOB_ASSERT(sb->items != NULL && "Buy more RAM lool!!");
2713+
sb->capacity = new_count;
2714+
}
2715+
b = ReadFile(file, sb->items + sb->count, chunk_size, &read, NULL);
2716+
if (b == FALSE)
2717+
{
2718+
err = GetLastError();
2719+
nob_log(NOB_ERROR, "Could not read from file %s: %s\n", path, nob_win32_error_message(err));
2720+
nob_return_defer(false);
2721+
}
2722+
sb->count += read;
2723+
if (read != chunk_size)
2724+
{
2725+
break;
2726+
}
2727+
}
2728+
defer:
2729+
if (file != INVALID_HANDLE_VALUE)
2730+
{
2731+
b = CloseHandle(file);
2732+
NOB_ASSERT(b != FALSE);
2733+
}
2734+
return result;
2735+
#else
26822736
bool result = true;
26832737

26842738
FILE *f = fopen(path, "rb");
26852739
size_t new_count = 0;
26862740
long long m = 0;
26872741
if (f == NULL) nob_return_defer(false);
26882742
if (fseek(f, 0, SEEK_END) < 0) nob_return_defer(false);
2689-
#ifndef _WIN32
26902743
m = ftell(f);
2691-
#else
2692-
m = _telli64(_fileno(f));
2693-
#endif
26942744
if (m < 0) nob_return_defer(false);
26952745
if (fseek(f, 0, SEEK_SET) < 0) nob_return_defer(false);
26962746

@@ -2712,6 +2762,7 @@ NOBDEF bool nob_read_entire_file(const char *path, Nob_String_Builder *sb)
27122762
if (!result) nob_log(NOB_ERROR, "Could not read file %s: %s", path, strerror(errno));
27132763
if (f) fclose(f);
27142764
return result;
2765+
#endif // _WIN32
27152766
}
27162767

27172768
NOBDEF int nob_sb_appendf(Nob_String_Builder *sb, const char *fmt, ...)

tests/unicode.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ static void test_unicode_utf8_file_operations(void)
6868
Nob_Fd fd;
6969
Nob_File_Type ft;
7070
bool b;
71+
Nob_String_Builder sb;
7172

7273
nob_log(NOB_INFO, "%s", "Testing file operations...");
7374
n = NOB_ARRAY_LEN(k_strings);
@@ -92,6 +93,15 @@ static void test_unicode_utf8_file_operations(void)
9293
b = nob_write_entire_file(k_strings[i], "test", 4);
9394
test(b);
9495

96+
sb.items = NULL;
97+
sb.capacity = 0;
98+
sb.count = 0;
99+
b = nob_read_entire_file(k_strings[i], &sb);
100+
test(b);
101+
test(sb.count == 4);
102+
test(memcmp(sb.items, "test", 4) == 0);
103+
NOB_FREE(sb.items);
104+
95105
b = nob_delete_file(k_strings[i]);
96106
test(b);
97107
}

0 commit comments

Comments
 (0)