strutl.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: strutl.cc,v 1.19 1999/02/01 08:11:57 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 "apt-pkg/strutl.h"
  16. #endif
  17. #include <apt-pkg/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[2] = 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. // DeQuoteString - Convert a string from quoted from /*{{{*/
  162. // ---------------------------------------------------------------------
  163. /* This undoes QuoteString */
  164. string DeQuoteString(string Str)
  165. {
  166. string Res;
  167. for (string::iterator I = Str.begin(); I != Str.end(); I++)
  168. {
  169. if (*I == '%' && I + 2 < Str.end())
  170. {
  171. char Tmp[3];
  172. Tmp[0] = I[1];
  173. Tmp[1] = I[2];
  174. Tmp[2] = 0;
  175. Res += (char)strtol(Tmp,0,16);
  176. I += 2;
  177. continue;
  178. }
  179. else
  180. Res += *I;
  181. }
  182. return Res;
  183. }
  184. /*}}}*/
  185. // QuoteString - Convert a string into quoted from /*{{{*/
  186. // ---------------------------------------------------------------------
  187. /* */
  188. string QuoteString(string Str,const char *Bad)
  189. {
  190. string Res;
  191. for (string::iterator I = Str.begin(); I != Str.end(); I++)
  192. {
  193. if (strchr(Bad,*I) != 0 || isprint(*I) == 0 ||
  194. *I <= 0x20 || *I >= 0x7F)
  195. {
  196. char Buf[10];
  197. sprintf(Buf,"%%%02x",(int)*I);
  198. Res += Buf;
  199. }
  200. else
  201. Res += *I;
  202. }
  203. return Res;
  204. }
  205. /*}}}*/
  206. // SizeToStr - Convert a long into a human readable size /*{{{*/
  207. // ---------------------------------------------------------------------
  208. /* A max of 4 digits are shown before conversion to the next highest unit.
  209. The max length of the string will be 5 chars unless the size is > 10
  210. YottaBytes (E24) */
  211. string SizeToStr(double Size)
  212. {
  213. char S[300];
  214. double ASize;
  215. if (Size >= 0)
  216. ASize = Size;
  217. else
  218. ASize = -1*Size;
  219. /* bytes, KiloBytes, MegaBytes, GigaBytes, TeraBytes, PetaBytes,
  220. ExaBytes, ZettaBytes, YottaBytes */
  221. char Ext[] = {'\0','k','M','G','T','P','E','Z','Y'};
  222. int I = 0;
  223. while (I <= 8)
  224. {
  225. if (ASize < 100 && I != 0)
  226. {
  227. sprintf(S,"%.1f%c",ASize,Ext[I]);
  228. break;
  229. }
  230. if (ASize < 10000)
  231. {
  232. sprintf(S,"%.0f%c",ASize,Ext[I]);
  233. break;
  234. }
  235. ASize /= 1000.0;
  236. I++;
  237. }
  238. return S;
  239. }
  240. /*}}}*/
  241. // TimeToStr - Convert the time into a string /*{{{*/
  242. // ---------------------------------------------------------------------
  243. /* Converts a number of seconds to a hms format */
  244. string TimeToStr(unsigned long Sec)
  245. {
  246. char S[300];
  247. while (1)
  248. {
  249. if (Sec > 60*60*24)
  250. {
  251. sprintf(S,"%lid %lih%lim%lis",Sec/60/60/24,(Sec/60/60) % 24,(Sec/60) % 60,Sec % 60);
  252. break;
  253. }
  254. if (Sec > 60*60)
  255. {
  256. sprintf(S,"%lih%lim%lis",Sec/60/60,(Sec/60) % 60,Sec % 60);
  257. break;
  258. }
  259. if (Sec > 60)
  260. {
  261. sprintf(S,"%lim%lis",Sec/60,Sec % 60);
  262. break;
  263. }
  264. sprintf(S,"%lis",Sec);
  265. break;
  266. }
  267. return S;
  268. }
  269. /*}}}*/
  270. // SubstVar - Substitute a string for another string /*{{{*/
  271. // ---------------------------------------------------------------------
  272. /* This replaces all occurances of Subst with Contents in Str. */
  273. string SubstVar(string Str,string Subst,string Contents)
  274. {
  275. string::size_type Pos = 0;
  276. string::size_type OldPos = 0;
  277. string Temp;
  278. while (OldPos < Str.length() &&
  279. (Pos = Str.find(Subst,OldPos)) != string::npos)
  280. {
  281. Temp += string(Str,OldPos,Pos) + Contents;
  282. OldPos = Pos + Subst.length();
  283. }
  284. if (OldPos == 0)
  285. return Str;
  286. return Temp + string(Str,OldPos);
  287. }
  288. /*}}}*/
  289. // URItoFileName - Convert the uri into a unique file name /*{{{*/
  290. // ---------------------------------------------------------------------
  291. /* This converts a URI into a safe filename. It quotes all unsafe characters
  292. and converts / to _ and removes the scheme identifier. The resulting
  293. file name should be unique and never occur again for a different file */
  294. string URItoFileName(string URI)
  295. {
  296. string::const_iterator I = URI.begin() + URI.find(':') + 1;
  297. for (; I < URI.end() && *I == '/'; I++);
  298. // "\x00-\x20{}|\\\\^\\[\\]<>\"\x7F-\xFF";
  299. URI = QuoteString(string(I,URI.end() - I),"\\|{}[]<>\"^~_=!@#$%^&*");
  300. string::iterator J = URI.begin();
  301. for (; J != URI.end(); J++)
  302. if (*J == '/')
  303. *J = '_';
  304. return URI;
  305. }
  306. /*}}}*/
  307. // Base64Encode - Base64 Encoding routine for short strings /*{{{*/
  308. // ---------------------------------------------------------------------
  309. /* This routine performs a base64 transformation on a string. It was ripped
  310. from wget and then patched and bug fixed.
  311. This spec can be found in rfc2045 */
  312. string Base64Encode(string S)
  313. {
  314. // Conversion table.
  315. static char tbl[64] = {'A','B','C','D','E','F','G','H',
  316. 'I','J','K','L','M','N','O','P',
  317. 'Q','R','S','T','U','V','W','X',
  318. 'Y','Z','a','b','c','d','e','f',
  319. 'g','h','i','j','k','l','m','n',
  320. 'o','p','q','r','s','t','u','v',
  321. 'w','x','y','z','0','1','2','3',
  322. '4','5','6','7','8','9','+','/'};
  323. // Pre-allocate some space
  324. string Final;
  325. Final.reserve((4*S.length() + 2)/3 + 2);
  326. /* Transform the 3x8 bits to 4x6 bits, as required by
  327. base64. */
  328. for (string::const_iterator I = S.begin(); I < S.end(); I += 3)
  329. {
  330. char Bits[3] = {0,0,0};
  331. Bits[0] = I[0];
  332. if (I + 1 < S.end())
  333. Bits[1] = I[1];
  334. if (I + 2 < S.end())
  335. Bits[2] = I[2];
  336. Final += tbl[Bits[0] >> 2];
  337. Final += tbl[((Bits[0] & 3) << 4) + (Bits[1] >> 4)];
  338. if (I + 1 >= S.end())
  339. break;
  340. Final += tbl[((Bits[1] & 0xf) << 2) + (Bits[2] >> 6)];
  341. if (I + 2 >= S.end())
  342. break;
  343. Final += tbl[Bits[2] & 0x3f];
  344. }
  345. /* Apply the padding elements, this tells how many bytes the remote
  346. end should discard */
  347. if (S.length() % 3 == 2)
  348. Final += '=';
  349. if (S.length() % 3 == 1)
  350. Final += "==";
  351. return Final;
  352. }
  353. /*}}}*/
  354. // stringcmp - Arbitary string compare /*{{{*/
  355. // ---------------------------------------------------------------------
  356. /* This safely compares two non-null terminated strings of arbitary
  357. length */
  358. int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd)
  359. {
  360. for (; A != AEnd && B != BEnd; A++, B++)
  361. if (*A != *B)
  362. break;
  363. if (A == AEnd && B == BEnd)
  364. return 0;
  365. if (A == AEnd)
  366. return 1;
  367. if (B == BEnd)
  368. return -1;
  369. if (*A < *B)
  370. return -1;
  371. return 1;
  372. }
  373. /*}}}*/
  374. // stringcasecmp - Arbitary case insensitive string compare /*{{{*/
  375. // ---------------------------------------------------------------------
  376. /* */
  377. int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd)
  378. {
  379. for (; A != AEnd && B != BEnd; A++, B++)
  380. if (toupper(*A) != toupper(*B))
  381. break;
  382. if (A == AEnd && B == BEnd)
  383. return 0;
  384. if (A == AEnd)
  385. return 1;
  386. if (B == BEnd)
  387. return -1;
  388. if (toupper(*A) < toupper(*B))
  389. return -1;
  390. return 1;
  391. }
  392. /*}}}*/
  393. // LookupTag - Lookup the value of a tag in a taged string /*{{{*/
  394. // ---------------------------------------------------------------------
  395. /* The format is like those used in package files and the method
  396. communication system */
  397. string LookupTag(string Message,const char *Tag,const char *Default)
  398. {
  399. // Look for a matching tag.
  400. int Length = strlen(Tag);
  401. for (string::iterator I = Message.begin(); I + Length < Message.end(); I++)
  402. {
  403. // Found the tag
  404. if (I[Length] == ':' && stringcasecmp(I,I+Length,Tag) == 0)
  405. {
  406. // Find the end of line and strip the leading/trailing spaces
  407. string::iterator J;
  408. I += Length + 1;
  409. for (; isspace(*I) != 0 && I < Message.end(); I++);
  410. for (J = I; *J != '\n' && J < Message.end(); J++);
  411. for (; J > I && isspace(J[-1]) != 0; J--);
  412. return string(I,J-I);
  413. }
  414. for (; *I != '\n' && I < Message.end(); I++);
  415. }
  416. // Failed to find a match
  417. if (Default == 0)
  418. return string();
  419. return Default;
  420. }
  421. /*}}}*/
  422. // StringToBool - Converts a string into a boolean /*{{{*/
  423. // ---------------------------------------------------------------------
  424. /* This inspects the string to see if it is true or if it is false and
  425. then returns the result. Several varients on true/false are checked. */
  426. int StringToBool(string Text,int Default = -1)
  427. {
  428. char *End;
  429. int Res = strtol(Text.c_str(),&End,0);
  430. if (End != Text.c_str() && Res >= 0 && Res <= 1)
  431. return Res;
  432. // Check for positives
  433. if (strcasecmp(Text.c_str(),"no") == 0 ||
  434. strcasecmp(Text.c_str(),"false") == 0 ||
  435. strcasecmp(Text.c_str(),"without") == 0 ||
  436. strcasecmp(Text.c_str(),"off") == 0 ||
  437. strcasecmp(Text.c_str(),"disable") == 0)
  438. return 0;
  439. // Check for negatives
  440. if (strcasecmp(Text.c_str(),"yes") == 0 ||
  441. strcasecmp(Text.c_str(),"true") == 0 ||
  442. strcasecmp(Text.c_str(),"with") == 0 ||
  443. strcasecmp(Text.c_str(),"on") == 0 ||
  444. strcasecmp(Text.c_str(),"enable") == 0)
  445. return 1;
  446. return Default;
  447. }
  448. /*}}}*/
  449. // TimeRFC1123 - Convert a time_t into RFC1123 format /*{{{*/
  450. // ---------------------------------------------------------------------
  451. /* This converts a time_t into a string time representation that is
  452. year 2000 complient and timezone neutral */
  453. string TimeRFC1123(time_t Date)
  454. {
  455. struct tm Conv = *gmtime(&Date);
  456. char Buf[300];
  457. const char *Day[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
  458. const char *Month[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul",
  459. "Aug","Sep","Oct","Nov","Dec"};
  460. sprintf(Buf,"%s, %02i %s %i %02i:%02i:%02i GMT",Day[Conv.tm_wday],
  461. Conv.tm_mday,Month[Conv.tm_mon],Conv.tm_year+1900,Conv.tm_hour,
  462. Conv.tm_min,Conv.tm_sec);
  463. return Buf;
  464. }
  465. /*}}}*/
  466. // ReadMessages - Read messages from the FD /*{{{*/
  467. // ---------------------------------------------------------------------
  468. /* This pulls full messages from the input FD into the message buffer.
  469. It assumes that messages will not pause during transit so no
  470. fancy buffering is used. */
  471. bool ReadMessages(int Fd, vector<string> &List)
  472. {
  473. char Buffer[4000];
  474. char *End = Buffer;
  475. while (1)
  476. {
  477. int Res = read(Fd,End,sizeof(Buffer) - (End-Buffer));
  478. // Process is dead, this is kind of bad..
  479. if (Res == 0)
  480. return false;
  481. // No data
  482. if (Res <= 0)
  483. return true;
  484. End += Res;
  485. // Look for the end of the message
  486. for (char *I = Buffer; I + 1 < End; I++)
  487. {
  488. if (I[0] != '\n' || I[1] != '\n')
  489. continue;
  490. // Pull the message out
  491. string Message(Buffer,0,I-Buffer);
  492. // Fix up the buffer
  493. for (; I < End && *I == '\n'; I++);
  494. End -= I-Buffer;
  495. memmove(Buffer,I,End-Buffer);
  496. I = Buffer;
  497. List.push_back(Message);
  498. }
  499. if (End == Buffer)
  500. return true;
  501. if (WaitFd(Fd) == false)
  502. return false;
  503. }
  504. }
  505. /*}}}*/
  506. // MonthConv - Converts a month string into a number /*{{{*/
  507. // ---------------------------------------------------------------------
  508. /* This was lifted from the boa webserver which lifted it from 'wn-v1.07'
  509. Made it a bit more robust with a few touppers though. */
  510. static int MonthConv(char *Month)
  511. {
  512. switch (toupper(*Month))
  513. {
  514. case 'A':
  515. return toupper(Month[1]) == 'P'?3:7;
  516. case 'D':
  517. return 11;
  518. case 'F':
  519. return 1;
  520. case 'J':
  521. if (toupper(Month[1]) == 'A')
  522. return 0;
  523. return toupper(Month[2]) == 'N'?5:6;
  524. case 'M':
  525. return toupper(Month[2]) == 'R'?2:4;
  526. case 'N':
  527. return 10;
  528. case 'O':
  529. return 9;
  530. case 'S':
  531. return 8;
  532. // Pretend it is January..
  533. default:
  534. return 0;
  535. }
  536. }
  537. /*}}}*/
  538. // StrToTime - Converts a string into a time_t /*{{{*/
  539. // ---------------------------------------------------------------------
  540. /* This handles all 3 populare time formats including RFC 1123, RFC 1036
  541. and the C library asctime format. It requires the GNU library function
  542. 'timegm' to convert a struct tm in UTC to a time_t. For some bizzar
  543. reason the C library does not provide any such function :<*/
  544. bool StrToTime(string Val,time_t &Result)
  545. {
  546. struct tm Tm;
  547. char Month[10];
  548. const char *I = Val.c_str();
  549. // Skip the day of the week
  550. for (;*I != 0 && *I != ' '; I++);
  551. // Handle RFC 1123 time
  552. if (sscanf(I," %d %3s %d %d:%d:%d GMT",&Tm.tm_mday,Month,&Tm.tm_year,
  553. &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) != 6)
  554. {
  555. // Handle RFC 1036 time
  556. if (sscanf(I," %d-%3s-%d %d:%d:%d GMT",&Tm.tm_mday,Month,
  557. &Tm.tm_year,&Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) == 6)
  558. Tm.tm_year += 1900;
  559. else
  560. {
  561. // asctime format
  562. if (sscanf(I," %3s %d %d:%d:%d %d",Month,&Tm.tm_mday,
  563. &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec,&Tm.tm_year) != 6)
  564. return false;
  565. }
  566. }
  567. Tm.tm_isdst = 0;
  568. Tm.tm_mon = MonthConv(Month);
  569. Tm.tm_year -= 1900;
  570. // Convert to local time and then to GMT
  571. Result = timegm(&Tm);
  572. return true;
  573. }
  574. /*}}}*/
  575. // URI::CopyFrom - Copy from an object /*{{{*/
  576. // ---------------------------------------------------------------------
  577. /* This parses the URI into all of its components */
  578. void URI::CopyFrom(string U)
  579. {
  580. string::const_iterator I = U.begin();
  581. // Locate the first colon, this seperates the scheme
  582. for (; I < U.end() && *I != ':' ; I++);
  583. string::const_iterator FirstColon = I;
  584. /* Determine if this is a host type URI with a leading double //
  585. and then search for the first single / */
  586. string::const_iterator SingleSlash = I;
  587. if (I + 3 < U.end() && I[1] == '/' && I[2] == '/')
  588. SingleSlash += 3;
  589. for (; SingleSlash < U.end() && *SingleSlash != '/'; SingleSlash++);
  590. if (SingleSlash > U.end())
  591. SingleSlash = U.end();
  592. // We can now write the access and path specifiers
  593. Access = string(U,0,FirstColon - U.begin());
  594. if (SingleSlash != U.end())
  595. Path = string(U,SingleSlash - U.begin());
  596. if (Path.empty() == true)
  597. Path = "/";
  598. // Now we attempt to locate a user:pass@host fragment
  599. if (FirstColon[1] == '/' && FirstColon[2] == '/')
  600. FirstColon += 3;
  601. else
  602. FirstColon += 1;
  603. if (FirstColon >= U.end())
  604. return;
  605. if (FirstColon > SingleSlash)
  606. FirstColon = SingleSlash;
  607. // Search for the @
  608. I = FirstColon;
  609. for (; I < SingleSlash && *I != '@'; I++);
  610. string::const_iterator At = I;
  611. // Colon in the @ section
  612. I = FirstColon + 1;
  613. for (; I < At && *I != ':'; I++);
  614. string::const_iterator SecondColon = I;
  615. // Now write the host and user/pass
  616. if (At == SingleSlash)
  617. {
  618. if (FirstColon < SingleSlash)
  619. Host = string(U,FirstColon - U.begin(),SingleSlash - FirstColon);
  620. }
  621. else
  622. {
  623. Host = string(U,At - U.begin() + 1,SingleSlash - At - 1);
  624. User = string(U,FirstColon - U.begin(),SecondColon - FirstColon);
  625. if (SecondColon < At)
  626. Password = string(U,SecondColon - U.begin() + 1,At - SecondColon - 1);
  627. }
  628. // Now we parse off a pot number from the hostname
  629. Port = 0;
  630. string::size_type Pos = Host.rfind(':');
  631. if (Pos == string::npos)
  632. return;
  633. Port = atoi(string(Host,Pos+1).c_str());
  634. Host = string(Host,0,Pos);
  635. }
  636. /*}}}*/
  637. // URI::operator string - Convert the URI to a string /*{{{*/
  638. // ---------------------------------------------------------------------
  639. /* */
  640. URI::operator string()
  641. {
  642. string Res = Access + ':';
  643. if (Host.empty() == false)
  644. {
  645. Res += "//";
  646. if (User.empty() == false)
  647. {
  648. Res += "//" + User;
  649. if (Password.empty() == false)
  650. Res += ":" + Password;
  651. Res += "@";
  652. }
  653. Res += Host;
  654. if (Port != 0)
  655. {
  656. char S[30];
  657. sprintf(S,":%u",Port);
  658. Res += S;
  659. }
  660. }
  661. if (Path.empty() == false)
  662. {
  663. if (Path[0] != '/')
  664. Res += "/" + Path;
  665. else
  666. Res += Path;
  667. }
  668. return Res;
  669. }
  670. /*}}}*/