tagfile.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: tagfile.cc,v 1.4 1998/07/05 05:33:58 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. #include <pkglib/tagfile.h>
  12. #include <pkglib/error.h>
  13. #include <string>
  14. #include <stdio.h>
  15. /*}}}*/
  16. // TagFile::pkgTagFile - Constructor /*{{{*/
  17. // ---------------------------------------------------------------------
  18. /* */
  19. pkgTagFile::pkgTagFile(File &Fd) : Fd(Fd)
  20. {
  21. Buffer = new char[64*1024];
  22. Start = End = Buffer + 64*1024;
  23. Left = Fd.Size();
  24. iOffset = 0;
  25. Fill();
  26. }
  27. /*}}}*/
  28. // TagFile::Step - Advance to the next section /*{{{*/
  29. // ---------------------------------------------------------------------
  30. /* If the Section Scanner fails we refill the buffer and try again. */
  31. bool pkgTagFile::Step(pkgTagSection &Tag)
  32. {
  33. if (Tag.Scan(Start,End - Start) == false)
  34. {
  35. if (Fill() == false)
  36. return false;
  37. if (Tag.Scan(Start,End - Start) == false)
  38. return _error->Error("Unable to parse package file");
  39. }
  40. Start += Tag.size();
  41. iOffset += Tag.size();
  42. return true;
  43. }
  44. /*}}}*/
  45. // TagFile::Fill - Top up the buffer /*{{{*/
  46. // ---------------------------------------------------------------------
  47. /* This takes the bit at the end of the buffer and puts it at the start
  48. then fills the rest from the file */
  49. bool pkgTagFile::Fill()
  50. {
  51. unsigned long Size = End - Start;
  52. if (Left == 0)
  53. {
  54. if (Size <= 1)
  55. return false;
  56. return true;
  57. }
  58. memmove(Buffer,Start,Size);
  59. Start = Buffer;
  60. // See if only a bit of the file is left or if
  61. if (Left < End - Buffer - Size)
  62. {
  63. if (Fd.Read(Buffer + Size,Left) == false)
  64. return false;
  65. End = Buffer + Size + Left;
  66. Left = 0;
  67. }
  68. else
  69. {
  70. if (Fd.Read(Buffer + Size, End - Buffer - Size) == false)
  71. return false;
  72. Left -= End - Buffer - Size;
  73. }
  74. return true;
  75. }
  76. /*}}}*/
  77. // TagSection::Scan - Scan for the end of the header information /*{{{*/
  78. // ---------------------------------------------------------------------
  79. /* This looks for the first double new line in the data stream. It also
  80. indexes the tags in the section. */
  81. bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
  82. {
  83. const char *End = Start + MaxLength;
  84. Stop = Section = Start;
  85. TagCount = 0;
  86. Indexes[TagCount++] = Stop - Section;
  87. Stop++;
  88. for (; Stop < End; Stop++)
  89. {
  90. if (Stop[-1] != '\n')
  91. continue;
  92. if (Stop[0] == '\n')
  93. {
  94. // Extra one at the end to simplify find
  95. Indexes[TagCount] = Stop - Section;
  96. for (; Stop[0] == '\n' && Stop < End; Stop++);
  97. return true;
  98. break;
  99. }
  100. if (isspace(Stop[0]) == 0)
  101. Indexes[TagCount++] = Stop - Section;
  102. // Just in case.
  103. if (TagCount > sizeof(Indexes)/sizeof(Indexes[0]))
  104. TagCount = sizeof(Indexes)/sizeof(Indexes[0]);
  105. }
  106. return false;
  107. }
  108. /*}}}*/
  109. // TagSection::Find - Locate a tag /*{{{*/
  110. // ---------------------------------------------------------------------
  111. /* This searches the section for a tag that matches the given string. */
  112. bool pkgTagSection::Find(const char *Tag,const char *&Start,
  113. const char *&End)
  114. {
  115. unsigned int Length = strlen(Tag);
  116. for (unsigned int I = 0; I != TagCount; I++)
  117. {
  118. if (strncasecmp(Tag,Section + Indexes[I],Length) != 0)
  119. continue;
  120. // Make sure the colon is in the right place
  121. const char *C = Section + Length + Indexes[I];
  122. for (; isspace(*C) != 0; C++);
  123. if (*C != ':')
  124. continue;
  125. // Strip off the gunk from the start end
  126. Start = C;
  127. End = Section + Indexes[I+1];
  128. for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
  129. for (; isspace(End[-1]) != 0 && End > Start; End--);
  130. return true;
  131. }
  132. Start = End = 0;
  133. return false;
  134. }
  135. /*}}}*/
  136. #include <pkglib/pkgcachegen.h>
  137. #include <pkglib/deblistparser.h>
  138. int main(int argc,char *argv[])
  139. {
  140. {
  141. File CacheF("./cache",File::WriteEmpty);
  142. DynamicMMap Map(CacheF,MMap::Public);
  143. pkgCacheGenerator Gen(Map);
  144. for (int I = 1; I != argc; I++)
  145. {
  146. cout << "Merging in " << argv[I] << endl;
  147. File F(argv[I],File::ReadOnly);
  148. Gen.SelectFile(argv[I]);
  149. debListParser Parser(F);
  150. Gen.MergeList(Parser);
  151. }
  152. }
  153. /*
  154. {
  155. File CacheF("./cache",File::WriteExists);
  156. MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
  157. pkgCache Cache(Map);
  158. for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  159. {
  160. cout << "Package: " << I.Name() << endl;
  161. for (pkgCache::VerIterator V = I.VersionList(); V.end() == false; V++)
  162. {
  163. cout << "Version: " << V.VerStr() << endl;
  164. cout << "Size: " << V->Size << endl;
  165. cout << "Installed-Size: " << V->InstalledSize << endl;
  166. cout << "Section: " << V.Section() << endl;
  167. cout << "Priority: " << Cache.Priority(V->Priority) << endl;
  168. pkgCache::PrvIterator P = V.ProvidesList();
  169. if (P.end() == false)
  170. {
  171. cout << "Provides: ";
  172. for (; P.end() == false; P++)
  173. cout << P.Name() << ", ";
  174. cout << endl;
  175. }
  176. }
  177. cout << endl;
  178. }
  179. }
  180. */
  181. #if 0
  182. pkgTagSection I;
  183. while (Test.Step(I) == true)
  184. {
  185. const char *Start;
  186. const char *End;
  187. if (I.Find("Package",Start,End) == false)
  188. {
  189. cout << "Failed" << endl;
  190. continue;
  191. }
  192. cout << "Package: " << string(Start,End - Start) << endl;
  193. /* for (const char *I = Start; I < End; I++)
  194. {
  195. const char *Begin = I;
  196. bool Number = true;
  197. while (isspace(*I) == 0 && ispunct(*I) == 0 && I < End)
  198. {
  199. if (isalpha(*I) != 0)
  200. Number = false;
  201. I++;
  202. }
  203. if (Number == false)
  204. cout << string(Begin,I-Begin) << endl;
  205. while ((isspace(*I) != 0 || ispunct(*I) != 0) && I < End)
  206. I++;
  207. I--;
  208. } */
  209. }
  210. #endif
  211. _error->DumpErrors();
  212. }