~groovestomp/c-parser

ref: 78f3a012072eccac987a5d7f1b217efc9d730fc3 c-parser/src/gs.h -rw-r--r-- 22.4 KiB
78f3a012 — Aaron Oman Normalize coding style for gs.h 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/******************************************************************************
 * File: gs.h
 * Created: 2016-07-14
 * Updated: 2016-11-03
 * Package: gslibc
 * Creator: Aaron Oman (GrooveStomp)
 * Copyright 2016 - 2020, Aaron Oman and the gslibc contributors
 * SPDX-License-Identifier: LGPL-3.0-only
 *-----------------------------------------------------------------------------
 *
 * Standard library for personal use. Heavily influenced by Sean Barrett's stb.
 *
 ******************************************************************************/
#ifndef GS_H
#define GS_H
#define GS_VERSION 0.2.0-dev

#define GSArraySize(Array) (sizeof((Array)) / sizeof((Array)[0]))

/******************************************************************************
 * Usage:
 *
 * int Numbers[] = { 1, 2, 3, 4, 5 };
 * GSArrayForEach(int *Number, Numbers) {
 *         printf("Number[%i]: %i\n", Index, *Number);
 * }
 *
 * NOTE:
 * The variable `Index' is automatically generated for you.
 * `Item' must be a pointer to the type of variable used in the Array.
 *
 * Implementation taken from: http://stackoverflow.com/a/400970
 ******************************************************************************/
