tagfile.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: tagfile.cc,v 1.8 1998/07/16 06:08:39 jgg Exp $
  4. /* ######################################################################
  5. Fast scanner for RFC-822 type header information
  6. This uses a rotating 64K buffer to load the package information into.
  7. The scanner runs over it and isolates and indexes a single section.
  8. ##################################################################### */
  9. /*}}}*/
  10. // Include Files /*{{{*/
  11. #ifdef __GNUG__
  12. #pragma implementation "apt-pkg/tagfile.h"
  13. #endif
  14. #include <apt-pkg/tagfile.h>
  15. #include <apt-pkg/error.h>
  16. #include <string>
  17. #include <stdio.h>
  18. /*}}}*/
  19. // TagFile::pkgTagFile - Constructor /*{{{*/
  20. // ---------------------------------------------------------------------
  21. /* */
  22. pkgTagFile::pkgTagFile(File &Fd) : Fd(Fd)
  23. {
  24. Buffer = new char[64*1024];
  25. Start = End = Buffer + 64*1024;
  26. Left = Fd.Size();
  27. iOffset = 0;
  28. Fill();
  29. }
  30. /*}}}*/
  31. // TagFile::Step - Advance to the next section /*{{{*/
  32. // ---------------------------------------------------------------------
  33. /* If the Section Scanner fails we refill the buffer and try again. */
  34. bool pkgTagFile::Step(pkgTagSection &Tag)
  35. {
  36. if (Tag.Scan(Start,End - Start) == false)
  37. {
  38. if (Fill() == false)
  39. return false;
  40. if (Tag.Scan(Start,End - Start) == false)
  41. return _error->Error("Unable to parse package file");
  42. }
  43. Start += Tag.size();
  44. iOffset += Tag.size();
  45. return true;
  46. }
  47. /*}}}*/
  48. // TagFile::Fill - Top up the buffer /*{{{*/
  49. // ---------------------------------------------------------------------
  50. /* This takes the bit at the end of the buffer and puts it at the start
  51. then fills the rest from the file */
  52. bool pkgTagFile::Fill()
  53. {
  54. unsigned long Size = End - Start;
  55. if (Left == 0)
  56. {
  57. if (Size <= 1)
  58. return false;
  59. return true;
  60. }
  61. memmove(Buffer,Start,Size);
  62. Start = Buffer;
  63. // See if only a bit of the file is left or if
  64. if (Left < End - Buffer - Size)
  65. {
  66. if (Fd.Read(Buffer + Size,Left) == false)
  67. return false;
  68. End = Buffer + Size + Left;
  69. Left = 0;
  70. }
  71. else
  72. {
  73. if (Fd.Read(Buffer + Size, End - Buffer - Size) == false)
  74. return false;
  75. Left -= End - Buffer - Size;
  76. }
  77. return true;
  78. }
  79. /*}}}*/
  80. // TagSection::Scan - Scan for the end of the header information /*{{{*/
  81. // ---------------------------------------------------------------------
  82. /* This looks for the first double new line in the data stream. It also
  83. indexes the tags in the section. */
  84. bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
  85. {
  86. const char *End = Start + MaxLength;
  87. Stop = Section = Start;
  88. TagCount = 0;
  89. Indexes[TagCount++] = Stop - Section;
  90. Stop++;
  91. for (; Stop < End; Stop++)
  92. {
  93. if (Stop[-1] != '\n')
  94. continue;
  95. if (Stop[0] == '\n')
  96. {
  97. // Extra one at the end to simplify find
  98. Indexes[TagCount] = Stop - Section;
  99. for (; Stop[0] == '\n' && Stop < End; Stop++);
  100. return true;
  101. break;
  102. }
  103. if (isspace(Stop[0]) == 0)
  104. Indexes[TagCount++] = Stop - Section;
  105. // Just in case.
  106. if (TagCount > sizeof(Indexes)/sizeof(Indexes[0]))
  107. TagCount = sizeof(Indexes)/sizeof(Indexes[0]);
  108. }
  109. return false;
  110. }
  111. /*}}}*/
  112. // TagSection::Find - Locate a tag /*{{{*/
  113. // ---------------------------------------------------------------------
  114. /* This searches the section for a tag that matches the given string. */
  115. bool pkgTagSection::Find(const char *Tag,const char *&Start,
  116. const char *&End)
  117. {
  118. unsigned int Length = strlen(Tag);
  119. for (unsigned int I = 0; I != TagCount; I++)
  120. {
  121. if (strncasecmp(Tag,Section + Indexes[I],Length) != 0)
  122. continue;
  123. // Make sure the colon is in the right place
  124. const char *C = Section + Length + Indexes[I];
  125. for (; isspace(*C) != 0; C++);
  126. if (*C != ':')
  127. continue;
  128. // Strip off the gunk from the start end
  129. Start = C;
  130. End = Section + Indexes[I+1];
  131. for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
  132. for (; isspace(End[-1]) != 0 && End > Start; End--);
  133. return true;
  134. }
  135. Start = End = 0;
  136. return false;
  137. }
  138. /*}}}*/