@@ -2679,18 +2679,68 @@ NOBDEF bool nob_rename(const char *old_path, const char *new_path)
26792679
26802680NOBDEF 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
27172768NOBDEF int nob_sb_appendf (Nob_String_Builder *sb, const char *fmt, ...)
0 commit comments