#define GSArrayForEach(Item, Array) \
        for (int Keep##__LINE__ = 1, \
                Count##__LINE__ = 0, \
                Index = 0, \
                Size##__LINE__ = sizeof((Array)) / sizeof(*(Array)); \
            Keep##__LINE__ && Count##__LINE__ != Size##__LINE__; \
            Keep##__LINE__ = !Keep##__LINE__, Count##__LINE__++) \
                for (Item = (Array) + Count##__LINE__; Keep##__LINE__; Keep##__LINE__ = !Keep##__LINE__, Index++)

#define GSMax(A, B) ((A) < (B) ? (B) : (A))
#define GSMin(A, B) ((A) < (B) ? (A) : (B))

// TODO: Move to platform-specific header
/* #define GSAbortWithMessage(...) \ */
/*         { \ */
/*                 char String##__LINE__[256];                             \ */
/*                 sprintf(String##__LINE__, "In %s() at line #%i: ", __func__, __LINE__); \ */
/*                 fprintf(stderr, String##__LINE__);                       \ */
/*                 fprintf(stderr, __VA_ARGS__); \ */
/*                 exit(EXIT_FAILURE); \ */
/*         } */

// TODO: Move to platform-specific header
/* #define GSLog(...) \ */
/*         { \ */
/*                 char String##__LINE__[256];                             \ */
/*                 sprintf(String##__LINE__, "In %s() at line #%i: ", __func__, __LINE__); \ */
/*                 fprintf(stdout, String##__LINE__);                       \ */
/*                 fprintf(stdout, __VA_ARGS__); \ */
/*         } */

#define GS1024Inverse 1.0/1024
#define GSBytesToKilobytes(X) (X) * GS1024Inverse
#define GSBytesToMegabytes(X) GSBytesToKilobytes((X)) * GS1024Inverse
#define GSBytesToGigabytes(X) GSBytesToMegabytes((X)) * GS1024Inverse

/******************************************************************************
 * Primitive Type Definitions
 * TODO: Conditionally do typedefs?
 ******************************************************************************/

#define GSNullChar '\0'

#ifndef NULL
#define NULL 0
#endif

#define GSNullPtr NULL

typedef int bool;
#ifndef false
#define false 0
#define true !false
#endif

typedef char i8;
typedef unsigned char u8;
typedef short i16;
typedef unsigned short u16;
typedef int i32;
typedef unsigned int u32;
typedef long i64;
typedef unsigned long u64;
typedef long long i128;
typedef unsigned long long u128;

typedef float f32;
typedef double f64;
typedef long double f128;

/******************************************************************************
 * Allocator
 ******************************************************************************/

typedef struct gs_allocator {
        void *(*Alloc)(u64);
        void (*Free)(void *);
        void *(*Realloc)(void *, u64);
        void *(*ArrayAlloc)(u64, u64);
} gs_allocator;

/******************************************************************************
 * Character Definitions
 *-----------------------------------------------------------------------------
 * Functions to interact with C's basic ASCII char type.
 ******************************************************************************/

bool GSCharIsEndOfStream(char c) {
	return c == '\0';
}

bool GSCharIsEndOfLine(char c) {
	return (c == '\n') || (c == '\r');
}

bool GSCharIsWhitespace(char c) {
	return (c == ' ') ||
	       (c == '\t') ||
	       (c == '\v') ||
	       (c == '\f') ||
	       GSCharIsEndOfLine(c);
}

bool GSCharIsOctal(char c) {
	return (c >= '0' && c <= '7');
}

bool GSCharIsDecimal(char c) {
	return (c >= '0' && c <= '9');
}

bool GSCharIsHexadecimal(char c) {
	return ((c >= '0' && c <= '9') ||
                (c >= 'a' && c <= 'f') ||
                (c >= 'A' && c <= 'F'));
}

bool GSCharIsAlphabetical(char c) {
	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}

bool GSCharIsAlphanumeric(char c) {
        return GSCharIsAlphabetical(c) || GSCharIsDecimal(c);
}

bool GSCharIsUpcase(char c) {
        return GSCharIsAlphabetical(c) && (c >= 'A') && (c <= 'z');
}

char GSCharUpcase(char c) {
        char Result = c;

        if (GSCharIsAlphabetical(c) && (c >= 'a' && c <= 'z')) {
                int Delta = c - 'a';
                Result = Delta + 'A';
        }

        return Result;
}

bool GSCharIsDowncase(char c) {
        return GSCharIsAlphabetical(c) && (c >= 'a') && (c <= 'z');
}

char GSCharDowncase(char c) {
        char Result = c;

        if (GSCharIsAlphabetical(c) && (c >= 'A' && c <= 'Z')) {
                int Delta = c - 'A';
                Result = Delta + 'a';
        }

        return Result;
}

/******************************************************************************
 * String Definitions
 *-----------------------------------------------------------------------------
 * C string type. That is, ASCII characters with terminating NULL.
 ******************************************************************************/

bool GSStringIsEqual(char *LeftString, char *RightString, int MaxNumToMatch) {
	int NumMatched = 0;

        if (*LeftString == GSNullChar ||
           *RightString == GSNullChar &&
           *LeftString != *RightString)
        {
                return false;
        }

	while (NumMatched < MaxNumToMatch) {
		if (*LeftString == *RightString) {
			LeftString++;
			RightString++;
			NumMatched++;
		} else {
			return false;
		}
	}

	return true;
}

u32 GSStringLength(char *String) {
	char *c = String;
	while (*c != '\0') c++;
	return c - String;
}

bool GSStringCopy(char *Source, char *Dest, int Max) {
        if (Source == NULL || Dest == NULL) {
                return false;
        }

        int i = 0;
        for (; Source[i] != '\0' && i < Max; i++) {
                Dest[i] = Source[i];
        }
        Dest[i] = '\0';

        return true;
}

bool GSStringCopyNoNull(char *Source, char *Dest, int Max) {
        if (Source == NULL || Dest == NULL) {
                return false;
        }

        for (int i = 0; Source[i] != '\0' && i < Max; i++) {
                Dest[i] = Source[i];
        }

        return true;
}

/* NOTE: Assumes a maximum string length of 512 bytes. */
/* Returns number of bytes copied. */
u32 GSStringTrimWhitespace(char *Source, u32 MaxLength) {
        char Dest[512];
        MaxLength = GSMin(512, MaxLength);

        int FirstChar, LastChar;
        for (FirstChar = 0; GSCharIsWhitespace(Source[FirstChar]); FirstChar++);

        int StringLength = GSStringLength(Source);
        for (LastChar = StringLength - 1; GSCharIsWhitespace(Source[LastChar]); LastChar--);

        int Count = 0;
        for (int i = FirstChar; i <= LastChar && Count < MaxLength; Count++, i++) {
                Dest[Count] = Source[i];
        }

        for (int i = 0; i < Count; i++) {
                Source[i] = Dest[i];
        }
        Source[Count] = GSNullChar;

        return Count;
}

/*
  For any ascii character following an underscore, remove the underscore
  and capitalize the ascii char.
  This function assumes a maximum string size of 512 bytes.
  The first character is capitalized.
*/
u32 GSStringSnakeCaseToCamelCase(char *Source, u32 SourceLength) {
        char Dest[512]; /* Scratch buffer. */
        int Si = 0, Di = 0; /* Iterable indices for Source and Dest. */

        if ((Source[Si] == '_') &&
           (Si+1 < SourceLength) &&
           GSCharIsAlphabetical(Source[Si+1])) {
                Si++;
        }
        Dest[Di] = GSCharUpcase(Source[Si]);
        Si++;
        Di++;

        SourceLength = GSMin(512, SourceLength);

        for (Si, Di; Si<SourceLength; Si++, Di++) {
                /* Replace any '_*' with 'upcase(*)' where * is an ascii char. */
                if ((Source[Si] == '_') &&
                   (Si+1 < SourceLength) &&
                   GSCharIsAlphabetical(Source[Si+1])) {
                        Dest[Di] = GSCharUpcase(Source[Si+1]);
                        Si++;
                }
                /* Copy chars normally. */
                else {
                        Dest[Di] = Source[Si];
                }
        }

        /* Write the modified string back to source. */
        for (int i = 0; i < Di; i++) {
                Source[i] = Dest[i];
        }
        Source[Di] = GSNullChar;

        return Di;
}

/*
  Prerequisites:
  - Dest must be large enough to contain the modified string.

  For any Capitalized ascii character, replace with an underscore followed by
  the lowercase version of that character. This does not apply to leading char.
  eg.: CamelCase -> Camel_case
*/
u32 GSStringCamelCaseToSnakeCase(char *Source, char *Dest, u32 SourceLength) {
        int i = 0, j = 0; /* Iterable indices for Source and Dest. */
        Dest[i] = GSCharDowncase(Source[i]);
        i++;
        j++;

        for (i, j; i<SourceLength && Source[i] != GSNullChar; i++, j++) {
                /* Replace upcase ascii char with '_' and downcase ascii char. */
                if (GSCharIsUpcase(Source[i])) {
                        Dest[j] = '_';
                        j++;
                        Dest[j] = GSCharDowncase(Source[i]);
                }
                /* Copy chars normally. */
                else {
                        Dest[j] = Source[i];
                }
        }
        Dest[j] = GSNullChar;

        return j;
}

/*
  Capitalizes the first character found.
  Modifies Source in-place.
  Returns Source.
  eg.: hello -> Hello
       123foos -> 123Foos
*/
char *GSStringCapitalize(char *Source, u32 Length) {
        int i = 0;

        while (true) {
                if (i >= Length)
                        break;
                if (Source[i] == GSNullChar)
                        break;
                if (GSCharIsAlphabetical(Source[i]))
                        break;
                i++;
        }

        if (i >= Length)
                return Source;

        Source[i] = GSCharUpcase(Source[i]);

        return Source;
}

typedef bool (*GSStringFilterFn)(char C);

/* Returns length of new string */
int GSStringKeep(char *Source, char *Dest, u32 MaxLength, GSStringFilterFn FilterFn) {
        int i = 0;
        int j = 0;

	while (i < MaxLength) {
                if (FilterFn(Source[i])) {
                        Dest[j] = Source[i];
                        j++;
                }
                i++;
        }
        Dest[j] = GSNullChar;

        return j + 1;
}

/* Returns length of new string */
int GSStringReject(char *Source, char *Dest, u32 MaxLength, GSStringFilterFn FilterFn) {
        int i = 0;
        int j = 0;

	while (i < MaxLength) {
                if (!FilterFn(Source[i])) {
                        Dest[j] = Source[i];
                        j++;
                }
                i++;
        }
        Dest[j] = GSNullChar;

        return j + 1;
}

/******************************************************************************
 * Hash Map
 *-----------------------------------------------------------------------------
 *
 * Usage:
 *     char *Value = "value";
 *     int StringLength = 256;
 *     int NumElements = 13;
 *     u32 BytesRequired = GSHashMapBytesRequired(StringLength, NumElements);
 *     gs_hash_map *Map = GSHashMapInit(alloca(BytesRequired), StringLength, NumElements);
 *     GSHashMapSet(Map, "key", Value);
 *     if (GSHashMapHasKey(Map, "key")) {
 *         char *Result = (char *)GSHashMapGet(Map, "key");
 *         printf("Key(%s), Value(%s)\n", "key", Result);
 *     }
 ******************************************************************************/

typedef struct gs_hash_map {
        u32 Count;
        u32 AllocatedBytes;
        u32 Capacity;
        u32 MaxKeyLength;

        char *Keys;
        void **Values;
} gs_hash_map;

/* String must be a NULL-terminated string */
u32 __GSHashMapComputeHash(gs_hash_map *Self, char *String) {
        /*
          sdbm hash function: http://stackoverflow.com/a/14409947
        */
        u32 HashAddress = 0;
        for (u32 i = 0; String[i] != GSNullChar; i++) {
                HashAddress = String[i] +
                        (HashAddress << 6) +
                        (HashAddress << 16) -
                        HashAddress;
        }
        u32 Result = HashAddress % Self->Capacity;
        return Result;
}

u32 GSHashMapBytesRequired(u32 MaxKeyLength, u32 NumEntries) {
        int AllocSize =
                sizeof(gs_hash_map) +
                (sizeof(char) * MaxKeyLength * NumEntries) +
                (sizeof(void *) * NumEntries);

        return AllocSize;
}

gs_hash_map *GSHashMapInit(void *Memory, u32 MaxKeyLength, u32 NumEntries) {
        gs_hash_map *Self = (gs_hash_map *)Memory;

        char *KeyValueMemory = (char *)Memory + sizeof(gs_hash_map);

        Self->MaxKeyLength = MaxKeyLength;
        Self->Capacity = NumEntries;
        Self->AllocatedBytes = GSHashMapBytesRequired(MaxKeyLength, NumEntries);
        Self->Count = 0;

        int KeysMemLength = MaxKeyLength * NumEntries;

        Self->Keys = KeyValueMemory;
        for (int i = 0; i < KeysMemLength; i++) {
                Self->Keys[i] = 0;
        }

        Self->Values = (void **)(Self->Keys + KeysMemLength);
        for (int i = 0; i < NumEntries; i++) {
                Self->Values[i] = 0;
        }

        return Self;
}

bool __GSHashMapUpdate(gs_hash_map *Self, char *Key, void *Value) {
        u32 KeyLength = GSStringLength(Key);
        u32 HashIndex = __GSHashMapComputeHash(Self, Key);

        u32 StartHash = HashIndex;

        do {
                if (GSStringIsEqual(&Self->Keys[HashIndex * Self->MaxKeyLength],
                                   Key,
                                   GSStringLength(Key))) {
                        Self->Values[HashIndex] = Value;
                        return true;
                }
                HashIndex = (HashIndex + 1) % Self->Capacity;
        } while (HashIndex != StartHash);

        /* Couldn't find Key to update. */
        return false;
}

/* Wanted must be a NULL terminated string */
bool GSHashMapHasKey(gs_hash_map *Self, char *Wanted) {
        u32 HashIndex = __GSHashMapComputeHash(Self, Wanted);
        char *Key = &Self->Keys[HashIndex * Self->MaxKeyLength];
        if (GSStringIsEqual(Wanted, Key, GSStringLength(Wanted))) {
                return true;
        }

        u32 StartHash = HashIndex;
        HashIndex = (HashIndex + 1) % Self->Capacity;

        while (true) {
                if (HashIndex == StartHash) break;

                Key = &Self->Keys[HashIndex * Self->MaxKeyLength];
                if (GSStringIsEqual(Wanted, Key, GSStringLength(Wanted))) {
                        return true;
                }
                HashIndex = (HashIndex + 1) % Self->Capacity;
        }

        return false;
}

/*
  Input: Key as string
  Computation: Hash key value into an integer.
  Algorithm: Open-addressing hash. Easy to predict space usage.
             See: https://en.wikipedia.org/wiki/Open_addressing
  Key must be a NULL terminated string.
 */
bool GSHashMapSet(gs_hash_map *Self, char *Key, void *Value) {
        u32 KeyLength = GSStringLength(Key);
        u32 HashIndex = __GSHashMapComputeHash(Self, Key);

        if (GSHashMapHasKey(Self, Key)) {
                return __GSHashMapUpdate(Self, Key, Value);
        }

        /* We're not updating, so return false if we're at capacity. */
        if (Self->Count >= Self->Capacity) return false;

        /* Add a brand-new key in. */
        if (Self->Keys[HashIndex * Self->MaxKeyLength] == GSNullChar) {
                GSStringCopy(Key, &Self->Keys[HashIndex * Self->MaxKeyLength], KeyLength);
                Self->Values[HashIndex] = Value;
                Self->Count++;
                return true;
        }

        /* We have a collision! Find a free index. */
        u32 StartHash = HashIndex;
        HashIndex = (HashIndex + 1) % Self->Capacity;

        while (true) {
                if (HashIndex == StartHash) break;

                if (Self->Keys[HashIndex * Self->MaxKeyLength] == GSNullChar) {
                        GSStringCopy(Key, &Self->Keys[HashIndex * Self->MaxKeyLength], KeyLength);
                        Self->Values[HashIndex] = Value;
                        Self->Count++;
                        return true;
                }
                HashIndex = (HashIndex + 1) % Self->Capacity;
        }

        /* Couldn't find any free space. */
        return false;
}

/* Memory must be large enough for the resized Hash. Memory _cannot_ overlap! */
bool GSHashMapGrow(gs_hash_map **Self, u32 NumEntries, void *New) {
        gs_hash_map *Old = *Self;

        /* No point in making smaller... */
        if (NumEntries <= Old->Capacity) return false;
        if (New == NULL) return false;

        *Self = GSHashMapInit(New, Old->MaxKeyLength, NumEntries);
        for (int i = 0; i < Old->Capacity; i++) {
                char *Key = &Old->Keys[i * Old->MaxKeyLength];
                char *Value = (char *)(Old->Values[i]);
                if (Key != NULL) {
                        bool Success = GSHashMapSet(*Self, Key, Value);
                        if (!Success) return false;
                }
        }

        return true;
}

/* Wanted must be a NULL terminated string */
void *GSHashMapGet(gs_hash_map *Self, char *Wanted) {
        u32 HashIndex = __GSHashMapComputeHash(Self, Wanted);
        char *Key = &Self->Keys[HashIndex * Self->MaxKeyLength];
        if (GSStringIsEqual(Wanted, Key, GSStringLength(Key))) {
                void *Result = Self->Values[HashIndex];
                return Result;
        }

        u32 StartHash = HashIndex;
        HashIndex = (HashIndex + 1) % Self->Capacity;

        while (true) {
                if (HashIndex == StartHash) break;

                Key = &Self->Keys[HashIndex * Self->MaxKeyLength];
                if (GSStringIsEqual(Wanted, Key, GSStringLength(Key))) {
                        void *Result = Self->Values[HashIndex];
                        return Result;
                }
                HashIndex = (HashIndex + 1) % Self->Capacity;
        }

        return NULL;
}

/* Wanted must be a NULL terminated string */
void *GSHashMapDelete(gs_hash_map *Self, char *Wanted) {
        u32 HashIndex = __GSHashMapComputeHash(Self, Wanted);
        char *Key = &Self->Keys[HashIndex * Self->MaxKeyLength];
        if (GSStringIsEqual(Wanted, Key, GSStringLength(Key))) {
                void *Result = Self->Values[HashIndex];
                Self->Values[HashIndex] = NULL;
                Self->Keys[HashIndex * Self->MaxKeyLength] = GSNullChar;
                Self->Count--;
                return Result;
        }

        u32 StartHash = HashIndex;
        HashIndex = (HashIndex + 1) % Self->Capacity;

        while (true) {
                if (HashIndex == StartHash) break;

                Key = &Self->Keys[HashIndex * Self->MaxKeyLength];
                if (GSStringIsEqual(Wanted, Key, GSStringLength(Key))) {
                        void *Result = Self->Values[HashIndex];
                        Self->Values[HashIndex] = NULL;
                        Self->Keys[HashIndex * Self->MaxKeyLength] = GSNullChar;
                        Self->Count--;
                        return Result;
                }
                HashIndex = (HashIndex + 1) % Self->Capacity;
        }

        return NULL;
}

/******************************************************************************
 * Byte streams / Buffers / File IO
 ******************************************************************************/

typedef struct gs_buffer {
        char *Start;
        char *Cursor;
        u64 Capacity;
        u64 Length;
        char *SavedCursor;
} gs_buffer;

gs_buffer *GSBufferInit(gs_buffer *Buffer, char *Start, u64 Size) {
        Buffer->Start = Start;
        Buffer->Cursor = Start;
        Buffer->Length = 0;
        Buffer->Capacity = Size;
        Buffer->SavedCursor = NULL;
        return Buffer;
}

bool GSBufferIsEOF(gs_buffer *Buffer) {
        u64 Size = Buffer->Cursor - Buffer->Start;
        bool Result = Size >= Buffer->Length;
        return Result;
}

void GSBufferNextLine(gs_buffer *Buffer) {
        while (true) {
                if (Buffer->Cursor[0] == '\n' ||
                   Buffer->Cursor[0] == '\0') {
                        break;
                }
                Buffer->Cursor++;
        }
        Buffer->Cursor++;
}

bool GSBufferSaveCursor(gs_buffer *Buffer) {
        Buffer->SavedCursor = Buffer->Cursor;
        return true;
}

bool GSBufferRestoreCursor(gs_buffer *Buffer) {
        if (Buffer->SavedCursor == NULL) return false;

        Buffer->Cursor = Buffer->SavedCursor;
        Buffer->SavedCursor = NULL;
        return true;
}

// TODO: Move to platform-specific header include.
/* size_t */
/* GSFileSize(char *FileName) */
/* { */
/*         size_t FileSize = 0; */
/*         FILE *File = fopen(FileName, "r"); */
/*         if (File != NULL) */
/*         { */
/*                 fseek(File, 0, SEEK_END); */
/*                 FileSize = ftell(File); */
/*                 fclose(File); */
/*         } */
/*         return FileSize; */
/* } */

// TODO: Move to platform-specific header include
/* bool */
/* GSFileCopyToBuffer(char *FileName, gs_buffer *Buffer) */
/* { */
/*         FILE *File = fopen(FileName, "r"); */
/*         if (File == NULL) return false; */

/*         fseek(File, 0, SEEK_END); */
/*         size_t FileSize = ftell(File); */
/*         int Remaining = (Buffer->Start + Buffer->Capacity) - Buffer->Cursor; */
/*         if (FileSize > Remaining) return false; */

/*         fseek(File, 0, SEEK_SET); */
/*         size_t BytesRead = fread(Buffer->Cursor, 1, FileSize, File); */
/*         Buffer->Length += FileSize; */
/*         Buffer->Cursor += FileSize; */
/*         *(Buffer->Cursor) = '\0'; */

/*         return true; */
/* } */

#endif /* GS_H */