strutl.cc 19 KB

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