Reverted unnecessary string changes
[ntk/apt.git] / apt-pkg / contrib / strutl.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: strutl.cc,v 1.41 2001/05/27 23:30:45 jgg Exp $
4 /* ######################################################################
5
6 String Util - Some useful string functions.
7
8 These have been collected from here and there to do all sorts of useful
9 things to strings. They are useful in file parsers, URI handlers and
10 especially in APT methods.
11
12 This source is placed in the Public Domain, do with it what you will
13 It was originally written by Jason Gunthorpe <jgg@gpu.srv.ualberta.ca>
14
15 ##################################################################### */
16 /*}}}*/
17 // Includes /*{{{*/
18 #ifdef __GNUG__
19 #pragma implementation "apt-pkg/strutl.h"
20 #endif
21
22 #include <apt-pkg/strutl.h>
23 #include <apt-pkg/fileutl.h>
24 #include <apt-pkg/error.h>
25
26 #include <apti18n.h>
27
28 #include <ctype.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <regex.h>
33 #include <errno.h>
34 #include <stdarg.h>
35
36 using namespace std;
37 /*}}}*/
38
39 // strstrip - Remove white space from the front and back of a string /*{{{*/
40 // ---------------------------------------------------------------------
41 /* This is handy to use when parsing a file. It also removes \n's left
42 over from fgets and company */
43 char *_strstrip(char *String)
44 {
45 for (;*String != 0 && (*String == ' ' || *String == '\t'); String++);
46
47 if (*String == 0)
48 return String;
49
50 char *End = String + strlen(String) - 1;
51 for (;End != String - 1 && (*End == ' ' || *End == '\t' || *End == '\n' ||
52 *End == '\r'); End--);
53 End++;
54 *End = 0;
55 return String;
56 };
57 /*}}}*/
58 // strtabexpand - Converts tabs into 8 spaces /*{{{*/
59 // ---------------------------------------------------------------------
60 /* */
61 char *_strtabexpand(char *String,size_t Len)
62 {
63 for (char *I = String; I != I + Len && *I != 0; I++)
64 {
65 if (*I != '\t')
66 continue;
67 if (I + 8 > String + Len)
68 {
69 *I = 0;
70 return String;
71 }
72
73 /* Assume the start of the string is 0 and find the next 8 char
74 division */
75 int Len;
76 if (String == I)
77 Len = 1;
78 else
79 Len = 8 - ((String - I) % 8);
80 Len -= 2;
81 if (Len <= 0)
82 {
83 *I = ' ';
84 continue;
85 }
86
87 memmove(I + Len,I + 1,strlen(I) + 1);
88 for (char *J = I; J + Len != I; *I = ' ', I++);
89 }
90 return String;
91 }
92 /*}}}*/
93 // ParseQuoteWord - Parse a single word out of a string /*{{{*/
94 // ---------------------------------------------------------------------
95 /* This grabs a single word, converts any % escaped characters to their
96 proper values and advances the pointer. Double quotes are understood
97 and striped out as well. This is for URI/URL parsing. It also can
98 understand [] brackets.*/
99 bool ParseQuoteWord(const char *&String,string &Res)
100 {
101 // Skip leading whitespace
102 const char *C = String;
103 for (;*C != 0 && *C == ' '; C++);
104 if (*C == 0)
105 return false;
106
107 // Jump to the next word
108 for (;*C != 0 && isspace(*C) == 0; C++)
109 {
110 if (*C == '"')
111 {
112 for (C++; *C != 0 && *C != '"'; C++);
113 if (*C == 0)
114 return false;
115 }
116 if (*C == '[')
117 {
118 for (C++; *C != 0 && *C != ']'; C++);
119 if (*C == 0)
120 return false;
121 }
122 }
123
124 // Now de-quote characters
125 char Buffer[1024];
126 char Tmp[3];
127 const char *Start = String;
128 char *I;
129 for (I = Buffer; I < Buffer + sizeof(Buffer) && Start != C; I++)
130 {
131 if (*Start == '%' && Start + 2 < C)
132 {
133 Tmp[0] = Start[1];
134 Tmp[1] = Start[2];
135 Tmp[2] = 0;
136 *I = (char)strtol(Tmp,0,16);
137 Start += 3;
138 continue;
139 }
140 if (*Start != '"')
141 *I = *Start;
142 else
143 I--;
144 Start++;
145 }
146 *I = 0;
147 Res = Buffer;
148
149 // Skip ending white space
150 for (;*C != 0 && isspace(*C) != 0; C++);
151 String = C;
152 return true;
153 }
154 /*}}}*/
155 // ParseCWord - Parses a string like a C "" expression /*{{{*/
156 // ---------------------------------------------------------------------
157 /* This expects a series of space separated strings enclosed in ""'s.
158 It concatenates the ""'s into a single string. */
159 bool ParseCWord(const char *&String,string &Res)
160 {
161 // Skip leading whitespace
162 const char *C = String;
163 for (;*C != 0 && *C == ' '; C++);
164 if (*C == 0)
165 return false;
166
167 char Buffer[1024];
168 char *Buf = Buffer;
169 if (strlen(String) >= sizeof(Buffer))
170 return false;
171
172 for (; *C != 0; C++)
173 {
174 if (*C == '"')
175 {
176 for (C++; *C != 0 && *C != '"'; C++)
177 *Buf++ = *C;
178
179 if (*C == 0)
180 return false;
181
182 continue;
183 }
184
185 if (C != String && isspace(*C) != 0 && isspace(C[-1]) != 0)
186 continue;
187 if (isspace(*C) == 0)
188 return false;
189 *Buf++ = ' ';
190 }
191 *Buf = 0;
192 Res = Buffer;
193 String = C;
194 return true;
195 }
196 /*}}}*/
197 // QuoteString - Convert a string into quoted from /*{{{*/
198 // ---------------------------------------------------------------------
199 /* */
200 string QuoteString(string Str,const char *Bad)
201 {
202 string Res;
203 for (string::iterator I = Str.begin(); I != Str.end(); I++)
204 {
205 if (strchr(Bad,*I) != 0 || isprint(*I) == 0 ||
206 *I <= 0x20 || *I >= 0x7F)
207 {
208 char Buf[10];
209 sprintf(Buf,"%%%02x",(int)*I);
210 Res += Buf;
211 }
212 else
213 Res += *I;
214 }
215 return Res;
216 }
217 /*}}}*/
218 // DeQuoteString - Convert a string from quoted from /*{{{*/
219 // ---------------------------------------------------------------------
220 /* This undoes QuoteString */
221 string DeQuoteString(string Str)
222 {
223 string Res;
224 for (string::const_iterator I = Str.begin(); I != Str.end(); I++)
225 {
226 if (*I == '%' && I + 2 < Str.end())
227 {
228 char Tmp[3];
229 Tmp[0] = I[1];
230 Tmp[1] = I[2];
231 Tmp[2] = 0;
232 Res += (char)strtol(Tmp,0,16);
233 I += 2;
234 continue;
235 }
236 else
237 Res += *I;
238 }
239 return Res;
240 }
241
242 /*}}}*/
243 // SizeToStr - Convert a long into a human readable size /*{{{*/
244 // ---------------------------------------------------------------------
245 /* A max of 4 digits are shown before conversion to the next highest unit.
246 The max length of the string will be 5 chars unless the size is > 10
247 YottaBytes (E24) */
248 string SizeToStr(double Size)
249 {
250 char S[300];
251 double ASize;
252 if (Size >= 0)
253 ASize = Size;
254 else
255 ASize = -1*Size;
256
257 /* bytes, KiloBytes, MegaBytes, GigaBytes, TeraBytes, PetaBytes,
258 ExaBytes, ZettaBytes, YottaBytes */
259 char Ext[] = {'\0','k','M','G','T','P','E','Z','Y'};
260 int I = 0;
261 while (I <= 8)
262 {
263 if (ASize < 100 && I != 0)
264 {
265 sprintf(S,"%.1f%c",ASize,Ext[I]);
266 break;
267 }
268
269 if (ASize < 10000)
270 {
271 sprintf(S,"%.0f%c",ASize,Ext[I]);
272 break;
273 }
274 ASize /= 1000.0;
275 I++;
276 }
277
278 return S;
279 }
280 /*}}}*/
281 // TimeToStr - Convert the time into a string /*{{{*/
282 // ---------------------------------------------------------------------
283 /* Converts a number of seconds to a hms format */
284 string TimeToStr(unsigned long Sec)
285 {
286 char S[300];
287
288 while (1)
289 {
290 if (Sec > 60*60*24)
291 {
292 sprintf(S,"%lid %lih%lim%lis",Sec/60/60/24,(Sec/60/60) % 24,(Sec/60) % 60,Sec % 60);
293 break;
294 }
295
296 if (Sec > 60*60)
297 {
298 sprintf(S,"%lih%lim%lis",Sec/60/60,(Sec/60) % 60,Sec % 60);
299 break;
300 }
301
302 if (Sec > 60)
303 {
304 sprintf(S,"%lim%lis",Sec/60,Sec % 60);
305 break;
306 }
307
308 sprintf(S,"%lis",Sec);
309 break;
310 }
311
312 return S;
313 }
314 /*}}}*/
315 // SubstVar - Substitute a string for another string /*{{{*/
316 // ---------------------------------------------------------------------
317 /* This replaces all occurances of Subst with Contents in Str. */
318 string SubstVar(string Str,string Subst,string Contents)
319 {
320 string::size_type Pos = 0;
321 string::size_type OldPos = 0;
322 string Temp;
323
324 while (OldPos < Str.length() &&
325 (Pos = Str.find(Subst,OldPos)) != string::npos)
326 {
327 Temp += string(Str,OldPos,Pos) + Contents;
328 OldPos = Pos + Subst.length();
329 }
330
331 if (OldPos == 0)
332 return Str;
333
334 return Temp + string(Str,OldPos);
335 }
336
337 string SubstVar(string Str,const struct SubstVar *Vars)
338 {
339 for (; Vars->Subst != 0; Vars++)
340 Str = SubstVar(Str,Vars->Subst,*Vars->Contents);
341 return Str;
342 }
343 /*}}}*/
344 // URItoFileName - Convert the uri into a unique file name /*{{{*/
345 // ---------------------------------------------------------------------
346 /* This converts a URI into a safe filename. It quotes all unsafe characters
347 and converts / to _ and removes the scheme identifier. The resulting
348 file name should be unique and never occur again for a different file */
349 string URItoFileName(string URI)
350 {
351 // Nuke 'sensitive' items
352 ::URI U(URI);
353 U.User = string();
354 U.Password = string();
355 U.Access = "";
356
357 // "\x00-\x20{}|\\\\^\\[\\]<>\"\x7F-\xFF";
358 URI = QuoteString(U,"\\|{}[]<>\"^~_=!@#$%^&*");
359 string::iterator J = URI.begin();
360 for (; J != URI.end(); J++)
361 if (*J == '/')
362 *J = '_';
363 return URI;
364 }
365 /*}}}*/
366 // Base64Encode - Base64 Encoding routine for short strings /*{{{*/
367 // ---------------------------------------------------------------------
368 /* This routine performs a base64 transformation on a string. It was ripped
369 from wget and then patched and bug fixed.
370
371 This spec can be found in rfc2045 */
372 string Base64Encode(string S)
373 {
374 // Conversion table.
375 static char tbl[64] = {'A','B','C','D','E','F','G','H',
376 'I','J','K','L','M','N','O','P',
377 'Q','R','S','T','U','V','W','X',
378 'Y','Z','a','b','c','d','e','f',
379 'g','h','i','j','k','l','m','n',
380 'o','p','q','r','s','t','u','v',
381 'w','x','y','z','0','1','2','3',
382 '4','5','6','7','8','9','+','/'};
383
384 // Pre-allocate some space
385 string Final;
386 Final.reserve((4*S.length() + 2)/3 + 2);
387
388 /* Transform the 3x8 bits to 4x6 bits, as required by
389 base64. */
390 for (string::const_iterator I = S.begin(); I < S.end(); I += 3)
391 {
392 char Bits[3] = {0,0,0};
393 Bits[0] = I[0];
394 if (I + 1 < S.end())
395 Bits[1] = I[1];
396 if (I + 2 < S.end())
397 Bits[2] = I[2];
398
399 Final += tbl[Bits[0] >> 2];
400 Final += tbl[((Bits[0] & 3) << 4) + (Bits[1] >> 4)];
401
402 if (I + 1 >= S.end())
403 break;
404
405 Final += tbl[((Bits[1] & 0xf) << 2) + (Bits[2] >> 6)];
406
407 if (I + 2 >= S.end())
408 break;
409
410 Final += tbl[Bits[2] & 0x3f];
411 }
412
413 /* Apply the padding elements, this tells how many bytes the remote
414 end should discard */
415 if (S.length() % 3 == 2)
416 Final += '=';
417 if (S.length() % 3 == 1)
418 Final += "==";
419
420 return Final;
421 }
422 /*}}}*/
423 // stringcmp - Arbitary string compare /*{{{*/
424 // ---------------------------------------------------------------------
425 /* This safely compares two non-null terminated strings of arbitary
426 length */
427 int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd)
428 {
429 for (; A != AEnd && B != BEnd; A++, B++)
430 if (*A != *B)
431 break;
432
433 if (A == AEnd && B == BEnd)
434 return 0;
435 if (A == AEnd)
436 return 1;
437 if (B == BEnd)
438 return -1;
439 if (*A < *B)
440 return -1;
441 return 1;
442 }
443 /*}}}*/
444 // stringcasecmp - Arbitary case insensitive string compare /*{{{*/
445 // ---------------------------------------------------------------------
446 /* */
447 int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd)
448 {
449 for (; A != AEnd && B != BEnd; A++, B++)
450 if (toupper(*A) != toupper(*B))
451 break;
452
453 if (A == AEnd && B == BEnd)
454 return 0;
455 if (A == AEnd)
456 return 1;
457 if (B == BEnd)
458 return -1;
459 if (toupper(*A) < toupper(*B))
460 return -1;
461 return 1;
462 }
463 /*}}}*/
464 // LookupTag - Lookup the value of a tag in a taged string /*{{{*/
465 // ---------------------------------------------------------------------
466 /* The format is like those used in package files and the method
467 communication system */
468 string LookupTag(string Message,const char *Tag,const char *Default)
469 {
470 // Look for a matching tag.
471 int Length = strlen(Tag);
472 for (const char *I = Message.c_str(); I + Length < Message.c_str() + Message.length(); I++)
473 {
474 // Found the tag
475 if (I[Length] == ':' && stringcasecmp(I,I+Length,Tag) == 0)
476 {
477 // Find the end of line and strip the leading/trailing spaces
478 const char *J;
479 I += Length + 1;
480 for (; isspace(*I) != 0 && *I != 0; I++);
481 for (J = I; *J != '\n' && *J != 0; J++);
482 for (; J > I && isspace(J[-1]) != 0; J--);
483
484 return string(I,J);
485 }
486
487 for (; *I != '\n' && *I != 0; I++);
488 }
489
490 // Failed to find a match
491 if (Default == 0)
492 return string();
493 return Default;
494 }
495 /*}}}*/
496 // StringToBool - Converts a string into a boolean /*{{{*/
497 // ---------------------------------------------------------------------
498 /* This inspects the string to see if it is true or if it is false and
499 then returns the result. Several varients on true/false are checked. */
500 int StringToBool(string Text,int Default)
501 {
502 char *End;
503 int Res = strtol(Text.c_str(),&End,0);
504 if (End != Text.c_str() && Res >= 0 && Res <= 1)
505 return Res;
506
507 // Check for positives
508 if (strcasecmp(Text.c_str(),"no") == 0 ||
509 strcasecmp(Text.c_str(),"false") == 0 ||
510 strcasecmp(Text.c_str(),"without") == 0 ||
511 strcasecmp(Text.c_str(),"off") == 0 ||
512 strcasecmp(Text.c_str(),"disable") == 0)
513 return 0;
514
515 // Check for negatives
516 if (strcasecmp(Text.c_str(),"yes") == 0 ||
517 strcasecmp(Text.c_str(),"true") == 0 ||
518 strcasecmp(Text.c_str(),"with") == 0 ||
519 strcasecmp(Text.c_str(),"on") == 0 ||
520 strcasecmp(Text.c_str(),"enable") == 0)
521 return 1;
522
523 return Default;
524 }
525 /*}}}*/
526 // TimeRFC1123 - Convert a time_t into RFC1123 format /*{{{*/
527 // ---------------------------------------------------------------------
528 /* This converts a time_t into a string time representation that is
529 year 2000 complient and timezone neutral */
530 string TimeRFC1123(time_t Date)
531 {
532 struct tm Conv = *gmtime(&Date);
533 char Buf[300];
534
535 const char *Day[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
536 const char *Month[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul",
537 "Aug","Sep","Oct","Nov","Dec"};
538
539 sprintf(Buf,"%s, %02i %s %i %02i:%02i:%02i GMT",Day[Conv.tm_wday],
540 Conv.tm_mday,Month[Conv.tm_mon],Conv.tm_year+1900,Conv.tm_hour,
541 Conv.tm_min,Conv.tm_sec);
542 return Buf;
543 }
544 /*}}}*/
545 // ReadMessages - Read messages from the FD /*{{{*/
546 // ---------------------------------------------------------------------
547 /* This pulls full messages from the input FD into the message buffer.
548 It assumes that messages will not pause during transit so no
549 fancy buffering is used. */
550 bool ReadMessages(int Fd, vector<string> &List)
551 {
552 char Buffer[4000];
553 char *End = Buffer;
554
555 while (1)
556 {
557 int Res = read(Fd,End,sizeof(Buffer) - (End-Buffer));
558 if (Res < 0 && errno == EINTR)
559 continue;
560
561 // Process is dead, this is kind of bad..
562 if (Res == 0)
563 return false;
564
565 // No data
566 if (Res < 0 && errno == EAGAIN)
567 return true;
568 if (Res < 0)
569 return false;
570
571 End += Res;
572
573 // Look for the end of the message
574 for (char *I = Buffer; I + 1 < End; I++)
575 {
576 if (I[0] != '\n' || I[1] != '\n')
577 continue;
578
579 // Pull the message out
580 string Message(Buffer,0,I-Buffer);
581
582 // Fix up the buffer
583 for (; I < End && *I == '\n'; I++);
584 End -= I-Buffer;
585 memmove(Buffer,I,End-Buffer);
586 I = Buffer;
587
588 List.push_back(Message);
589 }
590 if (End == Buffer)
591 return true;
592
593 if (WaitFd(Fd) == false)
594 return false;
595 }
596 }
597 /*}}}*/
598 // MonthConv - Converts a month string into a number /*{{{*/
599 // ---------------------------------------------------------------------
600 /* This was lifted from the boa webserver which lifted it from 'wn-v1.07'
601 Made it a bit more robust with a few touppers though. */
602 static int MonthConv(char *Month)
603 {
604 switch (toupper(*Month))
605 {
606 case 'A':
607 return toupper(Month[1]) == 'P'?3:7;
608 case 'D':
609 return 11;
610 case 'F':
611 return 1;
612 case 'J':
613 if (toupper(Month[1]) == 'A')
614 return 0;
615 return toupper(Month[2]) == 'N'?5:6;
616 case 'M':
617 return toupper(Month[2]) == 'R'?2:4;
618 case 'N':
619 return 10;
620 case 'O':
621 return 9;
622 case 'S':
623 return 8;
624
625 // Pretend it is January..
626 default:
627 return 0;
628 }
629 }
630 /*}}}*/
631 // timegm - Internal timegm function if gnu is not available /*{{{*/
632 // ---------------------------------------------------------------------
633 /* Ripped this evil little function from wget - I prefer the use of
634 GNU timegm if possible as this technique will have interesting problems
635 with leap seconds, timezones and other.
636
637 Converts struct tm to time_t, assuming the data in tm is UTC rather
638 than local timezone (mktime assumes the latter).
639
640 Contributed by Roger Beeman <beeman@cisco.com>, with the help of
641 Mark Baushke <mdb@cisco.com> and the rest of the Gurus at CISCO. */
642 #ifndef __USE_MISC // glib sets this
643 static time_t timegm(struct tm *t)
644 {
645 time_t tl, tb;
646
647 tl = mktime (t);
648 if (tl == -1)
649 return -1;
650 tb = mktime (gmtime (&tl));
651 return (tl <= tb ? (tl + (tl - tb)) : (tl - (tb - tl)));
652 }
653 #endif
654 /*}}}*/
655 // StrToTime - Converts a string into a time_t /*{{{*/
656 // ---------------------------------------------------------------------
657 /* This handles all 3 populare time formats including RFC 1123, RFC 1036
658 and the C library asctime format. It requires the GNU library function
659 'timegm' to convert a struct tm in UTC to a time_t. For some bizzar
660 reason the C library does not provide any such function :< This also
661 handles the weird, but unambiguous FTP time format*/
662 bool StrToTime(string Val,time_t &Result)
663 {
664 struct tm Tm;
665 char Month[10];
666 const char *I = Val.c_str();
667
668 // Skip the day of the week
669 for (;*I != 0 && *I != ' '; I++);
670
671 // Handle RFC 1123 time
672 Month[0] = 0;
673 if (sscanf(I," %d %3s %d %d:%d:%d GMT",&Tm.tm_mday,Month,&Tm.tm_year,
674 &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) != 6)
675 {
676 // Handle RFC 1036 time
677 if (sscanf(I," %d-%3s-%d %d:%d:%d GMT",&Tm.tm_mday,Month,
678 &Tm.tm_year,&Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) == 6)
679 Tm.tm_year += 1900;
680 else
681 {
682 // asctime format
683 if (sscanf(I," %3s %d %d:%d:%d %d",Month,&Tm.tm_mday,
684 &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec,&Tm.tm_year) != 6)
685 {
686 // 'ftp' time
687 if (sscanf(Val.c_str(),"%4d%2d%2d%2d%2d%2d",&Tm.tm_year,&Tm.tm_mon,
688 &Tm.tm_mday,&Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) != 6)
689 return false;
690 Tm.tm_mon--;
691 }
692 }
693 }
694
695 Tm.tm_isdst = 0;
696 if (Month[0] != 0)
697 Tm.tm_mon = MonthConv(Month);
698 Tm.tm_year -= 1900;
699
700 // Convert to local time and then to GMT
701 Result = timegm(&Tm);
702 return true;
703 }
704 /*}}}*/
705 // StrToNum - Convert a fixed length string to a number /*{{{*/
706 // ---------------------------------------------------------------------
707 /* This is used in decoding the crazy fixed length string headers in
708 tar and ar files. */
709 bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base)
710 {
711 char S[30];
712 if (Len >= sizeof(S))
713 return false;
714 memcpy(S,Str,Len);
715 S[Len] = 0;
716
717 // All spaces is a zero
718 Res = 0;
719 unsigned I;
720 for (I = 0; S[I] == ' '; I++);
721 if (S[I] == 0)
722 return true;
723
724 char *End;
725 Res = strtoul(S,&End,Base);
726 if (End == S)
727 return false;
728
729 return true;
730 }
731 /*}}}*/
732 // HexDigit - Convert a hex character into an integer /*{{{*/
733 // ---------------------------------------------------------------------
734 /* Helper for Hex2Num */
735 static int HexDigit(int c)
736 {
737 if (c >= '0' && c <= '9')
738 return c - '0';
739 if (c >= 'a' && c <= 'f')
740 return c - 'a' + 10;
741 if (c >= 'A' && c <= 'F')
742 return c - 'A' + 10;
743 return 0;
744 }
745 /*}}}*/
746 // Hex2Num - Convert a long hex number into a buffer /*{{{*/
747 // ---------------------------------------------------------------------
748 /* The length of the buffer must be exactly 1/2 the length of the string. */
749 bool Hex2Num(string Str,unsigned char *Num,unsigned int Length)
750 {
751 if (Str.length() != Length*2)
752 return false;
753
754 // Convert each digit. We store it in the same order as the string
755 int J = 0;
756 for (string::const_iterator I = Str.begin(); I != Str.end();J++, I += 2)
757 {
758 if (isxdigit(*I) == 0 || isxdigit(I[1]) == 0)
759 return false;
760
761 Num[J] = HexDigit(I[0]) << 4;
762 Num[J] += HexDigit(I[1]);
763 }
764
765 return true;
766 }
767 /*}}}*/
768 // TokSplitString - Split a string up by a given token /*{{{*/
769 // ---------------------------------------------------------------------
770 /* This is intended to be a faster splitter, it does not use dynamic
771 memories. Input is changed to insert nulls at each token location. */
772 bool TokSplitString(char Tok,char *Input,char **List,
773 unsigned long ListMax)
774 {
775 // Strip any leading spaces
776 char *Start = Input;
777 char *Stop = Start + strlen(Start);
778 for (; *Start != 0 && isspace(*Start) != 0; Start++);
779
780 unsigned long Count = 0;
781 char *Pos = Start;
782 while (Pos != Stop)
783 {
784 // Skip to the next Token
785 for (; Pos != Stop && *Pos != Tok; Pos++);
786
787 // Back remove spaces
788 char *End = Pos;
789 for (; End > Start && (End[-1] == Tok || isspace(End[-1]) != 0); End--);
790 *End = 0;
791
792 List[Count++] = Start;
793 if (Count >= ListMax)
794 {
795 List[Count-1] = 0;
796 return false;
797 }
798
799 // Advance pos
800 for (; Pos != Stop && (*Pos == Tok || isspace(*Pos) != 0 || *Pos == 0); Pos++);
801 Start = Pos;
802 }
803
804 List[Count] = 0;
805 return true;
806 }
807 /*}}}*/
808 // RegexChoice - Simple regex list/list matcher /*{{{*/
809 // ---------------------------------------------------------------------
810 /* */
811 unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin,
812 const char **ListEnd)
813 {
814 for (RxChoiceList *R = Rxs; R->Str != 0; R++)
815 R->Hit = false;
816
817 unsigned long Hits = 0;
818 for (; ListBegin != ListEnd; ListBegin++)
819 {
820 // Check if the name is a regex
821 const char *I;
822 bool Regex = true;
823 for (I = *ListBegin; *I != 0; I++)
824 if (*I == '.' || *I == '?' || *I == '*' || *I == '|')
825 break;
826 if (*I == 0)
827 Regex = false;
828
829 // Compile the regex pattern
830 regex_t Pattern;
831 if (Regex == true)
832 if (regcomp(&Pattern,*ListBegin,REG_EXTENDED | REG_ICASE |
833 REG_NOSUB) != 0)
834 Regex = false;
835
836 // Search the list
837 bool Done = false;
838 for (RxChoiceList *R = Rxs; R->Str != 0; R++)
839 {
840 if (R->Str[0] == 0)
841 continue;
842
843 if (strcasecmp(R->Str,*ListBegin) != 0)
844 {
845 if (Regex == false)
846 continue;
847 if (regexec(&Pattern,R->Str,0,0,0) != 0)
848 continue;
849 }
850 Done = true;
851
852 if (R->Hit == false)
853 Hits++;
854
855 R->Hit = true;
856 }
857
858 if (Regex == true)
859 regfree(&Pattern);
860
861 if (Done == false)
862 _error->Warning(_("Selection %s not found"),*ListBegin);
863 }
864
865 return Hits;
866 }
867 /*}}}*/
868 // ioprintf - C format string outputter to C++ iostreams /*{{{*/
869 // ---------------------------------------------------------------------
870 /* This is used to make the internationalization strinc easier to translate
871 and to allow reordering of parameters */
872 void ioprintf(ostream &out,const char *format,...)
873 {
874 va_list args;
875 va_start(args,format);
876
877 // sprintf the description
878 char S[400];
879 vsnprintf(S,sizeof(S),format,args);
880 out << S;
881 }
882 /*}}}*/
883
884 // CheckDomainList - See if Host is in a , seperate list /*{{{*/
885 // ---------------------------------------------------------------------
886 /* The domain list is a comma seperate list of domains that are suffix
887 matched against the argument */
888 bool CheckDomainList(string Host,string List)
889 {
890 const char *Start = List.c_str();
891 for (const char *Cur = List.c_str(); *Cur != 0; Cur++)
892 {
893 if (*Cur != ',')
894 continue;
895
896 // Match the end of the string..
897 if ((Host.size() >= (unsigned)(Cur - List.c_str())) &&
898 Cur - Start != 0 &&
899 stringcasecmp(Host.c_str() + Host.length() - (Cur - Start),Host.c_str()+Host.length(),Start,Cur) == 0)
900 return true;
901
902 Start = Cur + 1;
903 }
904 return false;
905 }
906 /*}}}*/
907
908 // URI::CopyFrom - Copy from an object /*{{{*/
909 // ---------------------------------------------------------------------
910 /* This parses the URI into all of its components */
911 void URI::CopyFrom(string U)
912 {
913 string::const_iterator I = U.begin();
914
915 // Locate the first colon, this separates the scheme
916 for (; I < U.end() && *I != ':' ; I++);
917 string::const_iterator FirstColon = I;
918
919 /* Determine if this is a host type URI with a leading double //
920 and then search for the first single / */
921 string::const_iterator SingleSlash = I;
922 if (I + 3 < U.end() && I[1] == '/' && I[2] == '/')
923 SingleSlash += 3;
924
925 /* Find the / indicating the end of the hostname, ignoring /'s in the
926 square brackets */
927 bool InBracket = false;
928 for (; SingleSlash < U.end() && (*SingleSlash != '/' || InBracket == true); SingleSlash++)
929 {
930 if (*SingleSlash == '[')
931 InBracket = true;
932 if (InBracket == true && *SingleSlash == ']')
933 InBracket = false;
934 }
935
936 if (SingleSlash > U.end())
937 SingleSlash = U.end();
938
939 // We can now write the access and path specifiers
940 Access = string(U,0,FirstColon - U.begin());
941 if (SingleSlash != U.end())
942 Path = string(U,SingleSlash - U.begin());
943 if (Path.empty() == true)
944 Path = "/";
945
946 // Now we attempt to locate a user:pass@host fragment
947 if (FirstColon[1] == '/' && FirstColon[2] == '/')
948 FirstColon += 3;
949 else
950 FirstColon += 1;
951 if (FirstColon >= U.end())
952 return;
953
954 if (FirstColon > SingleSlash)
955 FirstColon = SingleSlash;
956
957 // Find the colon...
958 I = FirstColon + 1;
959 if (I > SingleSlash)
960 I = SingleSlash;
961 for (; I < SingleSlash && *I != ':'; I++);
962 string::const_iterator SecondColon = I;
963
964 // Search for the @ after the colon
965 for (; I < SingleSlash && *I != '@'; I++);
966 string::const_iterator At = I;
967
968 // Now write the host and user/pass
969 if (At == SingleSlash)
970 {
971 if (FirstColon < SingleSlash)
972 Host = string(U,FirstColon - U.begin(),SingleSlash - FirstColon);
973 }
974 else
975 {
976 Host = string(U,At - U.begin() + 1,SingleSlash - At - 1);
977 User = string(U,FirstColon - U.begin(),SecondColon - FirstColon);
978 if (SecondColon < At)
979 Password = string(U,SecondColon - U.begin() + 1,At - SecondColon - 1);
980 }
981
982 // Now we parse the RFC 2732 [] hostnames.
983 unsigned long PortEnd = 0;
984 InBracket = false;
985 for (unsigned I = 0; I != Host.length();)
986 {
987 if (Host[I] == '[')
988 {
989 InBracket = true;
990 Host.erase(I,1);
991 continue;
992 }
993
994 if (InBracket == true && Host[I] == ']')
995 {
996 InBracket = false;
997 Host.erase(I,1);
998 PortEnd = I;
999 continue;
1000 }
1001 I++;
1002 }
1003
1004 // Tsk, weird.
1005 if (InBracket == true)
1006 {
1007 Host = string();
1008 return;
1009 }
1010
1011 // Now we parse off a port number from the hostname
1012 Port = 0;
1013 string::size_type Pos = Host.rfind(':');
1014 if (Pos == string::npos || Pos < PortEnd)
1015 return;
1016
1017 Port = atoi(string(Host,Pos+1).c_str());
1018 Host = string(Host,0,Pos);
1019 }
1020 /*}}}*/
1021 // URI::operator string - Convert the URI to a string /*{{{*/
1022 // ---------------------------------------------------------------------
1023 /* */
1024 URI::operator string()
1025 {
1026 string Res;
1027
1028 if (Access.empty() == false)
1029 Res = Access + ':';
1030
1031 if (Host.empty() == false)
1032 {
1033 if (Access.empty() == false)
1034 Res += "//";
1035
1036 if (User.empty() == false)
1037 {
1038 Res += User;
1039 if (Password.empty() == false)
1040 Res += ":" + Password;
1041 Res += "@";
1042 }
1043
1044 // Add RFC 2732 escaping characters
1045 if (Access.empty() == false &&
1046 (Host.find('/') != string::npos || Host.find(':') != string::npos))
1047 Res += '[' + Host + ']';
1048 else
1049 Res += Host;
1050
1051 if (Port != 0)
1052 {
1053 char S[30];
1054 sprintf(S,":%u",Port);
1055 Res += S;
1056 }
1057 }
1058
1059 if (Path.empty() == false)
1060 {
1061 if (Path[0] != '/')
1062 Res += "/" + Path;
1063 else
1064 Res += Path;
1065 }
1066
1067 return Res;
1068 }
1069 /*}}}*/
1070 // URI::SiteOnly - Return the schema and site for the URI /*{{{*/
1071 // ---------------------------------------------------------------------
1072 /* */
1073 string URI::SiteOnly(string URI)
1074 {
1075 ::URI U(URI);
1076 U.User = string();
1077 U.Password = string();
1078 U.Path = string();
1079 U.Port = 0;
1080 return U;
1081 }
1082 /*}}}*/