tagfile.cc 6.4 KB

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