Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "labeldat.h"
- #include <fstream> //
- #include <iostream> //cerr, mostly
- #include <string> //I don't wanna think about c-string manipulation :]
- #include <cctype> //isalpha, tolower, toupper
- #include <regex> //std::regex_match for case insensitive matching
- #include <limits> //std::numeric_limits<streamsize>::max()
- string iRequireSustenance::lessWs(string str)
- {
- //I don't really want to #include <algorithm> because <regex> already makes
- //the executable big enough. I actually don't know how large <algorithm> is
- //but I'm assuming it's quite yuge because it contains a lot of algorithms.
- for (string::iterator it = str.begin(); it < str.end(); ++it)
- {
- if(isspace(*it))
- { str.erase(it); }
- }
- return str;
- }
- void iRequireSustenance::munch(string str)
- {
- /*first, figure out where to separate the label from the data.
- *std::string::find_first_of returns -1 if there is no match.
- *
- !~ note: string::size_type is size_t, an UNSIGNED INTEGER typedef
- !~ this means assigning -1 to string::size_type makes it a yuge value.
- *
- */
- string::size_type equalsPos = str.find_first_of('=');
- //basically, as long as equalsPos is not the largest number,
- if !(equalsPos == static_cast<size_t>(-1))
- {
- /* separate the label and the data using std::string::substr,
- * then input the label into the [[2n]th] position, then, hopefully
- * error check the corresponding data.
- */
- //create a substring, containing (hopefully) just the label, with no
- //whitespace.
- tempStrB = iRequireSustenance::lessWs(str.substr(0,equalsPos))
- //check whether or not it is one of our labels.
- if(regex_match(tempStrB, m, validLabel))
- {
- data.push_back(tempStr);
- //TODO: implement error checking
- data.push_back(str.substr(equalsPos + 1, std::npos));
- }
- }
- else
- {
- std::cerr<< "ERROR: no '=' found in line "
- /* << figure out how to output line number */;
- }
- return void;
- }
- void iRequireSustenance::read(void)
- {
- //...are we there yet?
- fIn.peek()
- if(!eof)
- { //no? good!
- std::getline(fIn, tempStringA);
- iRequireSustenance::munch(tempStringA);
- ++lineNum;
- }
- else
- {
- fIn.clear();
- std::cerr << "Reached end of file parsing line " << lineNum << endl;
- }
- }
- void iRequireSustenance::read(int64_t lineNo)
- {
- //go to the start of the file,
- fIn.seekg(0);
- //tell our keep-track-of-er that we are at the start of file,
- lineNum = 0;
- //whip past a whole lotta lines, making sure to bring our
- //keeper-of-track-ofs along for the ride, because it'd be upset that it
- //missed such a wild ride.
- for (; lineNo != 0; --lineNo)
- {
- fIn.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- ++lineNum;
- }
- //then, just read the line.
- iRequireSustenance::read();
- }
- void iRequireSustenance::read(istream& is)
- {
- //...are we there yet?
- is.peek()
- if(!eof)
- { //no? good!
- std::getline(is, tempStringA);
- iRequireSustenance::munch(tempStringA);
- ++lineNum;
- }
- else
- {
- is.clear();
- std::cerr << "Reached end of file parsing line " << lineNum << endl;
- }
- }
- void iRequireSustenance::read(istream& is, int64_t lineNo)
- {
- //go to the start of the file,
- is.seekg(0);
- //tell our keep-track-of-er that we are at the start of file,
- lineNum = 0;
- //whip past a whole lotta lines, making sure to bring our
- //keeper-of-track-ofs along for the ride, because it'd be upset that it
- //missed such a wild ride.
- for (; lineNo != 0; --lineNo)
- {
- is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- ++lineNum;
- }
- //then, just read the line.
- iRequireSustenance::read(*is);
- }
- void iRequireSustenance::pumpItOut(void)
- { using namespace std;
- //I have a vector<bool> haveOutput as a member variable, with a number of
- //bools equal to the number of strings in labels. The first entry in
- //labels is the root label, so it doesn't factor in to processing, so I
- //will use the first entry in haveOutput to make this bad boy alternate
- //in behavior.
- //practicing with iterators--this should come in handy when I figure out
- //how to abstract these kinds of things.
- for(vector<string>::iterator it = data.begin(); it != data.end(); ++it)
- {
- if(!haveOutput[0])
- { //this means that we are printing a label
- fOut << "\t<" << *it << ">";
- haveOutput[0] = true;
- }
- else
- {
- fOut << *it << "<" << it[-1] << "/>\n";
- haveOutput[0] = false;
- }
- }
- }
- /* okay so I can require sustenance and not have to arrage it before I barf it
- * all back up. Good to know.
- //I am assuming that the first entry in labels is the root tag.
- void iRequireSustenance::arrange(void)
- {
- //the first element is root, so I only need to worry about the elements that
- //would actually have data corresponding to them.
- size_t elemonLabels = labels.size() - 1
- ,numData = data.size()
- ,remainder = numData % elemonLabels;
- /*only runs if numData%elemonLabels is non-zero--
- *resizes numData to be an even multiple of the number of labels.
- *This guarantees we cannot run off of the end of our data.
- *//*
- if(remainder)
- {
- data.resize(numData + remainder);
- }
- //keep track of how many groups we've taken care of--this lets us sort,
- //add more data later on, then not worry about re-sorting. This variable
- //will be referred to as n.
- unsigned short groupsProcessed = 0;
- //look at data entries in range [n * elemonLabels, 2n * elemonLabels]
- tempStr = "";
- for(size_t i = 0; i < elemonLabels; ++i)
- {
- //make a string of just the labels, delineated by spaces.
- tempStr += data.at(elemonLabels * groupsProcessed + 2 * i);
- tempStr.push_back(' ');
- }
- TODO: FIGURE OUT THIS SORTING SHIT
- //organize the data to be in the same order as labels is.
- //if we run into a repeated data value, assume that means the start of new
- //group. It also means that this group of data is missing an entry.
- //figure out what the missing entry is, and put a placeholder in there.
- //perhaps, give it a value of "not provided".
- //move on to the next group, being careful to not overrun bounds.
- return void;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement