-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraph_file_format.h
3304 lines (2812 loc) · 107 KB
/
graph_file_format.h
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#ifndef GF_GRAPH_FILE_FORMAT_H
#define GF_GRAPH_FILE_FORMAT_H
// This header file parses the .gf/.graph file format.
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <ctype.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#include <inttypes.h>
#include <limits.h>
/*----------------------------------TYPEDEFS----------------------------------------*/
typedef uint32_t gf_u32;
typedef uint64_t gf_u64;
typedef int32_t gf_s32;
typedef int64_t gf_s64;
typedef float gf_f32;
typedef double gf_f64;
/*----------------------------------------------------------------------------------*/
/*-------------------------STRING CONVERSIONS---------------------------------------*/
/*
Name: int gf_AreStringSpansEqual(const char *a, gf_u64 alength, const char *b, gf_u64 blength);
Description: Checks if *a and *b spanning strings are equal in value.
Assumptions: - *a and *b are not NULL and they are NULL terminated strings.
- alength and blength are the string lengths of a and b respectively (they do not include the null terminating character).
Returns: Returns 1 if they are equal. 0 if they are not.
*/
int gf_AreStringSpansEqual(const char *a, gf_u64 alength, const char *b, gf_u64 blength);
/*
Name: gf_u64 gf_StringLength(const char *str);
Description: Computes the length of *str.
Assumptions: - *str is not NULL and is NULL terminated.
Returns: The length of the string as a gf_u64.
*/
gf_u64 gf_StringLength(const char *str);
/*
Name: int gf_StringSpanToU32(const char *start, uint64_t length, gf_u32 *value);
Description: Converts a string that spans a certain length into an unsigned 32 bit integer.
If the provided length is longer than the NULL terminated string then it will only parse
up to the null terminated character.
Assumptions: - *start is not NULL and is NULL terminated.
- *value is not NULL
Returns: Returns 1 if the conversion was successful. 0 if it was not successful.
*/
int gf_StringSpanToU32(const char *start, uint64_t length, gf_u32 *value);
/*
Name: int gf_StringSpanToU64(const char *start, uint64_t length, gf_u64 *value);
Description: Converts a string that spans a certain length into an unsigned 64 bit integer.
If the provided length is longer than the NULL terminated string then it will only parse
up to the null terminated character.
Assumptions: - *start is not NULL and is NULL terminated.
- *value is not NULL
Returns: Returns 1 if the conversion was successful. 0 if it was not successful.
*/
int gf_StringSpanToU64(const char *start, uint64_t length, gf_u64 *value);
/*
Name: int gf_StringSpanToS32(const char *start, uint64_t length, gf_s32 *value);
Description: Converts a string that spans a certain length into an signed 32 bit integer.
If the provided length is longer than the NULL terminated string then it will only parse
up to the null terminated character.
Assumptions: - *start is not NULL and is NULL terminated.
- *value is not NULL
Returns: Returns 1 if the conversion was successful. 0 if it was not successful.
*/
int gf_StringSpanToS32(const char *start, uint64_t length, gf_s32 *value);
/*
Name: int gf_StringSpanToS64(const char *start, uint64_t length, gf_s64 *value);
Description: Converts a string that spans a certain length into an signed 64 bit integer.
If the provided length is longer than the NULL terminated string then it will only parse
up to the null terminated character.
Assumptions: - *start is not NULL and is NULL terminated.
- *value is not NULL
Returns: Returns 1 if the conversion was successful. 0 if it was not successful.
*/
int gf_StringSpanToS64(const char *start, uint64_t length, gf_s64 *value);
/*
Name: int gf_StringSpanToF64(const char *start, uint64_t length, gf_f64 *value);
Description: Converts a string that spans a certain length into an 64 bit floating point number.
If the provided length is longer than the NULL terminated string then it will only parse
up to the null terminated character.
Assumptions: - *start is not NULL and is NULL terminated.
- *value is not NULL
Returns: Returns 1 if the conversion was successful. 0 if it was not successful.
*/
int gf_StringSpanToF64(const char *start, uint64_t length, gf_f64 *value);
/*
Name: int gf_StringSpanToF32(const char *start, uint64_t length, gf_f32 *value);
Description: Converts a string that spans a certain length into an 32 bit floating point number.
If the provided length is longer than the NULL terminated string then it will only parse
up to the null terminated character.
Assumptions: - *start is not NULL and is NULL terminated.
- *value is not NULL
Returns: Returns 1 if the conversion was successful. 0 if it was not successful.
*/
int gf_StringSpanToF32(const char *start, uint64_t length, gf_f32 *value);
/*-----------------------------------------------------------------------------------*/
/*-------------------------------------TOKENS----------------------------------------*/
typedef enum gf_TokenType {
GF_TOKEN_TYPE_ROOT, // The type exclusively held by the hidden root token of the parser.
GF_TOKEN_TYPE_NAME, // A named/identifier token that isn't a string, integer, float or string.
GF_TOKEN_TYPE_CURLY_CLOSE, // This } token.
GF_TOKEN_TYPE_COMMENT, // The token associated with a comment which is /* and */
GF_TOKEN_TYPE_COMPOSITE_TYPE, // This is a named/identifier token that contains other tokens.
GF_TOKEN_TYPE_STRING, // A string value token which is between two quotations ""
GF_TOKEN_TYPE_FLOAT, // A floating point value like 1.0 or 1.
GF_TOKEN_TYPE_INTEGER, // An integer value which is a essentially a number without a floating point.
GF_TOKEN_TYPE_END_FILE, // The token reserved for the end of a file.
GF_TOKEN_TYPE_VALUE_ASSIGN // This token { which designates an assignment is about to happen for a composite token.
} gf_TokenType;
/*
Holds information about each token that is created from the tokeniser.
Tokens are stored as a singly linked list where each token points to the next one.
*/
typedef struct gf_Token {
const char *start; // The pointer into the buffer that signifies the start string of the token.
gf_u64 length; // The length of the start string.
gf_TokenType type; // The token type
gf_u64 lineno; // The line number that this token starts on.
gf_u64 colno; // The column number on the current line that this token starts on.
struct gf_Token *next; // The next token in the list
} gf_Token;
/*
Name: void gf_PrintToken(gf_Token *token);
Description: Prints a token to stdout.
Assumptions: - *token is not NULL and is NULL terminated.
Returns: Nothing.
*/
void gf_PrintToken(gf_Token *token);
/*
Name: void gf_PrintLineToken(gf_Token *token);
Description: Prints a token to stdout with a newline character at the end
Assumptions: - *token is not NULL and is NULL terminated.
Returns: Nothing.
*/
void gf_PrintLineToken(gf_Token *token);
/*
Name: const char *gf_TokenTypeToString(gf_TokenType type);
Description: Returns a NULL terminated string which corresponds to the given name of a token type.
Assumptions: - type is a valid gf_TokenType
Returns: The names of the token type as a string. The string is NULL terminated.
*/
const char *gf_TokenTypeToString(gf_TokenType type);
/*----------------------------------------------------------------------------------*/
/*-------------------------LOG AND FUNCTION POINTERS--------------------------*/
// Used to specify what kind of "error" you want to log.
typedef enum gf_LogLevel {
GF_LOG_ERROR = 0,
GF_LOG_WARNING,
GF_LOG_INFO
} gf_LogLevel;
/*
Name: gf_LogFunctionPtr
Description: This is the function signature for the log function. This can be user defined. The provided token can be NULL.
*/
typedef void (*gf_LogFunctionPtr)(gf_LogLevel, int, gf_Token *, const char *, va_list);
/*
Name: gf_AllocatorFunctionPtr
Description: This is the function signature for the allocation function. This can be user defined. By default this is malloc().
It must allocate pointers that are not invalidated with subsequent allocations.
*/
typedef void *(*gf_AllocatorFunctionPtr)(size_t);
/*
Name: gf_FreeFunctionPtr
Description: This is the function signature for the free function. This can be user defined. By default this is free().
This function must be able to deal with NULL pointers.
*/
typedef void (*gf_FreeFunctionPtr)(void *);
/*
Name: gf_LogAllocateFreeFunctions
Description: This is a helper struct that stores function pointers responsible for logging, allocating and freeing.
If any of these are NULL then gf_DefaultLog(), malloc() and free() are used respectively.
The allocator must obey the rules of malloc() which means it's pointers can not be invalidated after they
are allocated. free() must work on a NULL.
*/
typedef struct gf_LogAllocateFreeFunctions {
gf_LogFunctionPtr Log; // A pointer to the log function you want to use. If this is NULL gf_DefaultLog() is used
gf_AllocatorFunctionPtr Allocate; // A pointer to the allocation function you want to use. If this is NULL malloc() is used.
gf_FreeFunctionPtr Free; // A pointer to the free function you want to use. If this is NULL free() is used.
} gf_LogAllocateFreeFunctions;
/*
Name: void gf_DefaultLog(gf_LogLevel level, int lineno, gf_Token *, const char *format, va_list vlist);
Description: The default logging function used if none is specified. This prints to stdout.
Assumptions: - The token can be NULL.
- *format is assumed to be not NULL.
Returns: Nothing.
*/
void gf_DefaultLog(gf_LogLevel level, int lineno, gf_Token *, const char *format, va_list vlist);
/*
Name: void gf_Log(gf_LogFunctionPtr Log, gf_LogLevel level, int lineno, gf_Token *, const char *format, ...);
Description: An internal function that is called when the logging macro is invoked. This function calls the user specified log function.
Assumptions: - The token can be NULL.
- *format is assumed to be not NULL.
Returns: Nothing.
*/
void gf_Log(gf_LogFunctionPtr Log, gf_LogLevel level, int lineno, gf_Token *, const char *format, ...);
/*
Name: GF_LOG(loaderOrSaver, level, ...) gf_Log(loaderOrSaver->Log, level, __LINE__, NULL, __VA_ARGS__)
Description: A helper macro that calls gf_Log which in turn, calls the users logging function. A macro is used so the line number can be obtained
Assumptions: - loaderOrSave is not NULL and is of type gf_Loader or gf_Saver.
Returns: Nothing.
*/
#define GF_LOG(loaderOrSaver, level, ...) gf_Log(loaderOrSaver->Log, level, __LINE__, NULL, __VA_ARGS__)
/*
Name: GF_LOG(loaderOrSaver, level, ...) gf_Log(loaderOrSaver->Log, level, __LINE__, NULL, __VA_ARGS__)
Description: The same as GF_LOG but with a token too.
Assumptions: - loaderOrSave is not NULL and is of type gf_Loader or gf_Saver.
- The token can be NULL.
Returns: Nothing.
*/
#define GF_LOG_WITH_TOKEN(loaderOrSaver, level, token, ...) gf_Log(loaderOrSaver->Log, level, __LINE__, token, __VA_ARGS__)
/*-----------------------------------------------------------------------------------*/
/*--------------------------------------SAVER----------------------------------------*/
// A helper function used to save data. Use this to begin "serialisation".
typedef struct gf_Saver {
gf_LogFunctionPtr Log; // The function used to log errors.
unsigned int indent; // Tracks the current indent level of your data. Each level of nested data has an indentation.
} gf_Saver;
/*
Name: void gf_InitSaver(gf_Saver *saver, gf_LogFunctionPtr logfunction);
Description: Initialises the gf_Saver. Must be called before using the gf_Saver.
You can call it again to reset the saver.
If the logfunction is set to the NULL the default gf_DefaultLog function is used instead.
logfunction is a function pointer to a logging function of gf_LogFunctionPtr signature.
Assumptions: - saver is not NULL.
- logfunction can be NULL. If it is, the default logging function is used.
Returns: Nothing.
Examples:
{
gf_Saver saver;
gf_SaveInit(&saver, NULL);
}
*/
void gf_InitSaver(gf_Saver *saver, gf_LogFunctionPtr logfunction);
/*
Name: int gf_PrintIndent(gf_Saver *saver, FILE *file);
Description: Internal function used to insert spaces into the file corresponding to the current indent level of the saver.
Assumptions: - gf_InitSaver must have been called on saver atleast once.
- *saver is not NULL
- *file is not NULL and is a valid file.
Returns: 1 if it was successful. 0 if not. If an error occurs this is logged.
*/
int gf_PrintIndent(gf_Saver *saver, FILE *file);
/*
Name: int gf_SaveVariableS64(gf_Saver *saver, FILE *file, const char *identifier, gf_s64 *value);
Description: Saves a variable to the file with the given value. This will be in the format "a { 2 }"
where "a" is the identifier and "2" is the value.
Assumptions: - gf_InitSaver must have been called on saver atleast once.
- *saver is not NULL
- *file is not NULL and is a valid file.
- *identifier is not NULL and is a NULL terminated string.
- *value is not NULL
Returns: 1 if it was successful. 0 if not. If an error occurs this is logged.
Examples:
{
gf_Saver saver;
gf_SaveInit(&saver, NULL);
FILE *file = fopen(file, "myfile", "rb");
if (!file) return 0;
gf_s64 value = 1;
gf_SaveVariableS64(&saver, file, "identifier", &value);
fclose(file);
}
*/
int gf_SaveVariableS64(gf_Saver *saver, FILE *file, const char *identifier, gf_s64 *value);
/*
Name: int gf_SaveVariableS32(gf_Saver *saver, FILE *file, const char *identifier, gf_s32 *value);
Description: Saves a variable to the file with the given value. This will be in the format "a { 2 }"
where "a" is the identifier and "2" is the value.
Assumptions: - gf_InitSaver must have been called on saver atleast once.
- *saver is not NULL
- *file is not NULL and is a valid file.
- *identifier is not NULL and is a NULL terminated string.
- *value is not NULL
Returns: 1 if it was successful. 0 if not. If an error occurs this is logged.
Examples:
{
gf_Saver saver;
gf_SaveInit(&saver, NULL);
FILE *file = fopen(file, "myfile", "rb");
if (!file) return 0;
gf_s32 value = 1;
gf_SaveVariableS32(&saver, file, "identifier", &value);
fclose(file);
}
*/
int gf_SaveVariableS32(gf_Saver *saver, FILE *file, const char *identifier, gf_s32 *value);
/*
Name: int gf_SaveVariableU32(gf_Saver *saver, FILE *file, const char *identifier, gf_u32 *value);
Description: Saves a variable to the file with the given value. This will be in the format "a { 2 }"
where "a" is the identifier and "2" is the value.
Assumptions: - gf_InitSaver must have been called on saver atleast once.
- *saver is not NULL
- *file is not NULL and is a valid file.
- *identifier is not NULL and is a NULL terminated string.
- *value is not NULL
Returns: 1 if it was successful. 0 if not. If an error occurs this is logged.
Examples:
{
gf_Saver saver;
gf_SaveInit(&saver, NULL);
FILE *file = fopen(file, "myfile", "rb");
if (!file) return 0;
gf_u32 value = 1;
gf_SaveVariableU32(&saver, file, "identifier", &value);
fclose(file);
}
*/
int gf_SaveVariableU32(gf_Saver *saver, FILE *file, const char *identifier, gf_u32 *value);
/*
Name: int gf_SaveVariableU64(gf_Saver *saver, FILE *file, const char *identifier, gf_u64 *value);
Description: Saves a variable to the file with the given value. This will be in the format "a { 2 }"
where "a" is the identifier and "2" is the value.
Assumptions: - gf_InitSaver must have been called on saver atleast once.
- *saver is not NULL
- *file is not NULL and is a valid file.
- *identifier is not NULL and is a NULL terminated string.
- *value is not NULL
Returns: 1 if it was successful. 0 if not. If an error occurs this is logged.
Examples:
{
gf_Saver saver;
gf_SaveInit(&saver, NULL);
FILE *file = fopen(file, "myfile", "rb");
if (!file) return 0;
gf_u64 value = 1;
gf_SaveVariableU64(&saver, file, "identifier", &value);
fclose(file);
}
*/
int gf_SaveVariableU64(gf_Saver *saver, FILE *file, const char *identifier, gf_u64 *value);
/*
Name: int gf_SaveVariableString(gf_Saver *saver, FILE *file, const char *identifier, const char *str);
Description: Saves a string to the file with the given value. This will be in the format "a { "Hello, world" }"
where "a" is the identifier and "Hello, world" is the string.
Assumptions: - gf_InitSaver must have been called on saver atleast once.
- *saver is not NULL
- *file is not NULL and is a valid file.
- *identifier is not NULL and is a NULL terminated string.
- *str is not NULL and is a NULL terminated string.
Returns: 1 if it was successful. 0 if not. If an error occurs this is logged.
Examples:
{
gf_Saver saver;
gf_SaveInit(&saver, NULL);
FILE *file = fopen(file, "myfile", "rb");
if (!file) return 0;
const char *str = "hello, world";
gf_SaveVariableString(&saver, file, "identifier", str);
fclose(file);
}
*/
int gf_SaveVariableString(gf_Saver *saver, FILE *file, const char *identifier, const char *str);
/*
Name: int gf_SaveVariableF32(gf_Saver *saver, FILE *file, const char *identifier, gf_f32 *value);
Description: Saves a variable to the file with the given value. This will be in the format "a { 2.0 }"
where "a" is the identifier and "2.0" is the value.
Assumptions: - gf_InitSaver must have been called on saver atleast once.
- *saver is not NULL
- *file is not NULL and is a valid file.
- *identifier is not NULL and is a NULL terminated string.
- *value is not NULL
Returns: 1 if it was successful. 0 if not. If an error occurs this is logged.
Examples:
{
gf_Saver saver;
gf_SaveInit(&saver, NULL);
FILE *file = fopen(file, "myfile", "rb");
if (!file) return 0;
float value = 1.0;
gf_SaveVariableF32(&saver, file, "identifier", &value);
fclose(file);
}
*/
int gf_SaveVariableF32(gf_Saver *saver, FILE *file, const char *identifier, gf_f32 *value);
/*
Name: int gf_SaveVariableF64(gf_Saver *saver, FILE *file, const char *identifier, gf_f64 *value);
Description: Saves a variable to the file with the given value. This will be in the format "a { 2.0 }"
where "a" is the identifier and "2.0" is the value.
Assumptions: - gf_InitSaver must have been called on saver atleast once.
- *saver is not NULL
- *file is not NULL and is a valid file.
- *identifier is not NULL and is a NULL terminated string.
- *value is not NULL
Returns: 1 if it was successful. 0 if not. If an error occurs this is logged.
Examples:
{
gf_Saver saver;
gf_SaveInit(&saver, NULL);
FILE *file = fopen(file, "myfile", "rb");
if (!file) return 0;
double value = 1.0;
gf_SaveVariableF64(&saver, file, "identifier", &value);
fclose(file);
}
*/
int gf_SaveVariableF64(gf_Saver *saver, FILE *file, const char *identifier, gf_f64 *value);
/*
Name: int gf_SaveVariableStringSpan(gf_Saver *saver, FILE *file, const char *identifier, const char *str, int strLen);
Description: Saves a string span to the file with the given value. This will be in the format "a { "Hello, world" }"
where "a" is the identifier and "Hellow, world" is the string.
Assumptions: - gf_InitSaver must have been called on saver atleast once.
- *saver is not NULL
- *file is not NULL and is a valid file.
- *identifier is not NULL and is a NULL terminated string.
- *str is not NULL and is a NULL terminated string.
- if strlen is longer than the NULL terminated string then only the string up to the NULL terminated character is saved.
Returns: 1 if it was successful. 0 if not. If an error occurs this is logged.
Examples:
{
gf_Saver saver;
gf_SaveInit(&saver, NULL);
FILE *file = fopen(file, "myfile", "rb");
if (!file) return 0;
const char *str = "hello, world";
gf_SaveVariableStringSpan(&saver, file, "identifier", str, 5);
fclose(file);
}
*/
int gf_SaveVariableStringSpan(gf_Saver *saver, FILE *file, const char *identifier, const char *str, int strLen);
/*
Name: int gf_SaveVariableVec3(gf_Saver *saver, FILE *file, const char *identifier, gf_f32 *x, gf_f32 *y, gf_f32 *z);
Description: Saves a vector to the file with the given value. This will be in the format "a { 2.0, 1.0, 1.2 }"
where "a" is the identifier and "2.0, 1.0, 1.2" is x, y and z respectively.
Assumptions: - gf_InitSaver must have been called on saver atleast once.
- *saver is not NULL
- *file is not NULL and is a valid file.
- *identifier is not NULL and is a NULL terminated string.
- *x, *y and *z are not NULL
Returns: 1 if it was successful. 0 if not. If an error occurs this is logged.
Examples:
{
gf_Saver saver;
gf_SaveInit(&saver, NULL);
FILE *file = fopen(file, "myfile", "rb");
if (!file) return 0;
float x = 1.0f;
float y = 2.0f;
float z = 3.0f;
gf_SaveVariableVec3(&saver, file, "identifier", x, y, z);
fclose(file);
}
*/
int gf_SaveVariableVec3(gf_Saver *saver, FILE *file, const char *identifier, gf_f32 *x, gf_f32 *y, gf_f32 *z);
/*
Name: int gf_SaveArrayU64(gf_Saver *saver, FILE *file, const char *identifier, gf_u64 *value, int count);
Description: Saves an array to the file. This will be in the format "a { 1, 2, ... }"
up to a "count" of values.
Assumptions: - gf_InitSaver must have been called on saver atleast once.
- *saver is not NULL
- *file is not NULL and is a valid file.
- *identifier is not NULL and is a NULL terminated string.
- *value is not NULL
- value is atleast of length count.
Returns: 1 if it was successful. 0 if not. If an error occurs this is logged.
Examples:
{
gf_Saver saver;
gf_SaveInit(&saver, NULL);
FILE *file = fopen(file, "myfile", "rb");
if (!file) return 0;
gf_u64 value[] = { 1, 2 };
gf_SaveArrayU64(&saver, file, "identifier", value, 2);
fclose(file);
}
*/
int gf_SaveArrayU64(gf_Saver *saver, FILE *file, const char *identifier, gf_u64 *value, int count);
/*
Name: int gf_SaveArrayS64(gf_Saver *saver, FILE *file, const char *identifier, gf_s64 *value, int count);
Description: Saves an array to the file. This will be in the format "a { 1, 2, ... }"
up to a "count" of values.
Assumptions: - gf_InitSaver must have been called on saver atleast once.
- *saver is not NULL
- *file is not NULL and is a valid file.
- *identifier is not NULL and is a NULL terminated string.
- *value is not NULL
- value is atleast of length count.
Returns: 1 if it was successful. 0 if not. If an error occurs this is logged.
Examples:
{
gf_Saver saver;
gf_SaveInit(&saver, NULL);
FILE *file = fopen(file, "myfile", "rb");
if (!file) return 0;
gf_s64 value[] = { 1, 2 };
gf_SaveArrayS64(&saver, file, "identifier", value, 2);
fclose(file);
}
*/
int gf_SaveArrayS64(gf_Saver *saver, FILE *file, const char *identifier, gf_s64 *value, int count);
/*
Name: int gf_SaveArrayS32(gf_Saver *saver, FILE *file, const char *identifier, gf_s32 *value, int count);
Description: Saves an array to the file. This will be in the format "a { 1, 2, ... }"
up to a "count" of values.
Assumptions: - gf_InitSaver must have been called on saver atleast once.
- *saver is not NULL
- *file is not NULL and is a valid file.
- *identifier is not NULL and is a NULL terminated string.
- *value is not NULL
- value is atleast of length count.
Returns: 1 if it was successful. 0 if not. If an error occurs this is logged.
Examples:
{
gf_Saver saver;
gf_SaveInit(&saver, NULL);
FILE *file = fopen(file, "myfile", "rb");
if (!file) return 0;
gf_u32 value[] = { 1, 2 };
gf_SaveArrayS32(&saver, file, "identifier", value, 2);
fclose(file);
}
*/
int gf_SaveArrayS32(gf_Saver *saver, FILE *file, const char *identifier, gf_s32 *value, int count);
/*
Name: int gf_SaveStartList(gf_Saver *saver, FILE *file, const char *identifier);
Description: Saves a composite list to the file. This will be in the format "a { "
If you want to start writing an arbitrary list of values then this is a good start.
Must be capped off with a call to gf_SaveEndList otherwise your file will be malformed.
Assumptions: - gf_InitSaver must have been called on saver atleast once.
- *saver is not NULL
- *file is not NULL and is a valid file.
- *identifier is not NULL and is a NULL terminated string.
- When finished saveing to this list you end with a call to gf_SaveEndList.
Returns: 1 if it was successful. 0 if not. If an error occurs this is logged.
Examples:
{
gf_Saver saver;
gf_SaveInit(&saver, NULL);
FILE *file = fopen(file, "myfile", "rb");
if (!file) return 0;
gf_SaverStartList(&saver, file);
{
float value = 1.0f;
gf_SaveVariableF64(&saver, file, "myfloat", &value);
}
gf_SaverEndList(&saver, file);
fclose(file);
}
*/
int gf_SaveStartList(gf_Saver *saver, FILE *file, const char *identifier);
/*
Name: int gf_SaveEndList(gf_Saver *saver, FILE *file);
Description: Saves the end of a composite list to the file. This will be in the format " }"
If you want to end a list this is how you do it.
Assumptions: - gf_InitSaver must have been called on saver atleast once.
- *saver is not NULL
- *file is not NULL and is a valid file.
- *identifier is not NULL and is a NULL terminated string.
- Assumes gf_SaveEndList was called to start the list you want to end.
Returns: 1 if it was successful. 0 if not. If an error occurs this is logged.
Examples:
{
gf_Saver saver;
gf_SaveInit(&saver, NULL);
FILE *file = fopen(file, "myfile", "rb");
if (!file) return 0;
gf_SaverStartList(&saver, file);
{
float value = 1.0f;
gf_SaveVariableF64(&saver, file, "myfloat", &value);
}
gf_SaverEndList(&saver, file);
fclose(file);
}
*/
int gf_SaveEndList(gf_Saver *saver, FILE *file);
/*-----------------------------------------------------------------------------------*/
/*------------------------------------LOADER-----------------------------------------*/
/*
Stores a pointer to a buffer that you want to tokeniser. Tracks the current index
into this buffer as you tokenise. Also tracks things like the current line number and
column number.
*/
typedef struct gf_Tokeniser {
const char *buffer; // A pointer to a null terminated buffer that needs to be tokenised
gf_u64 index; // The internal tracking index that records the current character in the buffer
gf_u64 count; // The total size of the buffer.
gf_u64 lineno; // The current line number.
gf_u64 colno; // The current column number.
} gf_Tokeniser;
/*
Name: void gf_InitTokeniser(gf_Tokeniser *tokeniser, const char *buffer, gf_u64 count);
Description: Initialises the tokeniser. This must be called before using the tokeniser. You can call this multiple times to reset the tokeniser.
The buffer you wish to tokenise is passed in, along with the buffer length (count). This is not copied. So, while you
are tokenising and using the tokeniser, this buffer must be valid.
This buffer must be NULL terminated.
Assumptions: - *tokeniser is not NULL
- *buffer is not NULL and is NULL terminated.
- The count must represent a valid length for the buffer. If NULL terminating character is
encountered before the count is reached then tokenising will stop.
Returns: Nothing.
*/
void gf_InitTokeniser(gf_Tokeniser *tokeniser, const char *buffer, gf_u64 count);
/*
Name: char gf_GetChar(gf_Tokeniser *tokeniser);
Description: Get the character at the tokenisers internal current index, from the buffer pointed to by the tokeniser.
If the buffer's end is reached this will return a NULL terminating character.
Assumptions: - tokeniser has been initialised with a call to gf_InitTokeniser();
- *tokeniser is not NULL
- Assumes the buffer that the tokeniser is pointing to is still valid.
Returns: Returns the character of the buffer pointed to by the tokeniser, at the tracked index. Returns a NULL terminator if the end of this buffer is reached.
*/
char gf_GetChar(gf_Tokeniser *tokeniser);
/*
Name: void gf_IncrementIndex(gf_Tokeniser *tokeniser);
Description: Increments the internal tracking index of the tokeniser. If the end of the buffer tracked
by the tokeniser is reached, this tracking index is not incremented.
Assumptions: - tokeniser has been initialised with a call to gf_InitTokeniser();
- *tokeniser is not NULL
Returns: Nothing.
*/
void gf_IncrementIndex(gf_Tokeniser *tokeniser);
/*
Name: void gf_IncrementLineNo(gf_Tokeniser *tokeniser);
Description: Increments the current line number that is being tracked by the tokeniser.
Assumptions: - tokeniser has been initialised with a call to gf_InitTokeniser();
- *tokeniser is not NULL
Returns: Nothing.
*/
void gf_IncrementLineNo(gf_Tokeniser *tokeniser);
/*
Name: const char *gf_Ptr(gf_Tokeniser *tokeniser);
Description: Returns a pointer into the buffer pointed to by the tokeniser at the internal tokeniser index.
Assumptions: - tokeniser has been initialised with a call to gf_InitTokeniser();
- *tokeniser is not NULL
- Assumes the buffer that the tokeniser is pointing to is still valid.
Returns: A pointer into the buffer that the tokeniser points to at the location of the tokenisers internal tracking index.
If the end of the buffer is reached by the internal tracking index, then a NULL is returned.
*/
const char *gf_Ptr(gf_Tokeniser *tokeniser);
/*
A node that is stored as a graph that represents parsed text.
A node has an associated token. When the text forms a list these
nodes are stored as a doubly linked list where they are next to each other.
When data is nested, nodes will be stored as children of other nodes.
*/
typedef struct gf_LoaderNode {
gf_Token *token; // The token associated with this list
struct gf_LoaderNode *parent; // The parent of this node. This will only ever be NULL for the root token.
struct gf_LoaderNode *next; // The next node in the list.
struct gf_LoaderNode *prev; // The prev node in the list
struct gf_LoaderNode *childrenHead; // The head of the children list of this node
struct gf_LoaderNode *childrenTail; // The tail of the children list of this node.
struct gf_LoaderNode *nextAllocated; // A helper linked list that tracks allocated nodes.
} gf_LoaderNode;
/*
A helper function that is responsible for storing all tokens, nodes and potentially a buffer that
was allocated when opening a file.
*/
typedef struct gf_Loader {
gf_LogFunctionPtr Log; // The function used to log.
gf_AllocatorFunctionPtr Allocate; // The function used to allocate.
gf_FreeFunctionPtr Free; // The function used to free
gf_Token rootToken; // The root token that is always guaranteed to exist when parsing text.
gf_Token *lastToken; // The last token in the token list.
gf_Token *firstToken; // The first token in the token list.
gf_Token *curToken; // The current token in the token list.
gf_LoaderNode *rootNode; // The root node in the node graph.
gf_LoaderNode *lastNode; // The last allocated node in the node graph.
char *fileContentsBuffer; // A pointer that points to memory allocated from a file.
gf_u64 nestLevel; // When parsing, tracks how many {} we are nested in.
} gf_Loader;
/*
Name: void gf_InitLoader(gf_Loader *loader, gf_LogAllocateFreeFunctions *helperfunctions);
Description: Initialises the loader. This is called internally when calling one of the gf_Load...() functions. Do not call again until gf_Unload is called.
Ideally don't call at all and just call one of the load function instead to begin using the loader.
The loader is initialised with a log, allocate and
free function pointer data structure. If this is NULL, the default logging function, malloc() and free() are used respectively.
If it is not NULL and any of the helper functions within are NULL these default to gf_DefaultLog, malloc() and free respectively.
The allocator MUST have pointers that are not invalidated as more data is allocated.
Assumptions: - *loader is not NULL
- gf_InitLoader() has not been called yet.
Returns: Nothing.
*/
void gf_InitLoader(gf_Loader *loader, gf_LogAllocateFreeFunctions *helperfunctions);
/*
Name: void gf_IncrementLastTokenLength(gf_Loader *loader);
Description: Increments the length of the last allocated token by loader.
Assumptions: - gf_InitLoader() has been called on *loader.
- gf_LoadInternal() has been called. i.e. some data has been loaded.
- *loader is not NULL.
Returns: Nothing.
*/
void gf_IncrementLastTokenLength(gf_Loader *loader);
/*
Name: int gf_AddToken(gf_Loader *loader, const char *start, gf_TokenType type, gf_u64 lineno, gf_u64 colno);
Description: Allocates a new token used by the loader. The user specified allocator is used when the loader is
initialised. The token is given the specified type and lineno along with the start string within the loaded
buffer. It starts with a length of 1.
Assumptions: - gf_InitLoader() has been called on *loader.
- gf_LoadInternal() has been called. i.e. some data has been loaded.
- *loader is not NULL
- *start is not NULL
Returns: Returns 1 if it succeeds. 0 if it fails. The error is logged.
*/
int gf_AddToken(gf_Loader *loader, const char *start, gf_TokenType type, gf_u64 lineno, gf_u64 colno);
/*
Name: char *gf_AllocateNullTerminatedBufferFromFile(gf_Loader *loader, const char *filename, gf_u64 *bufferCountWithNullTerminator);
Description: Opens the given file. Allocates it's contents using loader's defined allocator (+1 to include the null terminator).
Then closes the file.
This allocated buffer is returned as a NULL terminated string.
The size of the allocated buffer is stored in bufferCountWithNullTerminator.
Assumptions: - gf_InitLoader() has been called on *loader.
- *loader is not NULL
- *filename is NOT NULL
- bufferCountWithNullTerminator is not NULL.
Returns: Returns a terminated string that holds the entire contents of the file +1 for the NULL terminator.
Returns NULL if this failed and the error is logged.
*/
char *gf_AllocateNullTerminatedBufferFromFile(gf_Loader *loader, const char *filename, gf_u64 *bufferCountWithNullTerminator);
/*
Name: int gf_TokeniseInternal(gf_Loader *loader, gf_Tokeniser *tokeniser);
Description: An internal function that begins tokenising the buffer pointed to by the tokeniser.
Assumptions: - gf_InitLoader() has been called on *loader.
- gf_InitTokeniser() has been called on *tokeniser.
- *loader is not NULL
- *tokeniser is not NULL
Returns: Returns 1 if this succeeds. 0 if this fails. The error is logged.
*/
int gf_TokeniseInternal(gf_Loader *loader, gf_Tokeniser *tokeniser);
/*
Name: int gf_Tokenise(gf_Loader *loader, const char *buffer, gf_u64 count);
Description: Tokenises the NULL terminated buffer that has the length count.
Assumptions: - gf_InitLoader() has been called on *loader.
- *buffer is NULL terminated.
- count is a valid length for the buffer.
- *loader is not NULL
- *buffer is not NULL
Returns: Returns 1 if this succeeds. 0 if this fails. The error is logged.
*/
int gf_Tokenise(gf_Loader *loader, const char *buffer, gf_u64 count);
/*
Name: gf_Token *gf_ConsumeToken(gf_Loader *loader);
Description: Returns the next token in the token list and increments the current tracking index that
points to the current token.
Assumptions: - gf_InitLoader() has been called on *loader.
- *loader is not NULL
Returns: Returns NULL if there are no more tokens, else returns the token as a pointer.
*/
gf_Token *gf_ConsumeToken(gf_Loader *loader);
/*
Name: gf_Token *gf_PeekToken(gf_Loader *loader);
Description: Returns the current token.
Assumptions: - gf_InitLoader() has been called on *loader.
- *loader is not NULL
Returns: Returns the current token. This can be NULL.
*/
gf_Token *gf_PeekToken(gf_Loader *loader);
/*
Name: gf_LoaderNode *gf_AddNode(gf_Loader *loader, gf_Token *token);
Description: Allocates a node using the specified allocation function. Assigns the token to this
node. Returns the newly allocated node.
Assumptions: - gf_InitLoader() has been called on *loader.
- *token is not NULL.
Returns: Returns the newly allocated node. Returns NULL if it fails. The error is logged.
*/
gf_LoaderNode *gf_AddNode(gf_Loader *loader, gf_Token *token);
/*
Name: void gf_AddChild(gf_LoaderNode *parent, gf_LoaderNode *child);
Description: Adds the child node to the parents children list of nodes.
Assumptions: - gf_InitLoader() has been called on *loader.
- *parent is not NULL.
- *child is not NULL.
Returns: Nothing.
*/
void gf_AddChild(gf_LoaderNode *parent, gf_LoaderNode *child);
/*
Name: int gf_Parse(gf_Loader *loader, gf_LoaderNode *parentNode);
Description: Parses the token list. The tokeniser must have been called before this happens and
the root node of loader must have been created. This is a recursive function. The first time
you call it, parentNode must be the root node of the loader.
Assumptions: - gf_InitLoader() has been called on *loader.
- The loader has performed tokenisation.
- *loader is not NULL.
- *parentNode is not NULL and needs to be the root node when you call this initially.
Returns: Returns 1 if it succeeds. Returns 0 if it fails. The error is logged.
*/
int gf_Parse(gf_Loader *loader, gf_LoaderNode *parentNode);
/*
Name: int gf_LoadInternal(gf_Loader *loader, const char *buffer, gf_u64 bufferCount);
Description: Begins tokenising and parsing the passed in buffer, preparing data that can be queried by the user.
Buffer needs to be NULL terminated. The bufferCount needs to represent a valid span for the buffer.
Assumptions: - *loader is not NULL.
- *buffer is not NULL.
- bufferCount is a valid length for the buffer.
Returns: Returns 1 if it succeeds. Returns 0 if it fails. The error is logged.
*/
int gf_LoadInternal(gf_Loader *loader, const char *buffer, gf_u64 bufferCount);
/*
Name: int gf_LoadFromBuffer(gf_Loader *loader, const char *buffer, gf_u64 bufferCount, gf_LogAllocateFreeFunctions *funcs);
Description: Begins tokenising and parsing the passed in buffer, preparing data that can be queried by the user.
This should be the first thing you call before using the loader. After you are done, you need to call gf_Unload(), even if this function fails.
The buffer needs to be NULL terminated. The bufferCount needs to represent a valid span for the buffer.
*funcs can be NULL or contain NULL function pointers. In this case, the default function pointers are used,
gf_DefaultLog, malloc() and free().
This function allocates things using the passed in allocation function.
Assumptions: - *loader is not NULL.
- *buffer is not NULL.
- funcs can be NULL.
- bufferCount is a valid length for the buffer.
Returns: Returns 1 if it succeeds. Returns 0 if it fails. The error is logged.
Examples:
{
gf_Loader loader;
const char *buffer = "mydata { 1.0 }";
gf_LoadFromBuffer(&loader, buffer, gf_StringLength(buffer), NULL);
// ... do stuff ...
gf_Unload(&loader);
}
*/
int gf_LoadFromBuffer(gf_Loader *loader, const char *buffer, gf_u64 bufferCount, gf_LogAllocateFreeFunctions *funcs);
/*
Name: int gf_LoadFromFile(gf_Loader *loader, const char *filename, gf_LogAllocateFreeFunctions *funcs);
Description: Begins tokenising and parsing the passed in buffer, preparing data that can be queried by the user.
This should be the first thing you call before using the loader. After you are done, you need to call gf_Unload(), even if this function fails.
This opens the specified file and allocates its contents into a buffer. This buffer is then tokenised and parsed to
be inspected by the user.
*funcs can be NULL or contain NULL function pointers. In this case, the default function pointers are used,
gf_DefaultLog, malloc() and free().
This function allocates things using the passed in allocation function.
This open
Assumptions: - *loader is not NULL.
- *filename is not NULL.
- funcs can be NULL.
Returns: Returns 1 if it succeeds. Returns 0 if it fails. The error is logged.
Examples:
{
gf_Loader loader;
const char *filename = "myfile.gf";
gf_LoadFromFile(&loader, filename, NULL);
// ... do stuff ...
gf_Unload(&loader);
}
*/
int gf_LoadFromFile(gf_Loader *loader, const char *filename, gf_LogAllocateFreeFunctions *funcs);
/*
Name: void gf_Unload(gf_Loader *loader);
Description: Frees all data allocated by the loader using the user defined Free() function. Must be called after gf_Load...()
function has been called prefably when you are finished with the loader. It does not matter,
if the load failed, you still need to call gf_Unload.
After unload is called you must load the loader again with new data if you want to use it.
If you called gf_LoadFromFile, this deallocates the buffer that was allocated.
Assumptions: - gf_LoadFromBuffer or gf_LoadFromFile has been called.
- *loader is not NULL.
Returns: Nothing
Examples:
{
gf_Loader loader;
const char *filename = "myfile.gf";
gf_LoadFromFile(&loader, filename, NULL);
// ... do stuff ...
gf_Unload(&loader);
}
*/
void gf_Unload(gf_Loader *loader);
/*
Name: int gf_LoaderNodeToU32(gf_Loader *loader, gf_LoaderNode *node, gf_u32 *value);
Description: Converts the value of the node and copies it into value.
Does type checking of the node.
A u32 node is an integer node. This takes the form => 123
Assumptions: - gf_LoadFromBuffer or gf_LoadFromFile has been called and was successful.
- *loader is not NULL.
- *value is not NULL.
- node can be NULL.
Returns: Returns 1 if the conversion was successful. Returns 0 if node was NULL, the node
was not an integer type or the conversion fail. The error is logged.
Examples:
{
gf_Loader loader;
const char *filename = "myfile.gf";
gf_LoadFromFile(&loader, filename, NULL);