Skip to content
Myr

various fixes:

Before adoption: this commit came in with the history the project adopted, so no landing records it.

add stb_intcmprev add stb_uidict fixes to stb__dirtree_scandir change rand functions from unsigned long to unsigned int so they're 32-bit on Linux as well

authored by Sean Barrett · committed by Sean Barrett · parent e5254a7 · Browse files · 1 file+102 added−39 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+102 added−39 removed

  • other:+102 added−39 removed

1 file+102 added−39 removed

modified:stb.h+102 added-39 removed
789 unmodified lines
790791static void stb__print_one(void *handle, char *s, int len)792{793 if (len)794 if (WriteConsoleA(handle, s, len, NULL,NULL))794 if (0==WriteConsoleA(handle, s, len, NULL,NULL))795 fwrite(s, 1, len, stdout); // if it fails, maybe redirected, so do normal796}797798static void stb__print(char *s)
743 unmodified lines
1542#endif154315441545STB_EXTERN int (*stb_intcmp(int offset))(const void *a, const void *b);1546STB_EXTERN int (*stb_intcmprev(int offset))(const void *a, const void *b);1547STB_EXTERN int (*stb_qsort_strcmp(int offset))(const void *a, const void *b);1548STB_EXTERN int (*stb_qsort_stricmp(int offset))(const void *a, const void *b);1549STB_EXTERN int (*stb_floatcmp(int offset))(const void *a, const void *b);1550STB_EXTERN int (*stb_doublecmp(int offset))(const void *a, const void *b);
9 unmodified lines
1560 const int q = *(const int *) ((const char *) b + stb__intcmpoffset);1561 return p < q ? -1 : p > q;1562}15631564int stb__intcmprev(const void *a, const void *b)1565{1566 const int p = *(const int *) ((const char *) a + stb__intcmpoffset);1567 const int q = *(const int *) ((const char *) b + stb__intcmpoffset);1568 return q < p ? -1 : q > p;1569}15701571int stb__ucharcmp(const void *a, const void *b)1572{1573 const int p = *(const unsigned char *) ((const char *) a + stb__ucharcmpoffset);1574 const int q = *(const unsigned char *) ((const char *) b + stb__ucharcmpoffset);
33 unmodified lines
1608 stb__intcmpoffset = offset;1609 return &stb__intcmp;1610}16111612int (*stb_intcmprev(int offset))(const void *, const void *)1613{1614 stb__intcmpoffset = offset;1615 return &stb__intcmprev;1616}16171618int (*stb_ucharcmp(int offset))(const void *, const void *)1619{1620 stb__ucharcmpoffset = offset;1621 return &stb__ucharcmp;
2471 unmodified lines
40934094#define STB_equal(a,b) ((a) == (b))40954096#define stb_define_hash(TYPE,N,KEY,EMPTY,DEL,HASH,VALUE) \4083 stb_define_hash_base(STB_noprefix, TYPE,STB_nofields,N,NC,0.85f, \4097 stb_define_hash_base(STB_noprefix, TYPE,STB_nofields,N,NC,0.85f, \4098 KEY,EMPTY,DEL,STB_nocopy,STB_nodelete,STB_nosafe, \4099 STB_equal,STB_equal,HASH, \4100 VALUE,STB_nonullvalue,0)41014102#define stb_define_hash_vnull(TYPE,N,KEY,EMPTY,DEL,HASH,VALUE,VNULL) \4089 stb_define_hash_base(STB_noprefix, TYPE,STB_nofields,N,NC,0.85f, \4103 stb_define_hash_base(STB_noprefix, TYPE,STB_nofields,N,NC,0.85f, \4104 KEY,EMPTY,DEL,STB_nocopy,STB_nodelete,STB_nosafe, \4105 STB_equal,STB_equal,HASH, \4106 VALUE,STB_nullvalue,VNULL)4107
6 unmodified lines
4114// which is why it was originally called stb_extra.41154116stb_declare_hash(STB_EXTERN, stb_ptrmap, stb_ptrmap_, void *, void *)4117stb_declare_hash(STB_EXTERN, stb_idict, stb_idict_, stb_int32, stb_int32)4118stb_declare_hash(STB_EXTERN, stb_uidict, stbi_uidict_, stb_uint32, stb_uint32)41194120STB_EXTERN void stb_ptrmap_delete(stb_ptrmap *e, void (*free_func)(void *));4121STB_EXTERN stb_ptrmap *stb_ptrmap_new(void);41224123STB_EXTERN stb_idict * stb_idict_new_size(int size);4124STB_EXTERN void stb_idict_remove_all(stb_idict *e);4125STB_EXTERN void stb_uidict_reset(stb_uidict *e);41264127#ifdef STB_DEFINE41284129#define STB_EMPTY ((void *) 2)
48 unmodified lines
4178 int n;4179 for (n=0; n < e->limit; ++n)4180 e->table[n].k = STB_IEMPTY;4181 e->has_empty = e->has_del = 0;4182 e->count = 0;4183 e->deleted = 0;4184}41854186stb_define_hash_base(STB_noprefix, stb_uidict, STB_nofields, stb_uidict_,stb_uidict_,0.85f,4187 stb_int32,0xffffffff,0xfffffffe,STB_nocopy,STB_nodelete,STB_nosafe,4188 STB_equal,STB_equal,4189 return stb_rehash_improved(k);,stb_uint32,STB_nonullvalue,0)41904191void stb_uidict_reset(stb_uidict *e)4192{4193 int n;4194 for (n=0; n < e->limit; ++n)4195 e->table[n].k = 0xffffffff;4196 e->has_empty = e->has_del = 0;4197 e->count = 0;4198 e->deleted = 0;4199}4200#endif42014202//////////////////////////////////////////////////////////////////////////////4203//
1762 unmodified lines
5966 ++i;5967 while (i < *argc)5968 argv[j++] = argv[i++];5969 break;5970 } else if (argv[i][1] == '-') {5971 // copy argument through including initial '-' for clarity5972 stb_arr_push(opts, strdup(argv[i]));5973 } else {5974 int k;5975 char *q = argv[i]; // traverse options list5976 for (k=1; q[k]; ++k) {
653 unmodified lines
6630#define STB__REG_OPTION_NON_VOLATILE 06631#define STB__REG_KEY_ALL_ACCESS 0x000f003f6632#define STB__REG_KEY_READ 0x0002001966336634#ifdef _M_AMD646635#define STB__HKEY_CURRENT_USER 0x80000001ull6636#define STB__HKEY_LOCAL_MACHINE 0x80000002ull6637#else6638#define STB__HKEY_CURRENT_USER 0x800000016639#define STB__HKEY_LOCAL_MACHINE 0x800000026640#endif66416642void *stb_reg_open(char *mode, char *where)6643{6644 long res;6645 HKEY base;6646 HKEY zreg;6647 if (!stb_stricmp(mode+1, "cu") || !stb_stricmp(mode+1, "hkcu"))6604 base = (HKEY) 0x80000001; // HKCU6648 base = (HKEY) STB__HKEY_CURRENT_USER;6649 else if (!stb_stricmp(mode+1, "lm") || !stb_stricmp(mode+1, "hklm"))6606 base = (HKEY) 0x80000002; // HKLM6650 base = (HKEY) STB__HKEY_LOCAL_MACHINE;6651 else6652 return NULL;66536654 if (mode[0] == 'r')
399 unmodified lines
7054 }7055 fclose(f);7056}70577058FILE *hlog;70597060static int stb__dircount, stb__dircount_mask, stb__showfile;7061static void stb__dirtree_scandir(char *path, time_t last_time, stb_dirtree *active)7062{7063 // this is dumb depth first; theoretically it might be faster
17 unmodified lines
7081 else7082 swprintf(full_path, L"%s/*", stb__from_utf8(path));7083#else7084 if (has_slash)7039 swprintf(full_path, 1024, L"%s*", stb__from_utf8(path));7085 swprintf((wchar_t *) full_path, (size_t) 1024, L"%s*", (wchar_t *) stb__from_utf8(path));7086 else7041 swprintf(full_path, 1024, L"%s/*", stb__from_utf8(path));7087 swprintf((wchar_t *) full_path, (size_t) 1024, L"%s/*", (wchar_t *) stb__from_utf8(path));7088#endif70897090 // it's possible this directory is already present: that means it was in the7091 // cache, but its parent wasn't... in that case, we're done with it
9 unmodified lines
7101 stb__dirtree_add_dir(path, last_time, active);7102 n = stb_arr_lastn(active->dirs);71037104 if (stb__showfile) printf("[");7059 if( (hFile = _wfindfirsti64( full_path, &c_file )) != -1L ) {7105 if( (hFile = _wfindfirsti64( (wchar_t *) full_path, &c_file )) != -1L ) {7106 do {7107 if (stb__showfile) printf(")");7108 if (c_file.attrib & _A_SUBDIR) {7109 // ignore subdirectories starting with '.', e.g. "." and ".."7110 if (c_file.name[0] != '.') {7111 char *new_path = (char *) full_path;7066 char *temp = stb__to_utf8(c_file.name);7112 char *temp = stb__to_utf8((stb__wchar *) c_file.name);71137114 if (has_slash)7115 sprintf(new_path, "%s%s", path, temp);7116 else
1 unmodified line
71187119 if (stb__dircount_mask) {7120 ++stb__dircount;7121 if (!(stb__dircount & stb__dircount_mask)) {7122 char dummy_path[128], *pad;7123 stb_strncpy(dummy_path, new_path, sizeof(dummy_path)-1);7124 if (strlen(dummy_path) > 96) {7125 strcpy(dummy_path+96/2-1, "...");7126 strcpy(dummy_path+96/2+2, new_path + strlen(new_path)-96/2+2);7127 }7128 pad = dummy_path + strlen(dummy_path);7129 while (pad < dummy_path+98)7130 *pad++ = ' ';7131 *pad = 0;7076 printf("%s\r", new_path);7132 printf("%s\r", dummy_path);7133 #if 07134 if (hlog == 0) {7135 hlog = fopen("c:/x/temp.log", "w");7136 fprintf(hlog, "%s\n", dummy_path);7137 }7138 #endif7139 }7140 }71417142 stb__dirtree_scandir(new_path, c_file.time_write, active);7143 }7144 } else {7083 char *temp = stb__to_utf8(c_file.name);7145 char *temp = stb__to_utf8((stb__wchar *) c_file.name);7146 stb__dirtree_add_file(temp, n, c_file.size, c_file.time_write, active);7147 }7148 if (stb__showfile) printf("(");7149 } while( _wfindnexti64( hFile, &c_file ) == 0 );
24 unmodified lines
7174 }7175 if (0 == _stat(db->dirs[i].path, &info)) {7176 if (info.st_mode & _S_IFDIR) {7177 // it's still a directory, as expected7116 int n = abs(info.st_mtime - db->dirs[i].last_modified);7178 int n = abs((int) (info.st_mtime - db->dirs[i].last_modified));7179 if (n > 1 && n != 3600) { // the 3600 is a hack because sometimes this jumps for no apparent reason, even when no time zone or DST issues are at play7180 // it's changed! force a rescan7181 // we don't want to scan it until we've stat()d its7182 // subdirs, though, so we queue it7121 if (stb__showfile) printf("Changed: %s - %08x:%08x\n", db->dirs[i].path, db->dirs[i].last_modified, info.st_mtime);7183 if (stb__showfile) printf("Changed: %s - %08x:%08x\n", db->dirs[i].path, (unsigned int) db->dirs[i].last_modified, (unsigned int) info.st_mtime);7184 stb_arr_push(rescan, i);7185 // update the last_mod time7186 db->dirs[i].last_modified = info.st_mtime;7187 // ignore existing files in this dir
132 unmodified lines
73207321stb_dirtree *stb_dirtree_get(char *dir)7322{7323 char cache_dir[256];7262 strcpy(cache_dir, "c:/stb");7324 strcpy(cache_dir, "c:/bindata");7325 #ifdef STB_HAS_REGISTRY7326 {7327 void *reg = stb_reg_open("rHKLM", "Software\\SilverSpaceship\\stb");7328 if (reg) {
938 unmodified lines
8267//8268// Random Numbers via Meresenne Twister or LCG8269//82708209STB_EXTERN unsigned long stb_srandLCG(unsigned long seed);8210STB_EXTERN unsigned long stb_randLCG(void);8271STB_EXTERN unsigned int stb_srandLCG(unsigned long seed);8272STB_EXTERN unsigned int stb_randLCG(void);8273STB_EXTERN double stb_frandLCG(void);82748213STB_EXTERN void stb_srand(unsigned long seed);8214STB_EXTERN unsigned long stb_rand(void);8275STB_EXTERN void stb_srand(unsigned int seed);8276STB_EXTERN unsigned int stb_rand(void);8277STB_EXTERN double stb_frand(void);8278STB_EXTERN void stb_shuffle(void *p, size_t n, size_t sz,8279 unsigned long seed);8280STB_EXTERN void stb_reverse(void *p, size_t n, size_t sz);82818220STB_EXTERN unsigned long stb_randLCG_explicit(unsigned long seed);8282STB_EXTERN unsigned int stb_randLCG_explicit(unsigned int seed);82838284#define stb_rand_define(x,y) \8285 \8224 unsigned long x(void) \8286 unsigned int x(void) \8287 { \8226 static unsigned long stb__rand = y; \8288 static unsigned int stb__rand = y; \8289 stb__rand = stb__rand * 2147001325 + 715136305; /* BCPL */ \8290 return 0x31415926 ^ ((stb__rand >> 16) + (stb__rand << 16)); \8291 }82928293#ifdef STB_DEFINE8232unsigned long stb_randLCG_explicit(unsigned long seed)8294unsigned int stb_randLCG_explicit(unsigned int seed)8295{8296 return seed * 2147001325 + 715136305;8297}82988237static unsigned long stb__rand_seed=0;8299static unsigned int stb__rand_seed=0;83008239unsigned long stb_srandLCG(unsigned long seed)8301unsigned int stb_srandLCG(unsigned long seed)8302{8241 unsigned long previous = stb__rand_seed;8303 unsigned int previous = stb__rand_seed;8304 stb__rand_seed = seed;8305 return previous;8306}83078246unsigned long stb_randLCG(void)8308unsigned int stb_randLCG(void)8309{8310 stb__rand_seed = stb__rand_seed * 2147001325 + 715136305; // BCPL generator8311 // shuffle non-random bits to the middle, and xor to decorrelate with seed8312 return 0x31415926 ^ ((stb__rand_seed >> 16) + (stb__rand_seed << 16));
32 unmodified lines
83458346// public domain Mersenne Twister by Michael Brundage8347#define STB__MT_LEN 62483488287int stb__mt_index = STB__MT_LEN*sizeof(unsigned long)+1;8288unsigned long stb__mt_buffer[STB__MT_LEN];8349int stb__mt_index = STB__MT_LEN*sizeof(int)+1;8350unsigned int stb__mt_buffer[STB__MT_LEN];83518290void stb_srand(unsigned long seed)8352void stb_srand(unsigned int seed)8353{8354 int i;8293 unsigned long old = stb_srandLCG(seed);8355 unsigned int old = stb_srandLCG(seed);8356 for (i = 0; i < STB__MT_LEN; i++)8357 stb__mt_buffer[i] = stb_randLCG();8358 stb_srandLCG(old);8297 stb__mt_index = STB__MT_LEN*sizeof(unsigned long);8359 stb__mt_index = STB__MT_LEN*sizeof(unsigned int);8360}83618362#define STB__MT_IA 3978363#define STB__MT_IB (STB__MT_LEN - STB__MT_IA)
2 unmodified lines
8366#define STB__MATRIX_A 0x9908B0DF8367#define STB__TWIST(b,i,j) ((b)[i] & STB__UPPER_MASK) | ((b)[j] & STB__LOWER_MASK)8368#define STB__MAGIC(s) (((s)&1)*STB__MATRIX_A)83698308unsigned long stb_rand()8370unsigned int stb_rand()8371{8310 unsigned long * b = stb__mt_buffer;8372 unsigned int * b = stb__mt_buffer;8373 int idx = stb__mt_index;8312 unsigned long s,r;8374 unsigned int s,r;8375 int i;8376 8315 if (idx >= STB__MT_LEN*sizeof(unsigned long)) {8316 if (idx > STB__MT_LEN*sizeof(unsigned long))8377 if (idx >= STB__MT_LEN*sizeof(unsigned int)) {8378 if (idx > STB__MT_LEN*sizeof(unsigned int))8379 stb_srand(0);8380 idx = 0;8381 i = 0;8382 for (; i < STB__MT_IB; i++) {
7 unmodified lines
8390 8391 s = STB__TWIST(b, STB__MT_LEN-1, 0);8392 b[STB__MT_LEN-1] = b[STB__MT_IA-1] ^ (s >> 1) ^ STB__MAGIC(s);8393 }8332 stb__mt_index = idx + sizeof(unsigned long);8394 stb__mt_index = idx + sizeof(unsigned int);8395 8334 r = *(unsigned long *)((unsigned char *)b + idx);8396 r = *(unsigned int *)((unsigned char *)b + idx);8397 8398 r ^= (r >> 11);8399 r ^= (r << 7) & 0x9D2C5680;8400 r ^= (r << 15) & 0xEFC60000;
4061 unmodified lines
12462// the parts that are in use. The overhead from using homogenous heaps12463// everywhere is 3MB. (That is, if you allocate 1 object of each size,12464// you'll use 3MB.)1246512404#if defined(STB_DEFINE) && (defined(_WIN32) || defined(STB_FASTMALLOC))12466#if defined(STB_DEFINE) && ((defined(_WIN32) && !defined(_M_AMD64)) || defined(STB_FASTMALLOC))1246712468#ifdef _WIN3212469 #ifndef _WINDOWS_12470 #ifndef STB__IMPORT
602 unmodified lines
13073#endif // !STB_DEBUG && !STB_ALWAYS_H130741307513076#ifdef STB_STUA13077#error "STUA is no longer supported"13078//////////////////////////////////////////////////////////////////////////13079//13080// stua: little scripting language13081//
1387 unmodified lines