/[opencvs]/eyes/util.c
ViewVC logotype

Contents of /eyes/util.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Wed Mar 9 09:39:56 2011 UTC (7 years, 9 months ago) by hib
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/plain
eyes - the best fucking mosaic out there
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 /* pulled from player 2/26/2011 */
6
7 char *gets(char *x) {
8 char *y;
9 y=fgets(x,19999,stdin);
10 if (y) {
11 int a=strlen(x);
12 if (a && x[a-1]=='\n') {
13 x[a-1]='\0';
14 }
15 }
16 return y;
17 }
18
19 char *field(char *x, char *y,int fieldnum)
20 {
21 char *z =y;
22 while (fieldnum && *x) {
23 if ((*x == '|')||(*x == ' ')) fieldnum--;
24 x++;
25 }
26 while (*x && (*x != '|') && (*x != ' ')) {
27 *z++ = *x++;
28 }
29 *z='\0';
30 return y;
31 }
32
33
34
35 char *fieldspan(char *x, char *y,int fromfieldnum,int tofieldnum,int withend)
36 {
37 char *z =y;
38 int i=0;
39 while (i<fromfieldnum && *x) {
40 if ((*x == '|')||(*x == ' ')) i++;
41 x++;
42 }
43 while (i<=tofieldnum && *x) {
44 if ((*x == '|')||(*x == ' ')) i++;
45 if (i>tofieldnum&&(!(withend))) break; /* skip the end if requested */
46 *z++ = *x++;
47 }
48 *z='\0';
49 return y;
50 }
51
52 int fieldi(char *x,int fieldnum) {
53 while (fieldnum && *x) {
54 if ((*x == '|')||(*x == ' ')) fieldnum--;
55 x++;
56 }
57 return atoi(x);
58 }
59
60
61
62 char *temp_dir() {
63 char * tmp = getenv("TMP");
64 if (!tmp || (tmp[0]=='\0')) {
65 tmp="/tmp";
66 }
67 return tmp;
68 }
69
70
71
72
73
74
75 int add_fields(char *x, int numfields) {
76 int i;
77 for (i=0;i<numfields;i++) {
78 while ((*x)&&(*x != '|')) {
79 x++;
80 }
81 if (*x == '|') {
82 x++;
83 }
84 else {
85 *x++ = '|';
86 *x = '\0';
87 }
88 }
89 }
90
91 /* this will clip or add extra | as needed
92 better than add_fields */
93 char *set_number_of_fields(char *x,int number_of_fields)
94 {
95 char lastfield='|';
96 char *z=x;
97 char *target = x;
98 int i=0;
99 while (i<number_of_fields && *x) {
100 if ((*x == '|')||(*x == ' ')) {
101 lastfield = *x;
102 target = x;
103 i++;
104 }
105 else {
106 target = x+1;
107 }
108 x++;
109 }
110 while (i<number_of_fields) {
111 target = x;
112 *(x++) = lastfield;
113 i++;
114 }
115 *target = '\0';
116 return z;
117 }
118
119
120
121
122
123
124
125 /* get the countfield at the end of a line:
126 blah|blah|blah|27
127 Makes out into
128 blah|blah|blah
129 and pcount gets 27
130 */
131 int getcount(char *out,char *in,int *pcount) {
132 char *number;
133 char *onumber;
134 number = in;
135 onumber = out;
136 while (*in) {
137 if (*in == '|') {
138 onumber = out;
139 number = in+1;
140 }
141 *out++ = *in++;
142 }
143 *onumber = '\0';
144 *pcount =atoi(number);
145 return(0);
146 }
147
148
149
150 /* get the 2 countfield2 at the end of a line:
151 blah|blah|blah|27|44
152 Makes out into
153 blah|blah|blah
154 and pcount1 gets 27
155 and pcount2 gets 44
156 */
157 int get2counts(char *out,char *in,int *pcount1,int *pcount2,int *pfields) {
158 char *number1;
159 char *onumber1;
160 char *number2;
161 char *onumber2;
162 int fields=0;
163 number1 = NULL;
164 number2 = in;
165 onumber1 =NULL;
166 onumber2 = out;
167 while (*in) {
168 if (*in == '|') {
169 fields++;
170 onumber1 = onumber2;
171 onumber2 = out;
172 number1 = number2;
173 number2 = in+1;
174 }
175 *out++ = *in++;
176 }
177 if (onumber1) {
178 *onumber1 = '\0';
179 if (number1) *pcount1 = atoi(number1);
180 else *pcount1 = atoi(number2);
181 fields--;
182 }
183 fields--;
184 *onumber2 = '\0';
185 *pcount2 =atoi(number2);
186 *pfields = fields;
187 /* add a vertical bar - makes it easier to search right */
188 if (onumber1) {
189 *onumber1++ ='|';
190 *onumber1 = '\0';
191 }
192 return(0);
193 }
194
195
196
197
198
199
200
201 /* make a marker identification string
202
203 Say we have
204 Engler|Hibbard
205 Engler|Micelle
206 Engler|Tim
207 And all are the same length,
208 we would produce:
209 Engler|
210 If markerfields is 1
211
212 Markerfields of 0 returns a null string
213 */
214 int make_marker(char *source,char *dest, int markerfields)
215 {
216 int i=0;
217 while ((i<markerfields)&&(*source)) {
218 if (*source=='|') {
219 i++;
220 if (i==markerfields) break;
221 }
222 *dest++ = *source++;
223 }
224 if (markerfields) *dest++ = '|';
225 *dest = '\0';
226 }
227
228
229
230
231
232
233
234 int date_to_mdy(char *date,int *pmonth,int *pday, int *pyear) {
235 int the_day;
236 char the_month[4];
237 int the_year;
238 int days;
239 int leaps;
240 the_month[3]='\0';
241 int month=0;
242 sscanf(date,"%d-%c%c%c-%d",&the_day,the_month,the_month+1,the_month+2,&the_year);
243
244 if (strcmp(the_month,"Jan")==0)
245 month=1;
246 else if (strcmp(the_month,"Feb")==0)
247 month=2;
248 else if (strcmp(the_month,"Mar")==0)
249 month=3;
250 else if (strcmp(the_month,"Apr")==0)
251 month=4;
252 else if (strcmp(the_month,"May")==0)
253 month=5;
254 else if (strcmp(the_month,"Jun")==0)
255 month=6;
256 else if (strcmp(the_month,"Jul")==0)
257 month=7;
258 else if (strcmp(the_month,"Aug")==0)
259 month=8;
260 else if (strcmp(the_month,"Sep")==0)
261 month=9;
262 else if (strcmp(the_month,"Oct")==0)
263 month=10;
264 else if (strcmp(the_month,"Nov")==0)
265 month=11;
266 else if (strcmp(the_month,"Dec")==0)
267 month=12;
268 *pmonth = month;
269 *pday = the_day;
270 *pyear = the_year;
271 }
272
273
274
275 static int dates[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
276 static int datesl[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
277 static char *months[12] = {"Jan","Feb","Mar","Apr","May","Jun",
278 "Jul","Aug","Sep","Oct","Nov","Dec"};
279
280
281 int date_to_code(char *date) {
282 int the_day;
283 char the_month[4];
284 int the_year;
285 int days;
286 int leaps;
287 the_month[3]='\0';
288 sscanf(date,"%d-%c%c%c-%d",&the_day,the_month,the_month+1,the_month+2,&the_year);
289 days = the_day;
290 int *dt;
291 days = days + 365 * (the_year - 1776);
292 leaps = (the_year - 1777) /4;
293 days += leaps;
294 if ((the_year - 1776) % 4 ==0) {
295 dt = datesl;
296 }
297 else {
298 dt = dates;
299 }
300 int i;
301 i=0;
302 if (strcmp(the_month,"Jan")) {
303 days += dt[i++];
304 if (strcmp(the_month,"Feb")) {
305 days += dt[i++];
306 if (strcmp(the_month,"Mar")) {
307 days += dt[i++];
308 if (strcmp(the_month,"Apr")) {
309 days += dt[i++];
310 if (strcmp(the_month,"May")) {
311 days += dt[i++];
312 if (strcmp(the_month,"Jun")) {
313 days += dt[i++];
314 if (strcmp(the_month,"Jul")) {
315 days += dt[i++];
316 if (strcmp(the_month,"Aug")) {
317 days += dt[i++];
318 if (strcmp(the_month,"Sep")) {
319 days += dt[i++];
320 if (strcmp(the_month,"Oct")) {
321 days += dt[i++];
322 if (strcmp(the_month,"Nov")) {
323 days += dt[i++];
324 if (strcmp(the_month,"Dec")) {
325 fprintf(stderr,"Error month of date %s\n",date);
326 exit(-1);
327 }
328 }
329 }
330 }
331 }
332 }
333 }
334 }
335 }
336 }
337 }
338 }
339
340 return days;
341
342 }
343
344
345 char * code_to_date(int code,char*date) {
346 int days;
347 int years4;
348 int years;
349 int *dt;
350 days = code;
351 years4 = days / (365 + 365 + 365 + 366);
352 days = days % (365 + 365 + 365 + 366);
353 if (days <366) {
354 years = years4*4 + days / 366;
355 days = days % 366;
356 dt = datesl;
357 }
358 else {
359 years = years4*4 + days / 365;
360 days = days % 365;
361 dt = dates;
362 }
363 int i=0;
364 while (days > dt[i]) {
365 days -= dt[i];
366 i++;
367 }
368 years += 1776;
369 days++;
370 sprintf(date,"%d-%s-%d",days,months[i],years);
371 return(date);
372 }
373
374
375
376
377
378
379 char * initcap(char *dest,char *src) {
380 char ch;
381 if (ch = *src) {
382 if (ch >= 'a' && ch <='z') {
383 ch = ch + 'A' - 'a';
384 }
385 *dest++ = ch;
386 src++;
387 }
388 while (ch= *src) {
389 if (ch >= 'A' && ch <='Z') {
390 ch = ch - 'A' + 'a';
391 }
392 *dest++ = ch;
393 src++;
394 }
395 *dest = '\0';
396 return dest;
397 }
398
399 char * lower(char *dest,char *src) {
400 char ch;
401 while (ch= *src) {
402 if (ch >= 'A' && ch <='Z') {
403 ch = ch - 'A' + 'a';
404 }
405 *dest++ = ch;
406 src++;
407 }
408 *dest = '\0';
409 return dest;
410 }
411
412
413 char * upper(char *dest,char *src) {
414 char ch;
415 while (ch= *src) {
416 if (ch >= 'a' && ch <='z') {
417 ch = ch + 'A' - 'a';
418 }
419 *dest++ = ch;
420 src++;
421 }
422 *dest = '\0';
423 return dest;
424 }
425
426
427
428
429 char *decode_url(char *out,char *in,int max) {
430 int i=0;
431 char *x=in;
432 char *z=out;
433 while ((*x)&&(i<max)) {
434 if ((x[0] == '%') && (((x[1]>='0')&&(x[1]<='9'))||((x[1]>='a')&&(x[1]<='f'))||
435 ((x[1]>='A')&&(x[1]<='F')))
436 && (((x[2]>='0')&&(x[2]<='9'))||((x[2]>='a')&&(x[2]<='f'))||
437 ((x[2]>='A')&&(x[2]<='F'))) ) {
438 char buf[3];
439 buf[0]=x[1];
440 buf[1]=x[2];
441 buf[2]='\0';
442 unsigned int a;
443 sscanf(buf,"%x",&a);
444 *z = a;
445 z++;
446 x+=3;
447 i+=3;
448 }
449 else {
450 *(z++) = *(x++);
451 i++;
452 }
453 }
454 *z = '\0';
455 return out;
456 }
457
458
459
460
461 char *encode_url(char *out,char *in,int max) {
462 int i=0;
463 char *x=in;
464 char *z=out;
465 while ((*x)&&(i<max)) {
466 if (index("/$-_.+!*'(),abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",x[0])) {
467 *z++ = *x++;
468 i++;
469 }
470 else {
471 *z++='%';
472 sprintf(z,"%02x",*x);
473 x++;
474 z+=2;
475 i++;
476 }
477 }
478 *z = '\0';
479 return out;
480 }
481
482
483 char *encode_html(char *out,char *in, int max) {
484 int i=0;
485 char *x=in;
486 char *z=out;
487 while ((*x)&&(i<max)) {
488 if (*x == '&') {
489 strcpy(z,"&amp;");
490 z += 5;
491 }
492 else if (*x == '<') {
493 strcpy(z,"&lt;");
494 z += 4;
495 }
496 else if (*x == '>') {
497 strcpy(z,"&gt;");
498 z += 4;
499 }
500 else {
501 *z++ = *x;
502 }
503 i++;
504 x++;
505 }
506 *z='\0';
507 return out;
508 }

  ViewVC Help
Powered by ViewVC 1.1.5