]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode: cpp; mode: fold -*- | |
2 | // Description /*{{{*/ | |
3 | // $Id: strutl.cc,v 1.48 2003/07/18 14:15:11 mdz 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 | #include <apt-pkg/strutl.h> | |
19 | #include <apt-pkg/fileutl.h> | |
20 | #include <apt-pkg/error.h> | |
21 | ||
22 | #include <apti18n.h> | |
23 | ||
24 | #include <ctype.h> | |
25 | #include <string.h> | |
26 | #include <stdio.h> | |
27 | #include <algorithm> | |
28 | #include <unistd.h> | |
29 | #include <regex.h> | |
30 | #include <errno.h> | |
31 | #include <stdarg.h> | |
32 | #include <iconv.h> | |
33 | ||
34 | #include "config.h" | |
35 | ||
36 | using namespace std; | |
37 | /*}}}*/ | |
38 | ||
39 | // UTF8ToCodeset - Convert some UTF-8 string for some codeset /*{{{*/ | |
40 | // --------------------------------------------------------------------- | |
41 | /* This is handy to use before display some information for enduser */ | |
42 | bool UTF8ToCodeset(const char *codeset, const string &orig, string *dest) | |
43 | { | |
44 | iconv_t cd; | |
45 | const char *inbuf; | |
46 | char *inptr, *outbuf; | |
47 | size_t insize, bufsize; | |
48 | dest->clear(); | |
49 | ||
50 | cd = iconv_open(codeset, "UTF-8"); | |
51 | if (cd == (iconv_t)(-1)) { | |
52 | // Something went wrong | |
53 | if (errno == EINVAL) | |
54 | _error->Error("conversion from 'UTF-8' to '%s' not available", | |
55 | codeset); | |
56 | else | |
57 | perror("iconv_open"); | |
58 | ||
59 | return false; | |
60 | } | |
61 | ||
62 | insize = bufsize = orig.size(); | |
63 | inbuf = orig.data(); | |
64 | inptr = (char *)inbuf; | |
65 | outbuf = new char[bufsize]; | |
66 | size_t lastError = -1; | |
67 | ||
68 | while (insize != 0) | |
69 | { | |
70 | char *outptr = outbuf; | |
71 | size_t outsize = bufsize; | |
72 | size_t const err = iconv(cd, &inptr, &insize, &outptr, &outsize); | |
73 | dest->append(outbuf, outptr - outbuf); | |
74 | if (err == (size_t)(-1)) | |
75 | { | |
76 | switch (errno) | |
77 | { | |
78 | case EILSEQ: | |
79 | insize--; | |
80 | inptr++; | |
81 | // replace a series of unknown multibytes with a single "?" | |
82 | if (lastError != insize) { | |
83 | lastError = insize - 1; | |
84 | dest->append("?"); | |
85 | } | |
86 | break; | |
87 | case EINVAL: | |
88 | insize = 0; | |
89 | break; | |
90 | case E2BIG: | |
91 | if (outptr == outbuf) | |
92 | { | |
93 | bufsize *= 2; | |
94 | delete[] outbuf; | |
95 | outbuf = new char[bufsize]; | |
96 | } | |
97 | break; | |
98 | } | |
99 | } | |
100 | } | |
101 | ||
102 | delete[] outbuf; | |
103 | ||
104 | iconv_close(cd); | |
105 | ||
106 | return true; | |
107 | } | |
108 | /*}}}*/ | |
109 | // strstrip - Remove white space from the front and back of a string /*{{{*/ | |
110 | // --------------------------------------------------------------------- | |
111 | /* This is handy to use when parsing a file. It also removes \n's left | |
112 | over from fgets and company */ | |
113 | char *_strstrip(char *String) | |
114 | { | |
115 | for (;*String != 0 && (*String == ' ' || *String == '\t'); String++); | |
116 | ||
117 | if (*String == 0) | |
118 | return String; | |
119 | ||
120 | char *End = String + strlen(String) - 1; | |
121 | for (;End != String - 1 && (*End == ' ' || *End == '\t' || *End == '\n' || | |
122 | *End == '\r'); End--); | |
123 | End++; | |
124 | *End = 0; | |
125 | return String; | |
126 | }; | |
127 | /*}}}*/ | |
128 | // strtabexpand - Converts tabs into 8 spaces /*{{{*/ | |
129 | // --------------------------------------------------------------------- | |
130 | /* */ | |
131 | char *_strtabexpand(char *String,size_t Len) | |
132 | { | |
133 | for (char *I = String; I != I + Len && *I != 0; I++) | |
134 | { | |
135 | if (*I != '\t') | |
136 | continue; | |
137 | if (I + 8 > String + Len) | |
138 | { | |
139 | *I = 0; | |
140 | return String; | |
141 | } | |
142 | ||
143 | /* Assume the start of the string is 0 and find the next 8 char | |
144 | division */ | |
145 | int Len; | |
146 | if (String == I) | |
147 | Len = 1; | |
148 | else | |
149 | Len = 8 - ((String - I) % 8); | |
150 | Len -= 2; | |
151 | if (Len <= 0) | |
152 | { | |
153 | *I = ' '; | |
154 | continue; | |
155 | } | |
156 | ||
157 | memmove(I + Len,I + 1,strlen(I) + 1); | |
158 | for (char *J = I; J + Len != I; *I = ' ', I++); | |
159 | } | |
160 | return String; | |
161 | } | |
162 | /*}}}*/ | |
163 | // ParseQuoteWord - Parse a single word out of a string /*{{{*/ | |
164 | // --------------------------------------------------------------------- | |
165 | /* This grabs a single word, converts any % escaped characters to their | |
166 | proper values and advances the pointer. Double quotes are understood | |
167 | and striped out as well. This is for URI/URL parsing. It also can | |
168 | understand [] brackets.*/ | |
169 | bool ParseQuoteWord(const char *&String,string &Res) | |
170 | { | |
171 | // Skip leading whitespace | |
172 | const char *C = String; | |
173 | for (;*C != 0 && *C == ' '; C++); | |
174 | if (*C == 0) | |
175 | return false; | |
176 | ||
177 | // Jump to the next word | |
178 | for (;*C != 0 && isspace(*C) == 0; C++) | |
179 | { | |
180 | if (*C == '"') | |
181 | { | |
182 | for (C++; *C != 0 && *C != '"'; C++); | |
183 | if (*C == 0) | |
184 | return false; | |
185 | } | |
186 | if (*C == '[') | |
187 | { | |
188 | for (C++; *C != 0 && *C != ']'; C++); | |
189 | if (*C == 0) | |
190 | return false; | |
191 | } | |
192 | } | |
193 | ||
194 | // Now de-quote characters | |
195 | char Buffer[1024]; | |
196 | char Tmp[3]; | |
197 | const char *Start = String; | |
198 | char *I; | |
199 | for (I = Buffer; I < Buffer + sizeof(Buffer) && Start != C; I++) | |
200 | { | |
201 | if (*Start == '%' && Start + 2 < C && | |
202 | isxdigit(Start[1]) && isxdigit(Start[2])) | |
203 | { | |
204 | Tmp[0] = Start[1]; | |
205 | Tmp[1] = Start[2]; | |
206 | Tmp[2] = 0; | |
207 | *I = (char)strtol(Tmp,0,16); | |
208 | Start += 3; | |
209 | continue; | |
210 | } | |
211 | if (*Start != '"') | |
212 | *I = *Start; | |
213 | else | |
214 | I--; | |
215 | Start++; | |
216 | } | |
217 | *I = 0; | |
218 | Res = Buffer; | |
219 | ||
220 | // Skip ending white space | |
221 | for (;*C != 0 && isspace(*C) != 0; C++); | |
222 | String = C; | |
223 | return true; | |
224 | } | |
225 | /*}}}*/ | |
226 | // ParseCWord - Parses a string like a C "" expression /*{{{*/ | |
227 | // --------------------------------------------------------------------- | |
228 | /* This expects a series of space separated strings enclosed in ""'s. | |
229 | It concatenates the ""'s into a single string. */ | |
230 | bool ParseCWord(const char *&String,string &Res) | |
231 | { | |
232 | // Skip leading whitespace | |
233 | const char *C = String; | |
234 | for (;*C != 0 && *C == ' '; C++); | |
235 | if (*C == 0) | |
236 | return false; | |
237 | ||
238 | char Buffer[1024]; | |
239 | char *Buf = Buffer; | |
240 | if (strlen(String) >= sizeof(Buffer)) | |
241 | return false; | |
242 | ||
243 | for (; *C != 0; C++) | |
244 | { | |
245 | if (*C == '"') | |
246 | { | |
247 | for (C++; *C != 0 && *C != '"'; C++) | |
248 | *Buf++ = *C; | |
249 | ||
250 | if (*C == 0) | |
251 | return false; | |
252 | ||
253 | continue; | |
254 | } | |
255 | ||
256 | if (C != String && isspace(*C) != 0 && isspace(C[-1]) != 0) | |
257 | continue; | |
258 | if (isspace(*C) == 0) | |
259 | return false; | |
260 | *Buf++ = ' '; | |
261 | } | |
262 | *Buf = 0; | |
263 | Res = Buffer; | |
264 | String = C; | |
265 | return true; | |
266 | } | |
267 | /*}}}*/ | |
268 | // QuoteString - Convert a string into quoted from /*{{{*/ | |
269 | // --------------------------------------------------------------------- | |
270 | /* */ | |
271 | string QuoteString(const string &Str, const char *Bad) | |
272 | { | |
273 | string Res; | |
274 | for (string::const_iterator I = Str.begin(); I != Str.end(); I++) | |
275 | { | |
276 | if (strchr(Bad,*I) != 0 || isprint(*I) == 0 || | |
277 | *I == 0x25 || // percent '%' char | |
278 | *I <= 0x20 || *I >= 0x7F) // control chars | |
279 | { | |
280 | char Buf[10]; | |
281 | sprintf(Buf,"%%%02x",(int)*I); | |
282 | Res += Buf; | |
283 | } | |
284 | else | |
285 | Res += *I; | |
286 | } | |
287 | return Res; | |
288 | } | |
289 | /*}}}*/ | |
290 | // DeQuoteString - Convert a string from quoted from /*{{{*/ | |
291 | // --------------------------------------------------------------------- | |
292 | /* This undoes QuoteString */ | |
293 | string DeQuoteString(const string &Str) | |
294 | { | |
295 | return DeQuoteString(Str.begin(),Str.end()); | |
296 | } | |
297 | string DeQuoteString(string::const_iterator const &begin, | |
298 | string::const_iterator const &end) | |
299 | { | |
300 | string Res; | |
301 | for (string::const_iterator I = begin; I != end; I++) | |
302 | { | |
303 | if (*I == '%' && I + 2 < end && | |
304 | isxdigit(I[1]) && isxdigit(I[2])) | |
305 | { | |
306 | char Tmp[3]; | |
307 | Tmp[0] = I[1]; | |
308 | Tmp[1] = I[2]; | |
309 | Tmp[2] = 0; | |
310 | Res += (char)strtol(Tmp,0,16); | |
311 | I += 2; | |
312 | continue; | |
313 | } | |
314 | else | |
315 | Res += *I; | |
316 | } | |
317 | return Res; | |
318 | } | |
319 | ||
320 | /*}}}*/ | |
321 | // SizeToStr - Convert a long into a human readable size /*{{{*/ | |
322 | // --------------------------------------------------------------------- | |
323 | /* A max of 4 digits are shown before conversion to the next highest unit. | |
324 | The max length of the string will be 5 chars unless the size is > 10 | |
325 | YottaBytes (E24) */ | |
326 | string SizeToStr(double Size) | |
327 | { | |
328 | char S[300]; | |
329 | double ASize; | |
330 | if (Size >= 0) | |
331 | ASize = Size; | |
332 | else | |
333 | ASize = -1*Size; | |
334 | ||
335 | /* bytes, KiloBytes, MegaBytes, GigaBytes, TeraBytes, PetaBytes, | |
336 | ExaBytes, ZettaBytes, YottaBytes */ | |
337 | char Ext[] = {'\0','k','M','G','T','P','E','Z','Y'}; | |
338 | int I = 0; | |
339 | while (I <= 8) | |
340 | { | |
341 | if (ASize < 100 && I != 0) | |
342 | { | |
343 | sprintf(S,"%'.1f%c",ASize,Ext[I]); | |
344 | break; | |
345 | } | |
346 | ||
347 | if (ASize < 10000) | |
348 | { | |
349 | sprintf(S,"%'.0f%c",ASize,Ext[I]); | |
350 | break; | |
351 | } | |
352 | ASize /= 1000.0; | |
353 | I++; | |
354 | } | |
355 | ||
356 | return S; | |
357 | } | |
358 | /*}}}*/ | |
359 | // TimeToStr - Convert the time into a string /*{{{*/ | |
360 | // --------------------------------------------------------------------- | |
361 | /* Converts a number of seconds to a hms format */ | |
362 | string TimeToStr(unsigned long Sec) | |
363 | { | |
364 | char S[300]; | |
365 | ||
366 | while (1) | |
367 | { | |
368 | if (Sec > 60*60*24) | |
369 | { | |
370 | //d means days, h means hours, min means minutes, s means seconds | |
371 | sprintf(S,_("%lid %lih %limin %lis"),Sec/60/60/24,(Sec/60/60) % 24,(Sec/60) % 60,Sec % 60); | |
372 | break; | |
373 | } | |
374 | ||
375 | if (Sec > 60*60) | |
376 | { | |
377 | //h means hours, min means minutes, s means seconds | |
378 | sprintf(S,_("%lih %limin %lis"),Sec/60/60,(Sec/60) % 60,Sec % 60); | |
379 | break; | |
380 | } | |
381 | ||
382 | if (Sec > 60) | |
383 | { | |
384 | //min means minutes, s means seconds | |
385 | sprintf(S,_("%limin %lis"),Sec/60,Sec % 60); | |
386 | break; | |
387 | } | |
388 | ||
389 | //s means seconds | |
390 | sprintf(S,_("%lis"),Sec); | |
391 | break; | |
392 | } | |
393 | ||
394 | return S; | |
395 | } | |
396 | /*}}}*/ | |
397 | // SubstVar - Substitute a string for another string /*{{{*/ | |
398 | // --------------------------------------------------------------------- | |
399 | /* This replaces all occurances of Subst with Contents in Str. */ | |
400 | string SubstVar(const string &Str,const string &Subst,const string &Contents) | |
401 | { | |
402 | string::size_type Pos = 0; | |
403 | string::size_type OldPos = 0; | |
404 | string Temp; | |
405 | ||
406 | while (OldPos < Str.length() && | |
407 | (Pos = Str.find(Subst,OldPos)) != string::npos) | |
408 | { | |
409 | Temp += string(Str,OldPos,Pos) + Contents; | |
410 | OldPos = Pos + Subst.length(); | |
411 | } | |
412 | ||
413 | if (OldPos == 0) | |
414 | return Str; | |
415 | ||
416 | return Temp + string(Str,OldPos); | |
417 | } | |
418 | ||
419 | string SubstVar(string Str,const struct SubstVar *Vars) | |
420 | { | |
421 | for (; Vars->Subst != 0; Vars++) | |
422 | Str = SubstVar(Str,Vars->Subst,*Vars->Contents); | |
423 | return Str; | |
424 | } | |
425 | /*}}}*/ | |
426 | // OutputInDepth - return a string with separator multiplied with depth /*{{{*/ | |
427 | // --------------------------------------------------------------------- | |
428 | /* Returns a string with the supplied separator depth + 1 times in it */ | |
429 | std::string OutputInDepth(const unsigned long Depth, const char* Separator) | |
430 | { | |
431 | std::string output = ""; | |
432 | for(unsigned long d=Depth+1; d > 0; d--) | |
433 | output.append(Separator); | |
434 | return output; | |
435 | } | |
436 | /*}}}*/ | |
437 | // URItoFileName - Convert the uri into a unique file name /*{{{*/ | |
438 | // --------------------------------------------------------------------- | |
439 | /* This converts a URI into a safe filename. It quotes all unsafe characters | |
440 | and converts / to _ and removes the scheme identifier. The resulting | |
441 | file name should be unique and never occur again for a different file */ | |
442 | string URItoFileName(const string &URI) | |
443 | { | |
444 | // Nuke 'sensitive' items | |
445 | ::URI U(URI); | |
446 | U.User.clear(); | |
447 | U.Password.clear(); | |
448 | U.Access.clear(); | |
449 | ||
450 | // "\x00-\x20{}|\\\\^\\[\\]<>\"\x7F-\xFF"; | |
451 | string NewURI = QuoteString(U,"\\|{}[]<>\"^~_=!@#$%^&*"); | |
452 | replace(NewURI.begin(),NewURI.end(),'/','_'); | |
453 | return NewURI; | |
454 | } | |
455 | /*}}}*/ | |
456 | // Base64Encode - Base64 Encoding routine for short strings /*{{{*/ | |
457 | // --------------------------------------------------------------------- | |
458 | /* This routine performs a base64 transformation on a string. It was ripped | |
459 | from wget and then patched and bug fixed. | |
460 | ||
461 | This spec can be found in rfc2045 */ | |
462 | string Base64Encode(const string &S) | |
463 | { | |
464 | // Conversion table. | |
465 | static char tbl[64] = {'A','B','C','D','E','F','G','H', | |
466 | 'I','J','K','L','M','N','O','P', | |
467 | 'Q','R','S','T','U','V','W','X', | |
468 | 'Y','Z','a','b','c','d','e','f', | |
469 | 'g','h','i','j','k','l','m','n', | |
470 | 'o','p','q','r','s','t','u','v', | |
471 | 'w','x','y','z','0','1','2','3', | |
472 | '4','5','6','7','8','9','+','/'}; | |
473 | ||
474 | // Pre-allocate some space | |
475 | string Final; | |
476 | Final.reserve((4*S.length() + 2)/3 + 2); | |
477 | ||
478 | /* Transform the 3x8 bits to 4x6 bits, as required by | |
479 | base64. */ | |
480 | for (string::const_iterator I = S.begin(); I < S.end(); I += 3) | |
481 | { | |
482 | char Bits[3] = {0,0,0}; | |
483 | Bits[0] = I[0]; | |
484 | if (I + 1 < S.end()) | |
485 | Bits[1] = I[1]; | |
486 | if (I + 2 < S.end()) | |
487 | Bits[2] = I[2]; | |
488 | ||
489 | Final += tbl[Bits[0] >> 2]; | |
490 | Final += tbl[((Bits[0] & 3) << 4) + (Bits[1] >> 4)]; | |
491 | ||
492 | if (I + 1 >= S.end()) | |
493 | break; | |
494 | ||
495 | Final += tbl[((Bits[1] & 0xf) << 2) + (Bits[2] >> 6)]; | |
496 | ||
497 | if (I + 2 >= S.end()) | |
498 | break; | |
499 | ||
500 | Final += tbl[Bits[2] & 0x3f]; | |
501 | } | |
502 | ||
503 | /* Apply the padding elements, this tells how many bytes the remote | |
504 | end should discard */ | |
505 | if (S.length() % 3 == 2) | |
506 | Final += '='; | |
507 | if (S.length() % 3 == 1) | |
508 | Final += "=="; | |
509 | ||
510 | return Final; | |
511 | } | |
512 | /*}}}*/ | |
513 | // stringcmp - Arbitrary string compare /*{{{*/ | |
514 | // --------------------------------------------------------------------- | |
515 | /* This safely compares two non-null terminated strings of arbitrary | |
516 | length */ | |
517 | int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd) | |
518 | { | |
519 | for (; A != AEnd && B != BEnd; A++, B++) | |
520 | if (*A != *B) | |
521 | break; | |
522 | ||
523 | if (A == AEnd && B == BEnd) | |
524 | return 0; | |
525 | if (A == AEnd) | |
526 | return 1; | |
527 | if (B == BEnd) | |
528 | return -1; | |
529 | if (*A < *B) | |
530 | return -1; | |
531 | return 1; | |
532 | } | |
533 | ||
534 | #if __GNUC__ >= 3 | |
535 | int stringcmp(string::const_iterator A,string::const_iterator AEnd, | |
536 | const char *B,const char *BEnd) | |
537 | { | |
538 | for (; A != AEnd && B != BEnd; A++, B++) | |
539 | if (*A != *B) | |
540 | break; | |
541 | ||
542 | if (A == AEnd && B == BEnd) | |
543 | return 0; | |
544 | if (A == AEnd) | |
545 | return 1; | |
546 | if (B == BEnd) | |
547 | return -1; | |
548 | if (*A < *B) | |
549 | return -1; | |
550 | return 1; | |
551 | } | |
552 | int stringcmp(string::const_iterator A,string::const_iterator AEnd, | |
553 | string::const_iterator B,string::const_iterator BEnd) | |
554 | { | |
555 | for (; A != AEnd && B != BEnd; A++, B++) | |
556 | if (*A != *B) | |
557 | break; | |
558 | ||
559 | if (A == AEnd && B == BEnd) | |
560 | return 0; | |
561 | if (A == AEnd) | |
562 | return 1; | |
563 | if (B == BEnd) | |
564 | return -1; | |
565 | if (*A < *B) | |
566 | return -1; | |
567 | return 1; | |
568 | } | |
569 | #endif | |
570 | /*}}}*/ | |
571 | // stringcasecmp - Arbitrary case insensitive string compare /*{{{*/ | |
572 | // --------------------------------------------------------------------- | |
573 | /* */ | |
574 | int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd) | |
575 | { | |
576 | for (; A != AEnd && B != BEnd; A++, B++) | |
577 | if (tolower_ascii(*A) != tolower_ascii(*B)) | |
578 | break; | |
579 | ||
580 | if (A == AEnd && B == BEnd) | |
581 | return 0; | |
582 | if (A == AEnd) | |
583 | return 1; | |
584 | if (B == BEnd) | |
585 | return -1; | |
586 | if (tolower_ascii(*A) < tolower_ascii(*B)) | |
587 | return -1; | |
588 | return 1; | |
589 | } | |
590 | #if __GNUC__ >= 3 | |
591 | int stringcasecmp(string::const_iterator A,string::const_iterator AEnd, | |
592 | const char *B,const char *BEnd) | |
593 | { | |
594 | for (; A != AEnd && B != BEnd; A++, B++) | |
595 | if (tolower_ascii(*A) != tolower_ascii(*B)) | |
596 | break; | |
597 | ||
598 | if (A == AEnd && B == BEnd) | |
599 | return 0; | |
600 | if (A == AEnd) | |
601 | return 1; | |
602 | if (B == BEnd) | |
603 | return -1; | |
604 | if (tolower_ascii(*A) < tolower_ascii(*B)) | |
605 | return -1; | |
606 | return 1; | |
607 | } | |
608 | int stringcasecmp(string::const_iterator A,string::const_iterator AEnd, | |
609 | string::const_iterator B,string::const_iterator BEnd) | |
610 | { | |
611 | for (; A != AEnd && B != BEnd; A++, B++) | |
612 | if (tolower_ascii(*A) != tolower_ascii(*B)) | |
613 | break; | |
614 | ||
615 | if (A == AEnd && B == BEnd) | |
616 | return 0; | |
617 | if (A == AEnd) | |
618 | return 1; | |
619 | if (B == BEnd) | |
620 | return -1; | |
621 | if (tolower_ascii(*A) < tolower_ascii(*B)) | |
622 | return -1; | |
623 | return 1; | |
624 | } | |
625 | #endif | |
626 | /*}}}*/ | |
627 | // LookupTag - Lookup the value of a tag in a taged string /*{{{*/ | |
628 | // --------------------------------------------------------------------- | |
629 | /* The format is like those used in package files and the method | |
630 | communication system */ | |
631 | string LookupTag(const string &Message,const char *Tag,const char *Default) | |
632 | { | |
633 | // Look for a matching tag. | |
634 | int Length = strlen(Tag); | |
635 | for (string::const_iterator I = Message.begin(); I + Length < Message.end(); I++) | |
636 | { | |
637 | // Found the tag | |
638 | if (I[Length] == ':' && stringcasecmp(I,I+Length,Tag) == 0) | |
639 | { | |
640 | // Find the end of line and strip the leading/trailing spaces | |
641 | string::const_iterator J; | |
642 | I += Length + 1; | |
643 | for (; isspace(*I) != 0 && I < Message.end(); I++); | |
644 | for (J = I; *J != '\n' && J < Message.end(); J++); | |
645 | for (; J > I && isspace(J[-1]) != 0; J--); | |
646 | ||
647 | return string(I,J); | |
648 | } | |
649 | ||
650 | for (; *I != '\n' && I < Message.end(); I++); | |
651 | } | |
652 | ||
653 | // Failed to find a match | |
654 | if (Default == 0) | |
655 | return string(); | |
656 | return Default; | |
657 | } | |
658 | /*}}}*/ | |
659 | // StringToBool - Converts a string into a boolean /*{{{*/ | |
660 | // --------------------------------------------------------------------- | |
661 | /* This inspects the string to see if it is true or if it is false and | |
662 | then returns the result. Several varients on true/false are checked. */ | |
663 | int StringToBool(const string &Text,int Default) | |
664 | { | |
665 | char *End; | |
666 | int Res = strtol(Text.c_str(),&End,0); | |
667 | if (End != Text.c_str() && Res >= 0 && Res <= 1) | |
668 | return Res; | |
669 | ||
670 | // Check for positives | |
671 | if (strcasecmp(Text.c_str(),"no") == 0 || | |
672 | strcasecmp(Text.c_str(),"false") == 0 || | |
673 | strcasecmp(Text.c_str(),"without") == 0 || | |
674 | strcasecmp(Text.c_str(),"off") == 0 || | |
675 | strcasecmp(Text.c_str(),"disable") == 0) | |
676 | return 0; | |
677 | ||
678 | // Check for negatives | |
679 | if (strcasecmp(Text.c_str(),"yes") == 0 || | |
680 | strcasecmp(Text.c_str(),"true") == 0 || | |
681 | strcasecmp(Text.c_str(),"with") == 0 || | |
682 | strcasecmp(Text.c_str(),"on") == 0 || | |
683 | strcasecmp(Text.c_str(),"enable") == 0) | |
684 | return 1; | |
685 | ||
686 | return Default; | |
687 | } | |
688 | /*}}}*/ | |
689 | // TimeRFC1123 - Convert a time_t into RFC1123 format /*{{{*/ | |
690 | // --------------------------------------------------------------------- | |
691 | /* This converts a time_t into a string time representation that is | |
692 | year 2000 complient and timezone neutral */ | |
693 | string TimeRFC1123(time_t Date) | |
694 | { | |
695 | struct tm Conv = *gmtime(&Date); | |
696 | char Buf[300]; | |
697 | ||
698 | const char *Day[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; | |
699 | const char *Month[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul", | |
700 | "Aug","Sep","Oct","Nov","Dec"}; | |
701 | ||
702 | sprintf(Buf,"%s, %02i %s %i %02i:%02i:%02i GMT",Day[Conv.tm_wday], | |
703 | Conv.tm_mday,Month[Conv.tm_mon],Conv.tm_year+1900,Conv.tm_hour, | |
704 | Conv.tm_min,Conv.tm_sec); | |
705 | return Buf; | |
706 | } | |
707 | /*}}}*/ | |
708 | // ReadMessages - Read messages from the FD /*{{{*/ | |
709 | // --------------------------------------------------------------------- | |
710 | /* This pulls full messages from the input FD into the message buffer. | |
711 | It assumes that messages will not pause during transit so no | |
712 | fancy buffering is used. | |
713 | ||
714 | In particular: this reads blocks from the input until it believes | |
715 | that it's run out of input text. Each block is terminated by a | |
716 | double newline ('\n' followed by '\n'). As noted below, there is a | |
717 | bug in this code: it assumes that all the blocks have been read if | |
718 | it doesn't see additional text in the buffer after the last one is | |
719 | parsed, which will cause it to lose blocks if the last block | |
720 | coincides with the end of the buffer. | |
721 | */ | |
722 | bool ReadMessages(int Fd, vector<string> &List) | |
723 | { | |
724 | char Buffer[64000]; | |
725 | char *End = Buffer; | |
726 | // Represents any left-over from the previous iteration of the | |
727 | // parse loop. (i.e., if a message is split across the end | |
728 | // of the buffer, it goes here) | |
729 | string PartialMessage; | |
730 | ||
731 | while (1) | |
732 | { | |
733 | int Res = read(Fd,End,sizeof(Buffer) - (End-Buffer)); | |
734 | if (Res < 0 && errno == EINTR) | |
735 | continue; | |
736 | ||
737 | // Process is dead, this is kind of bad.. | |
738 | if (Res == 0) | |
739 | return false; | |
740 | ||
741 | // No data | |
742 | if (Res < 0 && errno == EAGAIN) | |
743 | return true; | |
744 | if (Res < 0) | |
745 | return false; | |
746 | ||
747 | End += Res; | |
748 | ||
749 | // Look for the end of the message | |
750 | for (char *I = Buffer; I + 1 < End; I++) | |
751 | { | |
752 | if (I[0] != '\n' || I[1] != '\n') | |
753 | continue; | |
754 | ||
755 | // Pull the message out | |
756 | string Message(Buffer,I-Buffer); | |
757 | PartialMessage += Message; | |
758 | ||
759 | // Fix up the buffer | |
760 | for (; I < End && *I == '\n'; I++); | |
761 | End -= I-Buffer; | |
762 | memmove(Buffer,I,End-Buffer); | |
763 | I = Buffer; | |
764 | ||
765 | List.push_back(PartialMessage); | |
766 | PartialMessage.clear(); | |
767 | } | |
768 | if (End != Buffer) | |
769 | { | |
770 | // If there's text left in the buffer, store it | |
771 | // in PartialMessage and throw the rest of the buffer | |
772 | // away. This allows us to handle messages that | |
773 | // are longer than the static buffer size. | |
774 | PartialMessage += string(Buffer, End); | |
775 | End = Buffer; | |
776 | } | |
777 | else | |
778 | { | |
779 | // BUG ALERT: if a message block happens to end at a | |
780 | // multiple of 64000 characters, this will cause it to | |
781 | // terminate early, leading to a badly formed block and | |
782 | // probably crashing the method. However, this is the only | |
783 | // way we have to find the end of the message block. I have | |
784 | // an idea of how to fix this, but it will require changes | |
785 | // to the protocol (essentially to mark the beginning and | |
786 | // end of the block). | |
787 | // | |
788 | // -- dburrows 2008-04-02 | |
789 | return true; | |
790 | } | |
791 | ||
792 | if (WaitFd(Fd) == false) | |
793 | return false; | |
794 | } | |
795 | } | |
796 | /*}}}*/ | |
797 | // MonthConv - Converts a month string into a number /*{{{*/ | |
798 | // --------------------------------------------------------------------- | |
799 | /* This was lifted from the boa webserver which lifted it from 'wn-v1.07' | |
800 | Made it a bit more robust with a few tolower_ascii though. */ | |
801 | static int MonthConv(char *Month) | |
802 | { | |
803 | switch (tolower_ascii(*Month)) | |
804 | { | |
805 | case 'a': | |
806 | return tolower_ascii(Month[1]) == 'p'?3:7; | |
807 | case 'd': | |
808 | return 11; | |
809 | case 'f': | |
810 | return 1; | |
811 | case 'j': | |
812 | if (tolower_ascii(Month[1]) == 'a') | |
813 | return 0; | |
814 | return tolower_ascii(Month[2]) == 'n'?5:6; | |
815 | case 'm': | |
816 | return tolower_ascii(Month[2]) == 'r'?2:4; | |
817 | case 'n': | |
818 | return 10; | |
819 | case 'o': | |
820 | return 9; | |
821 | case 's': | |
822 | return 8; | |
823 | ||
824 | // Pretend it is January.. | |
825 | default: | |
826 | return 0; | |
827 | } | |
828 | } | |
829 | /*}}}*/ | |
830 | // timegm - Internal timegm function if gnu is not available /*{{{*/ | |
831 | // --------------------------------------------------------------------- | |
832 | /* Ripped this evil little function from wget - I prefer the use of | |
833 | GNU timegm if possible as this technique will have interesting problems | |
834 | with leap seconds, timezones and other. | |
835 | ||
836 | Converts struct tm to time_t, assuming the data in tm is UTC rather | |
837 | than local timezone (mktime assumes the latter). | |
838 | ||
839 | Contributed by Roger Beeman <beeman@cisco.com>, with the help of | |
840 | Mark Baushke <mdb@cisco.com> and the rest of the Gurus at CISCO. */ | |
841 | ||
842 | /* Turned it into an autoconf check, because GNU is not the only thing which | |
843 | can provide timegm. -- 2002-09-22, Joel Baker */ | |
844 | ||
845 | #ifndef HAVE_TIMEGM // Now with autoconf! | |
846 | static time_t timegm(struct tm *t) | |
847 | { | |
848 | time_t tl, tb; | |
849 | ||
850 | tl = mktime (t); | |
851 | if (tl == -1) | |
852 | return -1; | |
853 | tb = mktime (gmtime (&tl)); | |
854 | return (tl <= tb ? (tl + (tl - tb)) : (tl - (tb - tl))); | |
855 | } | |
856 | #endif | |
857 | /*}}}*/ | |
858 | // StrToTime - Converts a string into a time_t /*{{{*/ | |
859 | // --------------------------------------------------------------------- | |
860 | /* This handles all 3 populare time formats including RFC 1123, RFC 1036 | |
861 | and the C library asctime format. It requires the GNU library function | |
862 | 'timegm' to convert a struct tm in UTC to a time_t. For some bizzar | |
863 | reason the C library does not provide any such function :< This also | |
864 | handles the weird, but unambiguous FTP time format*/ | |
865 | bool StrToTime(const string &Val,time_t &Result) | |
866 | { | |
867 | struct tm Tm; | |
868 | char Month[10]; | |
869 | const char *I = Val.c_str(); | |
870 | ||
871 | // Skip the day of the week | |
872 | for (;*I != 0 && *I != ' '; I++); | |
873 | ||
874 | // Handle RFC 1123 time | |
875 | Month[0] = 0; | |
876 | if (sscanf(I," %d %3s %d %d:%d:%d GMT",&Tm.tm_mday,Month,&Tm.tm_year, | |
877 | &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) != 6) | |
878 | { | |
879 | // Handle RFC 1036 time | |
880 | if (sscanf(I," %d-%3s-%d %d:%d:%d GMT",&Tm.tm_mday,Month, | |
881 | &Tm.tm_year,&Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) == 6) | |
882 | Tm.tm_year += 1900; | |
883 | else | |
884 | { | |
885 | // asctime format | |
886 | if (sscanf(I," %3s %d %d:%d:%d %d",Month,&Tm.tm_mday, | |
887 | &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec,&Tm.tm_year) != 6) | |
888 | { | |
889 | // 'ftp' time | |
890 | if (sscanf(Val.c_str(),"%4d%2d%2d%2d%2d%2d",&Tm.tm_year,&Tm.tm_mon, | |
891 | &Tm.tm_mday,&Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) != 6) | |
892 | return false; | |
893 | Tm.tm_mon--; | |
894 | } | |
895 | } | |
896 | } | |
897 | ||
898 | Tm.tm_isdst = 0; | |
899 | if (Month[0] != 0) | |
900 | Tm.tm_mon = MonthConv(Month); | |
901 | Tm.tm_year -= 1900; | |
902 | ||
903 | // Convert to local time and then to GMT | |
904 | Result = timegm(&Tm); | |
905 | return true; | |
906 | } | |
907 | /*}}}*/ | |
908 | // StrToNum - Convert a fixed length string to a number /*{{{*/ | |
909 | // --------------------------------------------------------------------- | |
910 | /* This is used in decoding the crazy fixed length string headers in | |
911 | tar and ar files. */ | |
912 | bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base) | |
913 | { | |
914 | char S[30]; | |
915 | if (Len >= sizeof(S)) | |
916 | return false; | |
917 | memcpy(S,Str,Len); | |
918 | S[Len] = 0; | |
919 | ||
920 | // All spaces is a zero | |
921 | Res = 0; | |
922 | unsigned I; | |
923 | for (I = 0; S[I] == ' '; I++); | |
924 | if (S[I] == 0) | |
925 | return true; | |
926 | ||
927 | char *End; | |
928 | Res = strtoul(S,&End,Base); | |
929 | if (End == S) | |
930 | return false; | |
931 | ||
932 | return true; | |
933 | } | |
934 | /*}}}*/ | |
935 | // HexDigit - Convert a hex character into an integer /*{{{*/ | |
936 | // --------------------------------------------------------------------- | |
937 | /* Helper for Hex2Num */ | |
938 | static int HexDigit(int c) | |
939 | { | |
940 | if (c >= '0' && c <= '9') | |
941 | return c - '0'; | |
942 | if (c >= 'a' && c <= 'f') | |
943 | return c - 'a' + 10; | |
944 | if (c >= 'A' && c <= 'F') | |
945 | return c - 'A' + 10; | |
946 | return 0; | |
947 | } | |
948 | /*}}}*/ | |
949 | // Hex2Num - Convert a long hex number into a buffer /*{{{*/ | |
950 | // --------------------------------------------------------------------- | |
951 | /* The length of the buffer must be exactly 1/2 the length of the string. */ | |
952 | bool Hex2Num(const string &Str,unsigned char *Num,unsigned int Length) | |
953 | { | |
954 | if (Str.length() != Length*2) | |
955 | return false; | |
956 | ||
957 | // Convert each digit. We store it in the same order as the string | |
958 | int J = 0; | |
959 | for (string::const_iterator I = Str.begin(); I != Str.end();J++, I += 2) | |
960 | { | |
961 | if (isxdigit(*I) == 0 || isxdigit(I[1]) == 0) | |
962 | return false; | |
963 | ||
964 | Num[J] = HexDigit(I[0]) << 4; | |
965 | Num[J] += HexDigit(I[1]); | |
966 | } | |
967 | ||
968 | return true; | |
969 | } | |
970 | /*}}}*/ | |
971 | // TokSplitString - Split a string up by a given token /*{{{*/ | |
972 | // --------------------------------------------------------------------- | |
973 | /* This is intended to be a faster splitter, it does not use dynamic | |
974 | memories. Input is changed to insert nulls at each token location. */ | |
975 | bool TokSplitString(char Tok,char *Input,char **List, | |
976 | unsigned long ListMax) | |
977 | { | |
978 | // Strip any leading spaces | |
979 | char *Start = Input; | |
980 | char *Stop = Start + strlen(Start); | |
981 | for (; *Start != 0 && isspace(*Start) != 0; Start++); | |
982 | ||
983 | unsigned long Count = 0; | |
984 | char *Pos = Start; | |
985 | while (Pos != Stop) | |
986 | { | |
987 | // Skip to the next Token | |
988 | for (; Pos != Stop && *Pos != Tok; Pos++); | |
989 | ||
990 | // Back remove spaces | |
991 | char *End = Pos; | |
992 | for (; End > Start && (End[-1] == Tok || isspace(End[-1]) != 0); End--); | |
993 | *End = 0; | |
994 | ||
995 | List[Count++] = Start; | |
996 | if (Count >= ListMax) | |
997 | { | |
998 | List[Count-1] = 0; | |
999 | return false; | |
1000 | } | |
1001 | ||
1002 | // Advance pos | |
1003 | for (; Pos != Stop && (*Pos == Tok || isspace(*Pos) != 0 || *Pos == 0); Pos++); | |
1004 | Start = Pos; | |
1005 | } | |
1006 | ||
1007 | List[Count] = 0; | |
1008 | return true; | |
1009 | } | |
1010 | /*}}}*/ | |
1011 | // VectorizeString - Split a string up into a vector of strings /*{{{*/ | |
1012 | // --------------------------------------------------------------------- | |
1013 | /* This can be used to split a given string up into a vector, so the | |
1014 | propose is the same as in the method above and this one is a bit slower | |
1015 | also, but the advantage is that we have an iteratable vector */ | |
1016 | vector<string> VectorizeString(string const &haystack, char const &split) | |
1017 | { | |
1018 | string::const_iterator start = haystack.begin(); | |
1019 | string::const_iterator end = start; | |
1020 | vector<string> exploded; | |
1021 | do { | |
1022 | for (; end != haystack.end() && *end != split; ++end); | |
1023 | exploded.push_back(string(start, end)); | |
1024 | start = end + 1; | |
1025 | } while (end != haystack.end() && (++end) != haystack.end()); | |
1026 | return exploded; | |
1027 | } | |
1028 | /*}}}*/ | |
1029 | // RegexChoice - Simple regex list/list matcher /*{{{*/ | |
1030 | // --------------------------------------------------------------------- | |
1031 | /* */ | |
1032 | unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin, | |
1033 | const char **ListEnd) | |
1034 | { | |
1035 | for (RxChoiceList *R = Rxs; R->Str != 0; R++) | |
1036 | R->Hit = false; | |
1037 | ||
1038 | unsigned long Hits = 0; | |
1039 | for (; ListBegin != ListEnd; ListBegin++) | |
1040 | { | |
1041 | // Check if the name is a regex | |
1042 | const char *I; | |
1043 | bool Regex = true; | |
1044 | for (I = *ListBegin; *I != 0; I++) | |
1045 | if (*I == '.' || *I == '?' || *I == '*' || *I == '|') | |
1046 | break; | |
1047 | if (*I == 0) | |
1048 | Regex = false; | |
1049 | ||
1050 | // Compile the regex pattern | |
1051 | regex_t Pattern; | |
1052 | if (Regex == true) | |
1053 | if (regcomp(&Pattern,*ListBegin,REG_EXTENDED | REG_ICASE | | |
1054 | REG_NOSUB) != 0) | |
1055 | Regex = false; | |
1056 | ||
1057 | // Search the list | |
1058 | bool Done = false; | |
1059 | for (RxChoiceList *R = Rxs; R->Str != 0; R++) | |
1060 | { | |
1061 | if (R->Str[0] == 0) | |
1062 | continue; | |
1063 | ||
1064 | if (strcasecmp(R->Str,*ListBegin) != 0) | |
1065 | { | |
1066 | if (Regex == false) | |
1067 | continue; | |
1068 | if (regexec(&Pattern,R->Str,0,0,0) != 0) | |
1069 | continue; | |
1070 | } | |
1071 | Done = true; | |
1072 | ||
1073 | if (R->Hit == false) | |
1074 | Hits++; | |
1075 | ||
1076 | R->Hit = true; | |
1077 | } | |
1078 | ||
1079 | if (Regex == true) | |
1080 | regfree(&Pattern); | |
1081 | ||
1082 | if (Done == false) | |
1083 | _error->Warning(_("Selection %s not found"),*ListBegin); | |
1084 | } | |
1085 | ||
1086 | return Hits; | |
1087 | } | |
1088 | /*}}}*/ | |
1089 | // ioprintf - C format string outputter to C++ iostreams /*{{{*/ | |
1090 | // --------------------------------------------------------------------- | |
1091 | /* This is used to make the internationalization strings easier to translate | |
1092 | and to allow reordering of parameters */ | |
1093 | void ioprintf(ostream &out,const char *format,...) | |
1094 | { | |
1095 | va_list args; | |
1096 | va_start(args,format); | |
1097 | ||
1098 | // sprintf the description | |
1099 | char S[4096]; | |
1100 | vsnprintf(S,sizeof(S),format,args); | |
1101 | out << S; | |
1102 | } | |
1103 | /*}}}*/ | |
1104 | // strprintf - C format string outputter to C++ strings /*{{{*/ | |
1105 | // --------------------------------------------------------------------- | |
1106 | /* This is used to make the internationalization strings easier to translate | |
1107 | and to allow reordering of parameters */ | |
1108 | void strprintf(string &out,const char *format,...) | |
1109 | { | |
1110 | va_list args; | |
1111 | va_start(args,format); | |
1112 | ||
1113 | // sprintf the description | |
1114 | char S[4096]; | |
1115 | vsnprintf(S,sizeof(S),format,args); | |
1116 | out = string(S); | |
1117 | } | |
1118 | /*}}}*/ | |
1119 | // safe_snprintf - Safer snprintf /*{{{*/ | |
1120 | // --------------------------------------------------------------------- | |
1121 | /* This is a snprintf that will never (ever) go past 'End' and returns a | |
1122 | pointer to the end of the new string. The returned string is always null | |
1123 | terminated unless Buffer == end. This is a better alterantive to using | |
1124 | consecutive snprintfs. */ | |
1125 | char *safe_snprintf(char *Buffer,char *End,const char *Format,...) | |
1126 | { | |
1127 | va_list args; | |
1128 | unsigned long Did; | |
1129 | ||
1130 | va_start(args,Format); | |
1131 | ||
1132 | if (End <= Buffer) | |
1133 | return End; | |
1134 | ||
1135 | Did = vsnprintf(Buffer,End - Buffer,Format,args); | |
1136 | if (Did < 0 || Buffer + Did > End) | |
1137 | return End; | |
1138 | return Buffer + Did; | |
1139 | } | |
1140 | /*}}}*/ | |
1141 | ||
1142 | // tolower_ascii - tolower() function that ignores the locale /*{{{*/ | |
1143 | // --------------------------------------------------------------------- | |
1144 | /* This little function is the most called method we have and tries | |
1145 | therefore to do the absolut minimum - and is noteable faster than | |
1146 | standard tolower/toupper and as a bonus avoids problems with different | |
1147 | locales - we only operate on ascii chars anyway. */ | |
1148 | int tolower_ascii(int const c) | |
1149 | { | |
1150 | if (c >= 'A' && c <= 'Z') | |
1151 | return c + 32; | |
1152 | return c; | |
1153 | } | |
1154 | /*}}}*/ | |
1155 | ||
1156 | // CheckDomainList - See if Host is in a , seperate list /*{{{*/ | |
1157 | // --------------------------------------------------------------------- | |
1158 | /* The domain list is a comma seperate list of domains that are suffix | |
1159 | matched against the argument */ | |
1160 | bool CheckDomainList(const string &Host,const string &List) | |
1161 | { | |
1162 | string::const_iterator Start = List.begin(); | |
1163 | for (string::const_iterator Cur = List.begin(); Cur <= List.end(); Cur++) | |
1164 | { | |
1165 | if (Cur < List.end() && *Cur != ',') | |
1166 | continue; | |
1167 | ||
1168 | // Match the end of the string.. | |
1169 | if ((Host.size() >= (unsigned)(Cur - Start)) && | |
1170 | Cur - Start != 0 && | |
1171 | stringcasecmp(Host.end() - (Cur - Start),Host.end(),Start,Cur) == 0) | |
1172 | return true; | |
1173 | ||
1174 | Start = Cur + 1; | |
1175 | } | |
1176 | return false; | |
1177 | } | |
1178 | /*}}}*/ | |
1179 | ||
1180 | // URI::CopyFrom - Copy from an object /*{{{*/ | |
1181 | // --------------------------------------------------------------------- | |
1182 | /* This parses the URI into all of its components */ | |
1183 | void URI::CopyFrom(const string &U) | |
1184 | { | |
1185 | string::const_iterator I = U.begin(); | |
1186 | ||
1187 | // Locate the first colon, this separates the scheme | |
1188 | for (; I < U.end() && *I != ':' ; I++); | |
1189 | string::const_iterator FirstColon = I; | |
1190 | ||
1191 | /* Determine if this is a host type URI with a leading double // | |
1192 | and then search for the first single / */ | |
1193 | string::const_iterator SingleSlash = I; | |
1194 | if (I + 3 < U.end() && I[1] == '/' && I[2] == '/') | |
1195 | SingleSlash += 3; | |
1196 | ||
1197 | /* Find the / indicating the end of the hostname, ignoring /'s in the | |
1198 | square brackets */ | |
1199 | bool InBracket = false; | |
1200 | for (; SingleSlash < U.end() && (*SingleSlash != '/' || InBracket == true); SingleSlash++) | |
1201 | { | |
1202 | if (*SingleSlash == '[') | |
1203 | InBracket = true; | |
1204 | if (InBracket == true && *SingleSlash == ']') | |
1205 | InBracket = false; | |
1206 | } | |
1207 | ||
1208 | if (SingleSlash > U.end()) | |
1209 | SingleSlash = U.end(); | |
1210 | ||
1211 | // We can now write the access and path specifiers | |
1212 | Access.assign(U.begin(),FirstColon); | |
1213 | if (SingleSlash != U.end()) | |
1214 | Path.assign(SingleSlash,U.end()); | |
1215 | if (Path.empty() == true) | |
1216 | Path = "/"; | |
1217 | ||
1218 | // Now we attempt to locate a user:pass@host fragment | |
1219 | if (FirstColon + 2 <= U.end() && FirstColon[1] == '/' && FirstColon[2] == '/') | |
1220 | FirstColon += 3; | |
1221 | else | |
1222 | FirstColon += 1; | |
1223 | if (FirstColon >= U.end()) | |
1224 | return; | |
1225 | ||
1226 | if (FirstColon > SingleSlash) | |
1227 | FirstColon = SingleSlash; | |
1228 | ||
1229 | // Find the colon... | |
1230 | I = FirstColon + 1; | |
1231 | if (I > SingleSlash) | |
1232 | I = SingleSlash; | |
1233 | for (; I < SingleSlash && *I != ':'; I++); | |
1234 | string::const_iterator SecondColon = I; | |
1235 | ||
1236 | // Search for the @ after the colon | |
1237 | for (; I < SingleSlash && *I != '@'; I++); | |
1238 | string::const_iterator At = I; | |
1239 | ||
1240 | // Now write the host and user/pass | |
1241 | if (At == SingleSlash) | |
1242 | { | |
1243 | if (FirstColon < SingleSlash) | |
1244 | Host.assign(FirstColon,SingleSlash); | |
1245 | } | |
1246 | else | |
1247 | { | |
1248 | Host.assign(At+1,SingleSlash); | |
1249 | // username and password must be encoded (RFC 3986) | |
1250 | User.assign(DeQuoteString(FirstColon,SecondColon)); | |
1251 | if (SecondColon < At) | |
1252 | Password.assign(DeQuoteString(SecondColon+1,At)); | |
1253 | } | |
1254 | ||
1255 | // Now we parse the RFC 2732 [] hostnames. | |
1256 | unsigned long PortEnd = 0; | |
1257 | InBracket = false; | |
1258 | for (unsigned I = 0; I != Host.length();) | |
1259 | { | |
1260 | if (Host[I] == '[') | |
1261 | { | |
1262 | InBracket = true; | |
1263 | Host.erase(I,1); | |
1264 | continue; | |
1265 | } | |
1266 | ||
1267 | if (InBracket == true && Host[I] == ']') | |
1268 | { | |
1269 | InBracket = false; | |
1270 | Host.erase(I,1); | |
1271 | PortEnd = I; | |
1272 | continue; | |
1273 | } | |
1274 | I++; | |
1275 | } | |
1276 | ||
1277 | // Tsk, weird. | |
1278 | if (InBracket == true) | |
1279 | { | |
1280 | Host.clear(); | |
1281 | return; | |
1282 | } | |
1283 | ||
1284 | // Now we parse off a port number from the hostname | |
1285 | Port = 0; | |
1286 | string::size_type Pos = Host.rfind(':'); | |
1287 | if (Pos == string::npos || Pos < PortEnd) | |
1288 | return; | |
1289 | ||
1290 | Port = atoi(string(Host,Pos+1).c_str()); | |
1291 | Host.assign(Host,0,Pos); | |
1292 | } | |
1293 | /*}}}*/ | |
1294 | // URI::operator string - Convert the URI to a string /*{{{*/ | |
1295 | // --------------------------------------------------------------------- | |
1296 | /* */ | |
1297 | URI::operator string() | |
1298 | { | |
1299 | string Res; | |
1300 | ||
1301 | if (Access.empty() == false) | |
1302 | Res = Access + ':'; | |
1303 | ||
1304 | if (Host.empty() == false) | |
1305 | { | |
1306 | if (Access.empty() == false) | |
1307 | Res += "//"; | |
1308 | ||
1309 | if (User.empty() == false) | |
1310 | { | |
1311 | Res += User; | |
1312 | if (Password.empty() == false) | |
1313 | Res += ":" + Password; | |
1314 | Res += "@"; | |
1315 | } | |
1316 | ||
1317 | // Add RFC 2732 escaping characters | |
1318 | if (Access.empty() == false && | |
1319 | (Host.find('/') != string::npos || Host.find(':') != string::npos)) | |
1320 | Res += '[' + Host + ']'; | |
1321 | else | |
1322 | Res += Host; | |
1323 | ||
1324 | if (Port != 0) | |
1325 | { | |
1326 | char S[30]; | |
1327 | sprintf(S,":%u",Port); | |
1328 | Res += S; | |
1329 | } | |
1330 | } | |
1331 | ||
1332 | if (Path.empty() == false) | |
1333 | { | |
1334 | if (Path[0] != '/') | |
1335 | Res += "/" + Path; | |
1336 | else | |
1337 | Res += Path; | |
1338 | } | |
1339 | ||
1340 | return Res; | |
1341 | } | |
1342 | /*}}}*/ | |
1343 | // URI::SiteOnly - Return the schema and site for the URI /*{{{*/ | |
1344 | // --------------------------------------------------------------------- | |
1345 | /* */ | |
1346 | string URI::SiteOnly(const string &URI) | |
1347 | { | |
1348 | ::URI U(URI); | |
1349 | U.User.clear(); | |
1350 | U.Password.clear(); | |
1351 | U.Path.clear(); | |
1352 | U.Port = 0; | |
1353 | return U; | |
1354 | } | |
1355 | /*}}}*/ | |
1356 | // URI::NoUserPassword - Return the schema, site and path for the URI /*{{{*/ | |
1357 | // --------------------------------------------------------------------- | |
1358 | /* */ | |
1359 | string URI::NoUserPassword(const string &URI) | |
1360 | { | |
1361 | ::URI U(URI); | |
1362 | U.User.clear(); | |
1363 | U.Password.clear(); | |
1364 | U.Port = 0; | |
1365 | return U; | |
1366 | } | |
1367 | /*}}}*/ |