Advertisement
Guest User

labeldat.cpp

a guest
Nov 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.77 KB | None | 0 0
  1. #include "labeldat.h"
  2. #include <fstream>  //
  3. #include <iostream> //cerr, mostly
  4. #include <string>   //I don't wanna think about c-string manipulation :]
  5. #include <cctype>   //isalpha, tolower, toupper
  6. #include <regex>    //std::regex_match for case insensitive matching
  7. #include <limits>   //std::numeric_limits<streamsize>::max()
  8.  
  9. string iRequireSustenance::lessWs(string str)
  10. {
  11.     //I don't really want to #include <algorithm> because <regex> already makes
  12.     //the executable big enough. I actually don't know how large <algorithm> is
  13.     //but I'm assuming it's quite yuge because it contains a lot of algorithms.
  14.    
  15.     for (string::iterator it = str.begin(); it < str.end(); ++it)
  16.     {
  17.         if(isspace(*it))
  18.         { str.erase(it); }
  19.     }
  20.     return str;
  21. }
  22.  
  23.  
  24. void iRequireSustenance::munch(string str)
  25. {
  26.     /*first, figure out where to separate the label from the data.
  27.      *std::string::find_first_of returns -1 if there is no match.
  28.      *
  29.      !~ note: string::size_type is size_t, an UNSIGNED INTEGER typedef
  30.      !~ this means assigning -1 to string::size_type makes it a yuge value.
  31.      *
  32.      */
  33.     string::size_type equalsPos = str.find_first_of('=');
  34.    
  35.     //basically, as long as equalsPos is not the largest number,
  36.     if !(equalsPos == static_cast<size_t>(-1))
  37.     {
  38.         /* separate the label and the data using std::string::substr,
  39.          * then input the label into the [[2n]th] position, then, hopefully
  40.          * error check the corresponding data.
  41.          */
  42.        
  43.         //create a substring, containing (hopefully) just the label, with no
  44.         //whitespace.
  45.         tempStrB = iRequireSustenance::lessWs(str.substr(0,equalsPos))
  46.        
  47.         //check whether or not it is one of our labels.
  48.         if(regex_match(tempStrB, m, validLabel))
  49.         {
  50.             data.push_back(tempStr);
  51.            
  52.             //TODO: implement error checking
  53.             data.push_back(str.substr(equalsPos + 1, std::npos));
  54.         }
  55.     }
  56.     else
  57.     {
  58.         std::cerr<< "ERROR: no '=' found in line "
  59.               /* << figure out how to output line number */;
  60.     }
  61.     return void;
  62. }
  63.  
  64. void iRequireSustenance::read(void)
  65. {
  66.     //...are we there yet?
  67.     fIn.peek()
  68.     if(!eof)
  69.     {   //no? good!
  70.         std::getline(fIn, tempStringA);
  71.        
  72.         iRequireSustenance::munch(tempStringA);
  73.         ++lineNum;
  74.     }
  75.     else
  76.     {
  77.         fIn.clear();
  78.         std::cerr << "Reached end of file parsing line " << lineNum << endl;
  79.     }
  80. }
  81.  
  82. void iRequireSustenance::read(int64_t lineNo)
  83. {
  84.     //go to the start of the file,
  85.     fIn.seekg(0);
  86.    
  87.     //tell our keep-track-of-er that we are at the start of file,
  88.     lineNum = 0;
  89.    
  90.     //whip past a whole lotta lines, making sure to bring our
  91.     //keeper-of-track-ofs along for the ride, because it'd be upset that it
  92.     //missed such a wild ride.
  93.     for (; lineNo != 0; --lineNo)
  94.     {
  95.         fIn.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  96.         ++lineNum;
  97.     }
  98.    
  99.     //then, just read the line.
  100.     iRequireSustenance::read();
  101. }
  102.  
  103. void iRequireSustenance::read(istream& is)
  104. {
  105.     //...are we there yet?
  106.     is.peek()
  107.     if(!eof)
  108.     {   //no? good!
  109.         std::getline(is, tempStringA);
  110.        
  111.         iRequireSustenance::munch(tempStringA);
  112.         ++lineNum;
  113.     }
  114.     else
  115.     {
  116.         is.clear();
  117.         std::cerr << "Reached end of file parsing line " << lineNum << endl;
  118.     }
  119. }
  120.  
  121. void iRequireSustenance::read(istream& is, int64_t lineNo)
  122. {
  123.     //go to the start of the file,
  124.     is.seekg(0);
  125.    
  126.     //tell our keep-track-of-er that we are at the start of file,
  127.     lineNum = 0;
  128.    
  129.     //whip past a whole lotta lines, making sure to bring our
  130.     //keeper-of-track-ofs along for the ride, because it'd be upset that it
  131.     //missed such a wild ride.
  132.     for (; lineNo != 0; --lineNo)
  133.     {
  134.         is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  135.         ++lineNum;
  136.     }
  137.    
  138.     //then, just read the line.
  139.     iRequireSustenance::read(*is);
  140. }
  141.  
  142. void iRequireSustenance::pumpItOut(void)
  143. {   using namespace std;
  144.    
  145.     //I have a vector<bool> haveOutput as a member variable, with a number of
  146.     //bools equal to the number of strings in labels. The first entry in
  147.     //labels is the root label, so it doesn't factor in to processing, so I
  148.     //will use the first entry in haveOutput to make this bad boy alternate
  149.     //in behavior.
  150.  
  151.     //practicing with iterators--this should come in handy when I figure out
  152.     //how to abstract these kinds of things.
  153.     for(vector<string>::iterator it = data.begin(); it != data.end(); ++it)
  154.     {
  155.         if(!haveOutput[0])
  156.         { //this means that we are printing a label
  157.             fOut << "\t<" << *it << ">";
  158.             haveOutput[0] = true;
  159.         }
  160.         else
  161.         {
  162.             fOut << *it << "<" << it[-1] << "/>\n";
  163.             haveOutput[0] = false;  
  164.         }
  165.     }
  166. }
  167.  
  168. /* okay so I can require sustenance and not have to arrage it before I barf it
  169.  * all back up. Good to know.
  170. //I am assuming that the first entry in labels is the root tag.
  171. void iRequireSustenance::arrange(void)
  172. {
  173.     //the first element is root, so I only need to worry about the elements that
  174.     //would actually have data corresponding to them.
  175.     size_t elemonLabels = labels.size() - 1
  176.            ,numData = data.size()
  177.            ,remainder = numData % elemonLabels;
  178.    
  179.    
  180.     /*only runs if numData%elemonLabels is non-zero--
  181.      *resizes numData to be an even multiple of the number of labels.
  182.      *This guarantees we cannot run off of the end of our data.
  183.      *//*
  184.     if(remainder)
  185.     {
  186.         data.resize(numData + remainder);
  187.     }
  188.    
  189.     //keep track of how many groups we've taken care of--this lets us sort,
  190.     //add more data later on, then not worry about re-sorting. This variable
  191.     //will be referred to as n.
  192.     unsigned short groupsProcessed = 0;
  193.    
  194.     //look at data entries in range [n * elemonLabels, 2n * elemonLabels]
  195.    
  196.     tempStr = "";
  197.     for(size_t i = 0; i < elemonLabels; ++i)
  198.     {
  199.         //make a string of just the labels, delineated by spaces.
  200.         tempStr += data.at(elemonLabels * groupsProcessed + 2 * i);
  201.         tempStr.push_back(' ');
  202.     }
  203.     TODO: FIGURE OUT THIS SORTING SHIT
  204.     //organize the data to be in the same order as labels is.
  205.    
  206.     //if we run into a repeated data value, assume that means the start of new
  207.     //group. It also means that this group of data is missing an entry.
  208.    
  209.     //figure out what the missing entry is, and put a placeholder in there.
  210.     //perhaps, give it a value of "not provided".
  211.    
  212.     //move on to the next group, being careful to not overrun bounds.
  213.    
  214.     return void;
  215. }
  216. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement