tagfile.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: tagfile.cc,v 1.7 1998/07/12 23:58: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 <apt-pkg/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. /*}}}*/