| 1 | /*<html><pre> -<a href="index_r.htm#TOC" |
| 2 | >-------------------------------</a><a name="TOP">-</a> |
| 3 | |
| 4 | random_r.c and utilities |
| 5 | Park & Miller's minimimal standard random number generator |
| 6 | argc/argv conversion |
| 7 | |
| 8 | Used by rbox. Do not use 'qh' |
| 9 | */ |
| 10 | |
| 11 | #include "libqhull_r.h"cxx . . $ `<file>/vendor/qhull/libqhull_r/libqhull_r.h`/ |
| 12 | #include "random_r.h"cxx . . $ `<file>/vendor/qhull/libqhull_r/random_r.h`/ |
| 13 | |
| 14 | #include <string.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #ifdef _MSC_VER /* Microsoft Visual C++ -- warning level 4 */ |
| 19 | #pragma warning( disable : 4706) /* assignment within conditional function */ |
| 20 | #pragma warning( disable : 4996) /* function was declared deprecated(strcpy, localtime, etc.) */ |
| 21 | #endif |
| 22 | |
| 23 | /*-<a href="qh-globa_r.htm#TOC" |
| 24 | >-------------------------------</a><a name="argv_to_command">-</a> |
| 25 | |
| 26 | qh_argv_to_command( argc, argv, command, max_size ) |
| 27 | |
| 28 | build command from argc/argv |
| 29 | max_size is at least |
| 30 | |
| 31 | returns: |
| 32 | a space-delimited string of options (just as typed) |
| 33 | returns false if max_size is too short |
| 34 | |
| 35 | notes: |
| 36 | silently removes |
| 37 | makes option string easy to input and output |
| 38 | matches qh_argv_to_command_size |
| 39 | argc may be 0 |
| 40 | */ |
| 41 | int qh_argv_to_commandcxx . . $ qh_argv_to_command(1b4058b7c1c40fa6).(int argc, char *argv[], char* command, int max_size) { |
| 42 | int i, remaining; |
| 43 | char *s; |
| 44 | *command= '\0'; /* max_size > 0 */ |
| 45 | |
| 46 | if (argc) { |
| 47 | if ((s= strrchrcxx . . $ strrchr(2050848944e00d54).( argv[0], '\\')) /* get filename w/o .exe extension */ |
| 48 | || (s= strrchrcxx . . $ strrchr(2050848944e00d54).( argv[0], '/'))) |
| 49 | s++; |
| 50 | else |
| 51 | s= argv[0]; |
| 52 | if ((int)strlencxx . . $ strlen(751346f8406fd082).(s) < max_size) /* WARN64 */ |
| 53 | strcpycxx . . $ strcpy(b7b3a83273496486).(command, s); |
| 54 | else |
| 55 | goto error_argv; |
| 56 | if ((s= strstrcxx . . $ strstr(a713fe7b9a57cf96).(command, ".EXE")) |
| 57 | || (s= strstrcxx . . $ strstr(a713fe7b9a57cf96).(command, ".exe"))) |
| 58 | *s= '\0'; |
| 59 | } |
| 60 | for (i=1; i < argc; i++) { |
| 61 | s= argv[i]; |
| 62 | remaining= max_size - (int)strlencxx . . $ strlen(751346f8406fd082).(command) - (int)strlencxx . . $ strlen(751346f8406fd082).(s) - 2; /* WARN64 */ |
| 63 | if (!*s || strchrcxx . . $ strchr(2050848944e00d54).(s, ' ')) { |
| 64 | char *t= command + strlencxx . . $ strlen(751346f8406fd082).(command); |
| 65 | remaining -= 2; |
| 66 | if (remaining < 0) { |
| 67 | goto error_argv; |
| 68 | } |
| 69 | *t++= ' '; |
| 70 | *t++= '"'; |
| 71 | while (*s) { |
| 72 | if (*s == '"') { |
| 73 | if (--remaining < 0) |
| 74 | goto error_argv; |
| 75 | *t++= '\\'; |
| 76 | } |
| 77 | *t++= *s++; |
| 78 | } |
| 79 | *t++= '"'; |
| 80 | *t= '\0'; |
| 81 | }else if (remaining < 0) { |
| 82 | goto error_argv; |
| 83 | }else { |
| 84 | strcatcxx . . $ strcat(b7b3a83273496486).(command, " "); |
| 85 | strcatcxx . . $ strcat(b7b3a83273496486).(command, s); |
| 86 | } |
| 87 | } |
| 88 | return 1; |
| 89 | |
| 90 | error_argv: |
| 91 | return 0; |
| 92 | } /* argv_to_command */ |
| 93 | |
| 94 | /*-<a href="qh-globa_r.htm#TOC" |
| 95 | >-------------------------------</a><a name="argv_to_command_size">-</a> |
| 96 | |
| 97 | qh_argv_to_command_size( argc, argv ) |
| 98 | |
| 99 | return size to allocate for qh_argv_to_command() |
| 100 | |
| 101 | notes: |
| 102 | only called from rbox with qh_errexit not enabled |
| 103 | caller should report error if returned size is less than 1 |
| 104 | argc may be 0 |
| 105 | actual size is usually shorter |
| 106 | */ |
| 107 | int qh_argv_to_command_sizecxx . . $ qh_argv_to_command_size(afcd3168fc1188a4).(int argc, char *argv[]) { |
| 108 | int count= 1; /* null-terminator if argc==0 */ |
| 109 | int i; |
| 110 | char *s; |
| 111 | |
| 112 | for (i=0; i<argc; i++){ |
| 113 | count += (int)strlencxx . . $ strlen(751346f8406fd082).(argv[i]) + 1; /* WARN64 */ |
| 114 | if (i>0 && strchrcxx . . $ strchr(2050848944e00d54).(argv[i], ' ')) { |
| 115 | count += 2; /* quote delimiters */ |
| 116 | for (s=argv[i]; *s; s++) { |
| 117 | if (*s == '"') { |
| 118 | count++; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | return count; |
| 124 | } /* argv_to_command_size */ |
| 125 | |
| 126 | /*-<a href="qh-geom_r.htm#TOC" |
| 127 | >-------------------------------</a><a name="rand">-</a> |
| 128 | |
| 129 | qh_rand() |
| 130 | qh_srand(qh, seed ) |
| 131 | generate pseudo-random number between 1 and 2^31 -2 |
| 132 | |
| 133 | notes: |
| 134 | For qhull and rbox, called from qh_RANDOMint(),etc. [user_r.h] |
| 135 | |
| 136 | From Park & Miller's minimal standard random number generator |
| 137 | Communications of the ACM, 31:1192-1201, 1988. |
| 138 | Does not use 0 or 2^31 -1 |
| 139 | this is silently enforced by qh_srand() |
| 140 | Can make 'Rn' much faster by moving qh_rand to qh_distplane |
| 141 | */ |
| 142 | |
| 143 | /* Global variables and constants */ |
| 144 | |
| 145 | #define qh_rand_acxx . . $ `vendor/qhull/libqhull_r/random_r.c:145:9`! 16807 |
| 146 | #define qh_rand_mcxx . . $ `vendor/qhull/libqhull_r/random_r.c:146:9`! 2147483647 |
| 147 | #define qh_rand_qcxx . . $ `vendor/qhull/libqhull_r/random_r.c:147:9`! 127773 /* m div a */ |
| 148 | #define qh_rand_rcxx . . $ `vendor/qhull/libqhull_r/random_r.c:148:9`! 2836 /* m mod a */ |
| 149 | |
| 150 | int qh_randcxx . . $ qh_rand(c69f9c8928bec262).(qhTcxx . . $ qhT# *qh) { |
| 151 | int lo, hi, test; |
| 152 | int seed= qh->last_randomcxx . . $ qhT#last_random.; |
| 153 | |
| 154 | hi= seed / qh_rand_qcxx . . $ `vendor/qhull/libqhull_r/random_r.c:147:9`!; /* seed div q */ |
| 155 | lo= seed % qh_rand_qcxx . . $ `vendor/qhull/libqhull_r/random_r.c:147:9`!; /* seed mod q */ |
| 156 | test= qh_rand_acxx . . $ `vendor/qhull/libqhull_r/random_r.c:145:9`! * lo - qh_rand_rcxx . . $ `vendor/qhull/libqhull_r/random_r.c:148:9`! * hi; |
| 157 | if (test > 0) |
| 158 | seed= test; |
| 159 | else |
| 160 | seed= test + qh_rand_mcxx . . $ `vendor/qhull/libqhull_r/random_r.c:146:9`!; |
| 161 | qh->last_randomcxx . . $ qhT#last_random.= seed; |
| 162 | /* seed= seed < qh_RANDOMmax/2 ? 0 : qh_RANDOMmax; for testing */ |
| 163 | /* seed= qh_RANDOMmax; for testing */ |
| 164 | return seed; |
| 165 | } /* rand */ |
| 166 | |
| 167 | void qh_srandcxx . . $ qh_srand(dc2080372857db46).(qhTcxx . . $ qhT# *qh, int seed) { |
| 168 | if (seed < 1) |
| 169 | qh->last_randomcxx . . $ qhT#last_random.= 1; |
| 170 | else if (seed >= qh_rand_mcxx . . $ `vendor/qhull/libqhull_r/random_r.c:146:9`!) |
| 171 | qh->last_randomcxx . . $ qhT#last_random.= qh_rand_mcxx . . $ `vendor/qhull/libqhull_r/random_r.c:146:9`! - 1; |
| 172 | else |
| 173 | qh->last_randomcxx . . $ qhT#last_random.= seed; |
| 174 | } /* qh_srand */ |
| 175 | |
| 176 | /*-<a href="qh-geom_r.htm#TOC" |
| 177 | >-------------------------------</a><a name="randomfactor">-</a> |
| 178 | |
| 179 | qh_randomfactor(qh, scale, offset ) |
| 180 | return a random factor r * scale + offset |
| 181 | |
| 182 | notes: |
| 183 | qh.RANDOMa/b are defined in global_r.c |
| 184 | qh_RANDOMint requires 'qh' |
| 185 | */ |
| 186 | realTcxx . . $ `vendor/qhull/libqhull_r/user_r.h:159:9`! qh_randomfactorcxx . . $ qh_randomfactor(e9846f757e7c127).(qhTcxx . . $ qhT# *qh, realTcxx . . $ `vendor/qhull/libqhull_r/user_r.h:159:9`! scale, realTcxx . . $ `vendor/qhull/libqhull_r/user_r.h:159:9`! offset) { |
| 187 | realTcxx . . $ `vendor/qhull/libqhull_r/user_r.h:159:9`! randr; |
| 188 | |
| 189 | randr= qh_RANDOMintcxx . . $ `vendor/qhull/libqhull_r/user_r.h:314:9`!; |
| 190 | return randr * scale + offset; |
| 191 | } /* randomfactor */ |
| 192 | |
| 193 | /*-<a href="qh-geom_r.htm#TOC" |
| 194 | >-------------------------------</a><a name="randommatrix">-</a> |
| 195 | |
| 196 | qh_randommatrix(qh, buffer, dim, rows ) |
| 197 | generate a random dim X dim matrix in range [-1,1] |
| 198 | assumes buffer is [dim+1, dim] |
| 199 | |
| 200 | returns: |
| 201 | sets buffer to random numbers |
| 202 | sets rows to rows of buffer |
| 203 | sets row[dim] as scratch row |
| 204 | |
| 205 | notes: |
| 206 | qh_RANDOMint requires 'qh' |
| 207 | */ |
| 208 | void qh_randommatrixcxx . . $ qh_randommatrix(d8ead9c21c5f78a0).(qhTcxx . . $ qhT# *qh, realTcxx . . $ `vendor/qhull/libqhull_r/user_r.h:159:9`! *buffer, int dim, realTcxx . . $ `vendor/qhull/libqhull_r/user_r.h:159:9`! **rows) { |
| 209 | int i, k; |
| 210 | realTcxx . . $ `vendor/qhull/libqhull_r/user_r.h:159:9`! **rowi, *coord, realr; |
| 211 | |
| 212 | coord= buffer; |
| 213 | rowi= rows; |
| 214 | for (i=0; i < dim; i++) { |
| 215 | *(rowi++)= coord; |
| 216 | for (k=0; k < dim; k++) { |
| 217 | realr= qh_RANDOMintcxx . . $ `vendor/qhull/libqhull_r/user_r.h:314:9`!; |
| 218 | *(coord++)= 2.0 * realr/(qh_RANDOMmaxcxx . . $ `vendor/qhull/libqhull_r/user_r.h:313:9`!+1) - 1.0; |
| 219 | } |
| 220 | } |
| 221 | *rowi= coord; |
| 222 | } /* randommatrix */ |
| 223 | |
| 224 | /*-<a href="qh-globa_r.htm#TOC" |
| 225 | >-------------------------------</a><a name="strtol">-</a> |
| 226 | |
| 227 | qh_strtol( s, endp) qh_strtod( s, endp) |
| 228 | internal versions of strtol() and strtod() |
| 229 | does not skip trailing spaces |
| 230 | notes: |
| 231 | some implementations of strtol()/strtod() skip trailing spaces |
| 232 | */ |
| 233 | double qh_strtodcxx . . $ qh_strtod(3dfe5620a0d3160e).(const char *s, char **endp) { |
| 234 | double result; |
| 235 | |
| 236 | result= strtodcxx . . $ strtod(3dfe5620a0d3160e).(s, endp); |
| 237 | if (s < (*endp) && (*endp)[-1] == ' ') |
| 238 | (*endp)--; |
| 239 | return result; |
| 240 | } /* strtod */ |
| 241 | |
| 242 | int qh_strtolcxx . . $ qh_strtol(c4dada3283861623).(const char *s, char **endp) { |
| 243 | int result; |
| 244 | |
| 245 | result= (int) strtolcxx . . $ strtol(44bbb87609b212fc).(s, endp, 10); /* WARN64 */ |
| 246 | if (s< (*endp) && (*endp)[-1] == ' ') |
| 247 | (*endp)--; |
| 248 | return result; |
| 249 | } /* strtol */ |