add strings to console variables
[vg.git] / vg_platform.h
1 #ifndef VG_PLATFORM_H
2 #define VG_PLATFORM_H
3
4 //#include "vg.h"
5 #include "vg_stdint.h"
6
7 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
8
9 typedef unsigned int uint;
10
11 typedef int v2i[2];
12 typedef int v3i[3];
13 typedef int v4i[4];
14 typedef float v2f[2];
15 typedef float v3f[3];
16 typedef float v4f[4];
17 typedef v2f m2x2f[2];
18 typedef v3f m3x3f[3];
19 typedef v3f m4x3f[4];
20 typedef v4f m4x4f[4];
21 typedef v3f boxf[2];
22
23 // Resource types
24 typedef struct vg_tex2d vg_tex2d;
25
26 struct vg_achievement
27 {
28 int is_set;
29 const char *name;
30 };
31
32 #define vg_static_assert _Static_assert
33 #define vg_list_size( A ) (sizeof(A)/sizeof(A[0]))
34 #define VG_MUST_USE_RESULT __attribute__((warn_unused_result))
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdarg.h>
39 #include <ctype.h>
40 #include <math.h>
41 #include <assert.h>
42 #include <setjmp.h>
43 #include <sys/time.h>
44 #include <math.h>
45 #include <stdio.h>
46 #include <errno.h>
47 #include <stdlib.h>
48
49 #include "vg_string.h"
50
51 enum strncpy_behaviour{
52 k_strncpy_always_add_null = 0,
53 k_strncpy_allow_cutoff = 1,
54 k_strncpy_overflow_fatal = 2
55 };
56
57 static void vg_fatal_error( const char *fmt, ... );
58 static u32 vg_strncpy( const char *src, char *dst, u32 len,
59 enum strncpy_behaviour behaviour )
60 {
61 for( u32 i=0; i<len; i++ ){
62 dst[i] = src[i];
63
64 if( !src[i] ) return i;
65
66 if( i == len-1 ){
67 if( behaviour == k_strncpy_always_add_null ){
68 dst[i] = '\0';
69 return i;
70 }
71 else if( behaviour == k_strncpy_overflow_fatal ){
72 vg_fatal_error( "Strncpy dest exceeded buffer length\n" );
73 }
74 }
75 }
76
77 return 0;
78 }
79
80 static u32 vg_strdjb2( const char *str ){
81 u32 hash = 5381, c;
82
83 while( (c = *str++) )
84 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
85
86 return hash;
87 }
88
89 static int vg_strdjb2_eq( const char *s1, u32 h1,
90 const char *s2, u32 h2 )
91 {
92 if( h1 == h2 ){
93 if(!strcmp(s1, s2)) return 1;
94 else return 0;
95 } else return 0;
96 }
97
98 #define VG_STRDJB2_EQ( CS1, S2, H2 ) \
99 vg_strdjb2_eq( CS1, vg_strdjb2(CS1), S2, H2 )
100
101
102 #define VG_MIN( A, B ) ((A)<(B)?(A):(B))
103 #define VG_MAX( A, B ) ((A)>(B)?(A):(B))
104 #endif