replace VG_STATIC -> static
[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
48 enum strncpy_behaviour{
49 k_strncpy_always_add_null = 0,
50 k_strncpy_allow_cutoff = 1,
51 k_strncpy_overflow_fatal = 2
52 };
53
54 static void vg_fatal_error( const char *fmt, ... );
55 static u32 vg_strncpy( const char *src, char *dst, u32 len,
56 enum strncpy_behaviour behaviour )
57 {
58 for( u32 i=0; i<len; i++ ){
59 dst[i] = src[i];
60
61 if( !src[i] ) return i;
62
63 if( i == len-1 ){
64 if( behaviour == k_strncpy_always_add_null ){
65 dst[i] = '\0';
66 return i;
67 }
68 else if( behaviour == k_strncpy_overflow_fatal ){
69 vg_fatal_error( "Strncpy dest exceeded buffer length\n" );
70 }
71 }
72 }
73
74 return 0;
75 }
76
77 typedef struct vg_str vg_str;
78 struct vg_str{
79 char *buffer;
80 u32 i, len;
81 };
82
83 static void vg_strnull( vg_str *str, char *buffer, u32 len )
84 {
85 str->buffer = buffer;
86 str->buffer[0] = '\0';
87 str->i = 0;
88 str->len = len;
89 }
90
91 static void vg_strcat( vg_str *str, const char *append )
92 {
93 if( !append ) return;
94 for( u32 i=0; str->i < str->len; i++, str->i ++ ){
95 str->buffer[ str->i ] = append[i];
96
97 if( append[i] == '\0' ) return;
98 }
99 }
100
101 static int vg_strgood( vg_str *str )
102 {
103 if( str->i == str->len ){
104 if( str->buffer[str->i -1] == '\0' ) return 1;
105 else return 0;
106 }
107 else return 1;
108 }
109
110 static char *vg_strch( vg_str *str, char c )
111 {
112 char *ptr = NULL;
113 for( u32 i=0; i<str->i; i++ ){
114 if( str->buffer[i] == c )
115 ptr = str->buffer+i;
116 }
117
118 return ptr;
119 }
120
121 static u32 vg_strdjb2( const char *str )
122 {
123 u32 hash = 5381, c;
124
125 while( (c = *str++) )
126 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
127
128 return hash;
129 }
130
131 static int vg_strdjb2_eq( const char *s1, u32 h1,
132 const char *s2, u32 h2 )
133 {
134 if( h1 == h2 ){
135 if(!strcmp(s1, s2)) return 1;
136 else return 0;
137 } else return 0;
138 }
139
140 #define VG_STRDJB2_EQ( CS1, S2, H2 ) \
141 vg_strdjb2_eq( CS1, vg_strdjb2(CS1), S2, H2 )
142
143
144 #define VG_MIN( A, B ) ((A)<(B)?(A):(B))
145 #define VG_MAX( A, B ) ((A)>(B)?(A):(B))
146 #endif