]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/strutl.cc
Made NFS locking bail out
[apt.git] / apt-pkg / contrib / strutl.cc
CommitLineData
6c139d6e
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
7049d16d 3// $Id: strutl.cc,v 1.23 1999/03/16 07:27:17 jgg Exp $
6c139d6e
AL
4/* ######################################################################
5
6 String Util - Some usefull string functions.
7
24231681
AL
8 These have been collected from here and there to do all sorts of usefull
9 things to strings. They are usefull in file parsers, URI handlers and
10 especially in APT methods.
6c139d6e
AL
11
12 This source is placed in the Public Domain, do with it what you will
24231681 13 It was originally written by Jason Gunthorpe <jgg@gpu.srv.ualberta.ca>
6c139d6e
AL
14
15 ##################################################################### */
16 /*}}}*/
17// Includes /*{{{*/
492f957a 18#ifdef __GNUG__
cdcc6d34 19#pragma implementation "apt-pkg/strutl.h"
492f957a
AL
20#endif
21
cdcc6d34 22#include <apt-pkg/strutl.h>
7049d16d 23#include <apt-pkg/fileutl.h>
0a8a80e5 24
6c139d6e
AL
25#include <ctype.h>
26#include <string.h>
27#include <stdio.h>
2b154e53 28#include <unistd.h>
b0db36b1 29#include <errno.h>
6c139d6e
AL
30 /*}}}*/
31
32// strstrip - Remove white space from the front and back of a string /*{{{*/
33// ---------------------------------------------------------------------
34/* This is handy to use when parsing a file. It also removes \n's left
35 over from fgets and company */
36char *_strstrip(char *String)
37{
38 for (;*String != 0 && (*String == ' ' || *String == '\t'); String++);
39
40 if (*String == 0)
41 return String;
42
43 char *End = String + strlen(String) - 1;
44 for (;End != String - 1 && (*End == ' ' || *End == '\t' || *End == '\n' ||
45 *End == '\r'); End--);
46 End++;
47 *End = 0;
48 return String;
49};
50 /*}}}*/
51// strtabexpand - Converts tabs into 8 spaces /*{{{*/
52// ---------------------------------------------------------------------
53/* */
54char *_strtabexpand(char *String,size_t Len)
55{
56 for (char *I = String; I != I + Len && *I != 0; I++)
57 {
58 if (*I != '\t')
59 continue;
60 if (I + 8 > String + Len)
61 {
62 *I = 0;
63 return String;
64 }
65
66 /* Assume the start of the string is 0 and find the next 8 char
67 division */
68 int Len;
69 if (String == I)
70 Len = 1;
71 else
72 Len = 8 - ((String - I) % 8);
73 Len -= 2;
74 if (Len <= 0)
75 {
76 *I = ' ';
77 continue;
78 }
79
80 memmove(I + Len,I + 1,strlen(I) + 1);
81 for (char *J = I; J + Len != I; *I = ' ', I++);
82 }
83 return String;
84}
85 /*}}}*/
86// ParseQuoteWord - Parse a single word out of a string /*{{{*/
87// ---------------------------------------------------------------------
88/* This grabs a single word, converts any % escaped characters to their
89 proper values and advances the pointer. Double quotes are understood
08e8f724 90 and striped out as well. This is for URI/URL parsing. */
6c139d6e
AL
91bool ParseQuoteWord(const char *&String,string &Res)
92{
93 // Skip leading whitespace
94 const char *C = String;
95 for (;*C != 0 && *C == ' '; C++);
96 if (*C == 0)
97 return false;
98
99 // Jump to the next word
100 for (;*C != 0 && *C != ' '; C++)
101 {
102 if (*C == '"')
103 {
104 for (C++;*C != 0 && *C != '"'; C++);
105 if (*C == 0)
106 return false;
107 }
108 }
109
110 // Now de-quote characters
111 char Buffer[1024];
112 char Tmp[3];
113 const char *Start = String;
114 char *I;
115 for (I = Buffer; I < Buffer + sizeof(Buffer) && Start != C; I++)
116 {
117 if (*Start == '%' && Start + 2 < C)
118 {
119 Tmp[0] = Start[1];
120 Tmp[1] = Start[2];
1bc849af 121 Tmp[2] = 0;
6c139d6e
AL
122 *I = (char)strtol(Tmp,0,16);
123 Start += 3;
124 continue;
125 }
126 if (*Start != '"')
127 *I = *Start;
128 else
129 I--;
130 Start++;
131 }
132 *I = 0;
133 Res = Buffer;
134
135 // Skip ending white space
136 for (;*C != 0 && *C == ' '; C++);
137 String = C;
138 return true;
139}
140 /*}}}*/
08e8f724
AL
141// ParseCWord - Parses a string like a C "" expression /*{{{*/
142// ---------------------------------------------------------------------
143/* This expects a series of space seperated strings enclosed in ""'s.
144 It concatenates the ""'s into a single string. */
145bool ParseCWord(const char *String,string &Res)
146{
147 // Skip leading whitespace
148 const char *C = String;
149 for (;*C != 0 && *C == ' '; C++);
150 if (*C == 0)
151 return false;
152
153 char Buffer[1024];
154 char *Buf = Buffer;
155 if (strlen(String) >= sizeof(Buffer))
156 return false;
157
158 for (; *C != 0; C++)
159 {
160 if (*C == '"')
161 {
162 for (C++; *C != 0 && *C != '"'; C++)
163 *Buf++ = *C;
164
165 if (*C == 0)
166 return false;
167
168 continue;
169 }
170
171 if (C != String && isspace(*C) != 0 && isspace(C[-1]) != 0)
172 continue;
173 if (isspace(*C) == 0)
174 return false;
175 *Buf++ = ' ';
176 }
177 *Buf = 0;
178 Res = Buffer;
179 return true;
180}
181 /*}}}*/
6d5dd02a 182// QuoteString - Convert a string into quoted from /*{{{*/
1bc849af 183// ---------------------------------------------------------------------
6d5dd02a
AL
184/* */
185string QuoteString(string Str,const char *Bad)
1bc849af
AL
186{
187 string Res;
188 for (string::iterator I = Str.begin(); I != Str.end(); I++)
189 {
6d5dd02a
AL
190 if (strchr(Bad,*I) != 0 || isprint(*I) == 0 ||
191 *I <= 0x20 || *I >= 0x7F)
1bc849af 192 {
6d5dd02a
AL
193 char Buf[10];
194 sprintf(Buf,"%%%02x",(int)*I);
195 Res += Buf;
1bc849af
AL
196 }
197 else
198 Res += *I;
199 }
200 return Res;
201}
202 /*}}}*/
6d5dd02a 203// DeQuoteString - Convert a string from quoted from /*{{{*/
6c139d6e 204// ---------------------------------------------------------------------
6d5dd02a
AL
205/* This undoes QuoteString */
206string DeQuoteString(string Str)
6c139d6e
AL
207{
208 string Res;
209 for (string::iterator I = Str.begin(); I != Str.end(); I++)
210 {
6d5dd02a 211 if (*I == '%' && I + 2 < Str.end())
6c139d6e 212 {
6d5dd02a
AL
213 char Tmp[3];
214 Tmp[0] = I[1];
215 Tmp[1] = I[2];
216 Tmp[2] = 0;
217 Res += (char)strtol(Tmp,0,16);
218 I += 2;
219 continue;
6c139d6e
AL
220 }
221 else
222 Res += *I;
223 }
6d5dd02a 224 return Res;
6c139d6e 225}
6d5dd02a
AL
226
227 /*}}}*/
6c139d6e
AL
228// SizeToStr - Convert a long into a human readable size /*{{{*/
229// ---------------------------------------------------------------------
24231681
AL
230/* A max of 4 digits are shown before conversion to the next highest unit.
231 The max length of the string will be 5 chars unless the size is > 10
6c139d6e
AL
232 YottaBytes (E24) */
233string SizeToStr(double Size)
234{
235 char S[300];
236 double ASize;
237 if (Size >= 0)
238 ASize = Size;
239 else
240 ASize = -1*Size;
241
242 /* bytes, KiloBytes, MegaBytes, GigaBytes, TeraBytes, PetaBytes,
243 ExaBytes, ZettaBytes, YottaBytes */
7f25bdff 244 char Ext[] = {'\0','k','M','G','T','P','E','Z','Y'};
6c139d6e
AL
245 int I = 0;
246 while (I <= 8)
247 {
248 if (ASize < 100 && I != 0)
249 {
250 sprintf(S,"%.1f%c",ASize,Ext[I]);
251 break;
252 }
253
254 if (ASize < 10000)
255 {
256 sprintf(S,"%.0f%c",ASize,Ext[I]);
257 break;
258 }
259 ASize /= 1000.0;
260 I++;
261 }
262
263 return S;
264}
265 /*}}}*/
266// TimeToStr - Convert the time into a string /*{{{*/
267// ---------------------------------------------------------------------
268/* Converts a number of seconds to a hms format */
269string TimeToStr(unsigned long Sec)
270{
271 char S[300];
272
273 while (1)
274 {
275 if (Sec > 60*60*24)
276 {
277 sprintf(S,"%lid %lih%lim%lis",Sec/60/60/24,(Sec/60/60) % 24,(Sec/60) % 60,Sec % 60);
278 break;
279 }
280
281 if (Sec > 60*60)
282 {
283 sprintf(S,"%lih%lim%lis",Sec/60/60,(Sec/60) % 60,Sec % 60);
284 break;
285 }
286
287 if (Sec > 60)
288 {
289 sprintf(S,"%lim%lis",Sec/60,Sec % 60);
290 break;
291 }
292
293 sprintf(S,"%lis",Sec);
294 break;
295 }
296
297 return S;
298}
299 /*}}}*/
300// SubstVar - Substitute a string for another string /*{{{*/
301// ---------------------------------------------------------------------
302/* This replaces all occurances of Subst with Contents in Str. */
303string SubstVar(string Str,string Subst,string Contents)
304{
8efa2a3b 305 string::size_type Pos = 0;
6c139d6e
AL
306 string::size_type OldPos = 0;
307 string Temp;
308
309 while (OldPos < Str.length() &&
310 (Pos = Str.find(Subst,OldPos)) != string::npos)
311 {
312 Temp += string(Str,OldPos,Pos) + Contents;
313 OldPos = Pos + Subst.length();
314 }
315
316 if (OldPos == 0)
317 return Str;
318
319 return Temp + string(Str,OldPos);
320}
321 /*}}}*/
ad00ae81
AL
322// URItoFileName - Convert the uri into a unique file name /*{{{*/
323// ---------------------------------------------------------------------
324/* This converts a URI into a safe filename. It quotes all unsafe characters
325 and converts / to _ and removes the scheme identifier. The resulting
326 file name should be unique and never occur again for a different file */
327string URItoFileName(string URI)
328{
329 string::const_iterator I = URI.begin() + URI.find(':') + 1;
330 for (; I < URI.end() && *I == '/'; I++);
331
332 // "\x00-\x20{}|\\\\^\\[\\]<>\"\x7F-\xFF";
333 URI = QuoteString(string(I,URI.end() - I),"\\|{}[]<>\"^~_=!@#$%^&*");
334 string::iterator J = URI.begin();
335 for (; J != URI.end(); J++)
336 if (*J == '/')
337 *J = '_';
338 return URI;
339}
340 /*}}}*/
6c139d6e
AL
341// Base64Encode - Base64 Encoding routine for short strings /*{{{*/
342// ---------------------------------------------------------------------
343/* This routine performs a base64 transformation on a string. It was ripped
344 from wget and then patched and bug fixed.
345
346 This spec can be found in rfc2045 */
347string Base64Encode(string S)
348{
349 // Conversion table.
350 static char tbl[64] = {'A','B','C','D','E','F','G','H',
351 'I','J','K','L','M','N','O','P',
352 'Q','R','S','T','U','V','W','X',
353 'Y','Z','a','b','c','d','e','f',
354 'g','h','i','j','k','l','m','n',
355 'o','p','q','r','s','t','u','v',
356 'w','x','y','z','0','1','2','3',
357 '4','5','6','7','8','9','+','/'};
358
359 // Pre-allocate some space
360 string Final;
361 Final.reserve((4*S.length() + 2)/3 + 2);
362
363 /* Transform the 3x8 bits to 4x6 bits, as required by
364 base64. */
365 for (string::const_iterator I = S.begin(); I < S.end(); I += 3)
366 {
367 char Bits[3] = {0,0,0};
368 Bits[0] = I[0];
369 if (I + 1 < S.end())
370 Bits[1] = I[1];
371 if (I + 2 < S.end())
372 Bits[2] = I[2];
373
374 Final += tbl[Bits[0] >> 2];
375 Final += tbl[((Bits[0] & 3) << 4) + (Bits[1] >> 4)];
376
377 if (I + 1 >= S.end())
378 break;
379
380 Final += tbl[((Bits[1] & 0xf) << 2) + (Bits[2] >> 6)];
381
382 if (I + 2 >= S.end())
383 break;
384
385 Final += tbl[Bits[2] & 0x3f];
386 }
387
388 /* Apply the padding elements, this tells how many bytes the remote
389 end should discard */
390 if (S.length() % 3 == 2)
391 Final += '=';
392 if (S.length() % 3 == 1)
393 Final += "==";
394
395 return Final;
396}
397 /*}}}*/
398// stringcmp - Arbitary string compare /*{{{*/
399// ---------------------------------------------------------------------
400/* This safely compares two non-null terminated strings of arbitary
401 length */
402int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd)
403{
404 for (; A != AEnd && B != BEnd; A++, B++)
405 if (*A != *B)
406 break;
407
408 if (A == AEnd && B == BEnd)
409 return 0;
410 if (A == AEnd)
411 return 1;
412 if (B == BEnd)
413 return -1;
414 if (*A < *B)
415 return -1;
416 return 1;
417}
418 /*}}}*/
419// stringcasecmp - Arbitary case insensitive string compare /*{{{*/
420// ---------------------------------------------------------------------
421/* */
422int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd)
423{
424 for (; A != AEnd && B != BEnd; A++, B++)
425 if (toupper(*A) != toupper(*B))
426 break;
3b5421b4 427
6c139d6e
AL
428 if (A == AEnd && B == BEnd)
429 return 0;
430 if (A == AEnd)
431 return 1;
432 if (B == BEnd)
433 return -1;
434 if (toupper(*A) < toupper(*B))
435 return -1;
436 return 1;
437}
438 /*}}}*/
3b5421b4
AL
439// LookupTag - Lookup the value of a tag in a taged string /*{{{*/
440// ---------------------------------------------------------------------
441/* The format is like those used in package files and the method
442 communication system */
443string LookupTag(string Message,const char *Tag,const char *Default)
444{
445 // Look for a matching tag.
446 int Length = strlen(Tag);
447 for (string::iterator I = Message.begin(); I + Length < Message.end(); I++)
448 {
449 // Found the tag
450 if (I[Length] == ':' && stringcasecmp(I,I+Length,Tag) == 0)
451 {
452 // Find the end of line and strip the leading/trailing spaces
453 string::iterator J;
454 I += Length + 1;
455 for (; isspace(*I) != 0 && I < Message.end(); I++);
456 for (J = I; *J != '\n' && J < Message.end(); J++);
457 for (; J > I && isspace(J[-1]) != 0; J--);
458
459 return string(I,J-I);
460 }
461
462 for (; *I != '\n' && I < Message.end(); I++);
463 }
464
465 // Failed to find a match
466 if (Default == 0)
467 return string();
468 return Default;
469}
470 /*}}}*/
471// StringToBool - Converts a string into a boolean /*{{{*/
472// ---------------------------------------------------------------------
473/* This inspects the string to see if it is true or if it is false and
474 then returns the result. Several varients on true/false are checked. */
475int StringToBool(string Text,int Default = -1)
476{
477 char *End;
478 int Res = strtol(Text.c_str(),&End,0);
479 if (End != Text.c_str() && Res >= 0 && Res <= 1)
480 return Res;
481
482 // Check for positives
483 if (strcasecmp(Text.c_str(),"no") == 0 ||
484 strcasecmp(Text.c_str(),"false") == 0 ||
485 strcasecmp(Text.c_str(),"without") == 0 ||
7f25bdff 486 strcasecmp(Text.c_str(),"off") == 0 ||
3b5421b4
AL
487 strcasecmp(Text.c_str(),"disable") == 0)
488 return 0;
489
490 // Check for negatives
491 if (strcasecmp(Text.c_str(),"yes") == 0 ||
492 strcasecmp(Text.c_str(),"true") == 0 ||
493 strcasecmp(Text.c_str(),"with") == 0 ||
7f25bdff 494 strcasecmp(Text.c_str(),"on") == 0 ||
3b5421b4
AL
495 strcasecmp(Text.c_str(),"enable") == 0)
496 return 1;
497
498 return Default;
499}
500 /*}}}*/
0a8a80e5
AL
501// TimeRFC1123 - Convert a time_t into RFC1123 format /*{{{*/
502// ---------------------------------------------------------------------
503/* This converts a time_t into a string time representation that is
504 year 2000 complient and timezone neutral */
505string TimeRFC1123(time_t Date)
506{
507 struct tm Conv = *gmtime(&Date);
508 char Buf[300];
509
510 const char *Day[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
511 const char *Month[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul",
512 "Aug","Sep","Oct","Nov","Dec"};
513
514 sprintf(Buf,"%s, %02i %s %i %02i:%02i:%02i GMT",Day[Conv.tm_wday],
515 Conv.tm_mday,Month[Conv.tm_mon],Conv.tm_year+1900,Conv.tm_hour,
516 Conv.tm_min,Conv.tm_sec);
517 return Buf;
518}
519 /*}}}*/
520// ReadMessages - Read messages from the FD /*{{{*/
521// ---------------------------------------------------------------------
522/* This pulls full messages from the input FD into the message buffer.
523 It assumes that messages will not pause during transit so no
524 fancy buffering is used. */
525bool ReadMessages(int Fd, vector<string> &List)
526{
527 char Buffer[4000];
528 char *End = Buffer;
529
530 while (1)
531 {
532 int Res = read(Fd,End,sizeof(Buffer) - (End-Buffer));
b0db36b1
AL
533 if (Res < 0 && errno == EINTR)
534 continue;
0a8a80e5
AL
535
536 // Process is dead, this is kind of bad..
537 if (Res == 0)
538 return false;
539
540 // No data
541 if (Res <= 0)
542 return true;
543
544 End += Res;
545
546 // Look for the end of the message
c88edf1d 547 for (char *I = Buffer; I + 1 < End; I++)
0a8a80e5
AL
548 {
549 if (I[0] != '\n' || I[1] != '\n')
550 continue;
551
552 // Pull the message out
553 string Message(Buffer,0,I-Buffer);
554
555 // Fix up the buffer
556 for (; I < End && *I == '\n'; I++);
557 End -= I-Buffer;
558 memmove(Buffer,I,End-Buffer);
559 I = Buffer;
560
561 List.push_back(Message);
562 }
563 if (End == Buffer)
564 return true;
565
566 if (WaitFd(Fd) == false)
567 return false;
568 }
569}
570 /*}}}*/
24231681
AL
571// MonthConv - Converts a month string into a number /*{{{*/
572// ---------------------------------------------------------------------
573/* This was lifted from the boa webserver which lifted it from 'wn-v1.07'
574 Made it a bit more robust with a few touppers though. */
575static int MonthConv(char *Month)
576{
577 switch (toupper(*Month))
578 {
579 case 'A':
580 return toupper(Month[1]) == 'P'?3:7;
581 case 'D':
582 return 11;
583 case 'F':
584 return 1;
585 case 'J':
586 if (toupper(Month[1]) == 'A')
587 return 0;
588 return toupper(Month[2]) == 'N'?5:6;
589 case 'M':
590 return toupper(Month[2]) == 'R'?2:4;
591 case 'N':
592 return 10;
593 case 'O':
594 return 9;
595 case 'S':
596 return 8;
597
598 // Pretend it is January..
599 default:
600 return 0;
601 }
602}
603 /*}}}*/
6d5dd02a
AL
604// timegm - Internal timegm function if gnu is not available /*{{{*/
605// ---------------------------------------------------------------------
606/* Ripped this evil little function from wget - I prefer the use of
607 GNU timegm if possible as this technique will have interesting problems
608 with leap seconds, timezones and other.
609
610 Converts struct tm to time_t, assuming the data in tm is UTC rather
611 than local timezone (mktime assumes the latter).
612
613 Contributed by Roger Beeman <beeman@cisco.com>, with the help of
614 Mark Baushke <mdb@cisco.com> and the rest of the Gurus at CISCO. */
615#ifndef __USE_MISC // glib sets this
616static time_t timegm(struct tm *t)
617{
618 time_t tl, tb;
619
620 tl = mktime (t);
621 if (tl == -1)
622 return -1;
623 tb = mktime (gmtime (&tl));
624 return (tl <= tb ? (tl + (tl - tb)) : (tl - (tb - tl)));
625}
626#endif
627 /*}}}*/
24231681
AL
628// StrToTime - Converts a string into a time_t /*{{{*/
629// ---------------------------------------------------------------------
630/* This handles all 3 populare time formats including RFC 1123, RFC 1036
631 and the C library asctime format. It requires the GNU library function
632 'timegm' to convert a struct tm in UTC to a time_t. For some bizzar
633 reason the C library does not provide any such function :<*/
634bool StrToTime(string Val,time_t &Result)
635{
636 struct tm Tm;
637 char Month[10];
638 const char *I = Val.c_str();
639
640 // Skip the day of the week
641 for (;*I != 0 && *I != ' '; I++);
642
643 // Handle RFC 1123 time
644 if (sscanf(I," %d %3s %d %d:%d:%d GMT",&Tm.tm_mday,Month,&Tm.tm_year,
645 &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) != 6)
646 {
647 // Handle RFC 1036 time
648 if (sscanf(I," %d-%3s-%d %d:%d:%d GMT",&Tm.tm_mday,Month,
649 &Tm.tm_year,&Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) == 6)
650 Tm.tm_year += 1900;
651 else
652 {
653 // asctime format
654 if (sscanf(I," %3s %d %d:%d:%d %d",Month,&Tm.tm_mday,
655 &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec,&Tm.tm_year) != 6)
656 return false;
657 }
658 }
659
660 Tm.tm_isdst = 0;
661 Tm.tm_mon = MonthConv(Month);
662 Tm.tm_year -= 1900;
663
664 // Convert to local time and then to GMT
665 Result = timegm(&Tm);
666 return true;
667}
668 /*}}}*/
93bf083d 669
be4401bf 670// URI::CopyFrom - Copy from an object /*{{{*/
93bf083d
AL
671// ---------------------------------------------------------------------
672/* This parses the URI into all of its components */
be4401bf 673void URI::CopyFrom(string U)
93bf083d
AL
674{
675 string::const_iterator I = U.begin();
676
677 // Locate the first colon, this seperates the scheme
678 for (; I < U.end() && *I != ':' ; I++);
679 string::const_iterator FirstColon = I;
680
bfd22fc0
AL
681 /* Determine if this is a host type URI with a leading double //
682 and then search for the first single / */
93bf083d
AL
683 string::const_iterator SingleSlash = I;
684 if (I + 3 < U.end() && I[1] == '/' && I[2] == '/')
bfd22fc0
AL
685 SingleSlash += 3;
686 for (; SingleSlash < U.end() && *SingleSlash != '/'; SingleSlash++);
687 if (SingleSlash > U.end())
f46e7681 688 SingleSlash = U.end();
93bf083d
AL
689
690 // We can now write the access and path specifiers
691 Access = string(U,0,FirstColon - U.begin());
692 if (SingleSlash != U.end())
92e889c8
AL
693 Path = string(U,SingleSlash - U.begin());
694 if (Path.empty() == true)
695 Path = "/";
696
93bf083d 697 // Now we attempt to locate a user:pass@host fragment
542ec555 698 if (FirstColon[1] == '/' && FirstColon[2] == '/')
f46e7681
AL
699 FirstColon += 3;
700 else
701 FirstColon += 1;
93bf083d
AL
702 if (FirstColon >= U.end())
703 return;
704
705 if (FirstColon > SingleSlash)
706 FirstColon = SingleSlash;
707
708 // Search for the @
709 I = FirstColon;
710 for (; I < SingleSlash && *I != '@'; I++);
711 string::const_iterator At = I;
712
713 // Colon in the @ section
714 I = FirstColon + 1;
715 for (; I < At && *I != ':'; I++);
716 string::const_iterator SecondColon = I;
717
718 // Now write the host and user/pass
719 if (At == SingleSlash)
720 {
721 if (FirstColon < SingleSlash)
722 Host = string(U,FirstColon - U.begin(),SingleSlash - FirstColon);
723 }
724 else
725 {
726 Host = string(U,At - U.begin() + 1,SingleSlash - At - 1);
727 User = string(U,FirstColon - U.begin(),SecondColon - FirstColon);
728 if (SecondColon < At)
729 Password = string(U,SecondColon - U.begin() + 1,At - SecondColon - 1);
730 }
731
732 // Now we parse off a pot number from the hostname
733 Port = 0;
734 string::size_type Pos = Host.rfind(':');
735 if (Pos == string::npos)
736 return;
737
738 Port = atoi(string(Host,Pos+1).c_str());
739 Host = string(Host,0,Pos);
740}
741 /*}}}*/
742// URI::operator string - Convert the URI to a string /*{{{*/
743// ---------------------------------------------------------------------
744/* */
745URI::operator string()
746{
747 string Res = Access + ':';
748 if (Host.empty() == false)
749 {
492f957a 750 Res += "//";
93bf083d
AL
751 if (User.empty() == false)
752 {
753 Res += "//" + User;
754 if (Password.empty() == false)
755 Res += ":" + Password;
756 Res += "@";
757 }
758 Res += Host;
492f957a
AL
759 if (Port != 0)
760 {
761 char S[30];
762 sprintf(S,":%u",Port);
763 Res += S;
764 }
93bf083d
AL
765 }
766
767 if (Path.empty() == false)
492f957a
AL
768 {
769 if (Path[0] != '/')
770 Res += "/" + Path;
771 else
772 Res += Path;
773 }
93bf083d
AL
774
775 return Res;
776}
777 /*}}}*/