Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help filling in this code, I am creating a class in C++ to parse through

ID: 3731852 • Letter: I

Question

I need help filling in this code, I am creating a class in C++ to parse through a string and each method needs to be filled in. The parameters and directions for each method are commented out above the start of the method. Help would be greatly appreciated!

#include
#include
#include
#include "../327_proj3_test/includes/StringParserClass.h"
#include "../327_proj3_test/includes/constants.h"

namespace KP_StringParserClass{

   class StringParserClass
   {

       //dont forget to initialize member variables
       StringParserClass(void){

       }

       //call cleanup to release any allocated memory
       virtual ~StringParserClass(void){

       }

       //these are the start tag and the end tags that we want to find,
       //presumably the data of interest is between them, please make a
       //COPY of what pStartTag and pEndTag point to. In other words
       //DO NOT SET pStartTag = pStart
       //returns:
       //SUCCESS
       //ERROR_TAGS_NULL if either pStart or pEnd is null
       int setTags(const char *pStart, const char *pEnd){

       }


       //First clears myVector
       //going to search thru pDataToSearchThru, looking for info bracketed by
       //pStartTag and pEndTag, will add that info only to myVector
       //returns
       //SUCCESS finished searching for data between tags, results in myVector (0 or more entries)
       //ERROR_TAGS_NULL if either pStart or pEnd is null
       //ERROR_DATA_NULL pDataToSearchThru is null
       int getDataBetweenTags(char *pDataToSearchThru, std::vector &myVector){

       }

       void cleanup(){

       }

       //Searches a string starting at pStart for pTagToLookFor
       //returns:
       //SUCCESS found pTagToLookFor, pStart points to beginning of tag and pEnd points to end of tag
       //FAIL did not find pTagToLookFor and pEnd points to 0
       //ERROR_TAGS_NULL if either pStart or pEnd is null
       int findTag(char *pTagToLookFor, char *&pStart, char *&pEnd){

       }

       char   *pStartTag;
       char   *pEndTag;
       bool   areTagsSet;
   }
}

Header files are given below that I have implemented into the .cpp file above. There is also a FileIO class I have written but I don't think that this stringparser class needs to implement any of that.

/*
* StringParserClass.h
*
* Created on: Oct 8, 2017
*      Author: keith
*/
#ifndef STRINGPARSER_H_
#define STRINGPARSER_H_

#include <vector>
#include <string>

namespace KP_StringParserClass{

   class StringParserClass
   {
   public:

       //dont forget to initialize member variables
       StringParserClass(void);

       //call cleanup to release any allocated memory
       virtual ~StringParserClass(void);

       //these are the start tag and the end tags that we want to find,
       //presumably the data of interest is between them, please make a
       //COPY of what pStartTag and pEndTag point to. In other words
       //DO NOT SET pStartTag = pStart
       //returns:
       //SUCCESS
       //ERROR_TAGS_NULL if either pStart or pEnd is null
       int setTags(const char *pStart, const char *pEnd);

       //First clears myVector
       //going to search thru pDataToSearchThru, looking for info bracketed by
       //pStartTag and pEndTag, will add that info only to myVector
       //returns
       //SUCCESS finished searching for data between tags, results in myVector (0 or more entries)
       //ERROR_TAGS_NULL if either pStart or pEnd is null
       //ERROR_DATA_NULL pDataToSearchThru is null
       int getDataBetweenTags(char *pDataToSearchThru, std::vector<std::string> &myVector);

   private:
       void cleanup();

       //Searches a string starting at pStart for pTagToLookFor
       //returns:
       //SUCCESS found pTagToLookFor, pStart points to beginning of tag and pEnd points to end of tag
       //FAIL did not find pTagToLookFor and pEnd points to 0
       //ERROR_TAGS_NULL if either pStart or pEnd is null
       int findTag(char *pTagToLookFor, char *&pStart, char *&pEnd);

       char   *pStartTag;
       char   *pEndTag;
       bool   areTagsSet;
   };
}
#endif

* constants.h
*
* Created on: Oct 8, 2017
*      Author: keith
*/

#ifndef CONSTANTS_H_
#define CONSTANTS_H_
   #include <string>

   //common
   const int    SUCCESS                              = 0;

   //stringparser specific error conditions
   const int   FAIL                                = SUCCESS-1;
   const int   ERROR_NO_ERROR                        = SUCCESS-2;
   const int   ERROR_TAGS_NULL                       = SUCCESS-3;
   const int   ERROR_DATA_NULL                       = SUCCESS-4;

   //fileIO specific error conditions
   const int    COULD_NOT_OPEN_FILE_TO_READ           = SUCCESS-5;
   const int    COULD_NOT_OPEN_FILE_TO_WRITE       = SUCCESS-6;


   //test app specific info
   const int FAIL_WRONG_NUMBER_ARGS = -6;
   const int EXPECTED_NUMBER_ARGUMENTS =5;
   const std::string WRONG_NUMB_ARGS = "This program expects 4 arguments, inputfile starttag endtag outputfile";

#endif /* CONSTANTS_H_ */

Explanation / Answer

main.cpp

#include <iostream>

#include <algorithm>

#include <vector>

#include <string>

#include "..IncludeFileReader.h" //relative path, from PWD, go up one, then down into Include to find FileReader.h

#include "..Includeconstants.h"

#include "..IncludeDebug_Help.h"

#include "..Includestringparserclass.h"

void outputvectorrow(std::string i){

std::cout<<i<<std::endl;

}

void foreach(std::vector<std::string> myVector){

std::for_each(myVector.begin(), myVector.end(), outputvectorrow);

}

int main(){

//TODO open file, if not there ask for a different file or exit

std::string fileContents = "";

KP_FileReaderClass::FileReader aFileReader;

aFileReader.getFileContents(TEST_FILE_NAME, fileContents);

//got file data, this is a bogus time and memory wasting step

//whose sole purpose is to provide a way to pass

//in a non const pointer to getDataBetweenTags(..) without casting

/*vector<char> myLine;

std::copy(filecontents.begin(), filecontents.end(), back_inserter(myLine));*/

//TODO create an instance of the stringparser

KP_StringParserClass::StringParserClass parser;

//TODO set the tags

parser.setTags(START_TAG, END_TAG);

//TODO pull out the data

char *charData = strdup(filecontents.c_str());

parser.getDataBetweenTags(charData, storageForData);

//TODO write to file and to screen

if(outFile.is_open()){

for

}

}

StringParserClass.cpp

#include <string>

#include "..IncludestringparserClass.h"

#include "..IncludeFileReader.h"

#include "..Includeconstants.h"

#include "..IncludeDebug_Help.h"

using namespace std;

using namespace KP_StringParserClass;

//TODO Fill this in

StringParserClass::StringParserClass(void){

char *pStartTag = new char;

char *pEndTag = new char;

bool areTagsSet = false;

int lastError=0;

}

StringParserClass::~StringParserClass(){}

int StringParserClass::getLastError(){

return lastError;

}

bool StringParserClass::setTags(const char *pStartTag, const char *pEndTag){

//length of both

int sl= strlen(pStartTag)+1;

int el = strlen(pEndTag)+1;

//initialize memory space to both

StringParserClass::pStartTag = (char*)malloc(sl);

StringParserClass::pEndTag = (char*)malloc(el);

//set memeory space to pStart

strncpy(StringParserClass::pStartTag,pStartTag,sl);

strncpy(StringParserClass::pEndTag,pEndTag,el);

if(*StringParserClass::pStartTag != NULL && *StringParserClass::pEndTag != NULL){

areTagsSet = true;

}else{

lastError = ERROR_DATA_NULL;

return false;

}

return areTagsSet;

}

bool StringParserClass::getDataBetweenTags(char *pDataToSearchThru, vector<string> &myVector){

char *sTag = pDataToSearchThru;

bool notEnd = true;

while(notEnd){

char *eTag = 0;

//find start mem space

findTag(pStartTag, sTag, eTag);

char *sTag2 = eTag;

char *eTag2 = 0;

//find end mem space

findTag(pEndTag, sTag2, eTag2);

int i = 1;

//store data

std::string data = "";

while (*(eTag + i) != *(sTag2)){

if(*eTag == '' || *sTag == ''){

break;

}

data += *(eTag + i);

++i;

}

if (data != ""){

//add data

myVector.push_back(data);

}

else{

lastError = ERROR_DATA_NULL;

}

if (*eTag2 == ''){

notEnd = false;

}

else{

sTag = eTag2;

}

}

cleanup();

return SUCCEEDED;

}

bool StringParserClass::findTag(char *pTagToLookFor, char *&pStart, char *&pEnd){

int i = 0;

while(*(pStart + i) != ''){

int k = i;

int j = 0;

if(*(pStart + k) == *(pTagToLookFor + j)){

while(*(pStart + k) == *(pTagToLookFor + j)){

char *t = (pStart + k);

++k;

++j;

if(*(pS + k) == '>'){

pStart = pStart + i;

pEnd = pStart + j;

return true;

}

}

}

++i;

}

pEnd = pStart + i;

return false;

}

void StringParser::cleanup(){

if (pStartTag){

delete[] pStartTag;

}

if(pEndTag){

delete[] pEndTag;

}

}

Debug_Help.h

#pragma once

#include <iostream>

#ifdef _DEBUG

#define DEBUG_PRINT1(val) std::cout<<val<<std::endl;

#define DEBUG_PRINT2(val1,val2) std::cout<<val1<<" "<<val2<<std::endl;

#else

#define DEBUG_PRINT1(val) ;

#define DEBUG_PRINT2(val1,val2) ;

#endif

FileReader.h

#pragma once

#include <string>

namespace KP_FileReaderClass{

class FileReader

{

public:

FileReader();

virtual ~FileReader();

//anathema! access to internal data

int getFileContents(const std::string filename, std::string &contents);

private:

int ReadTheWholeFile(const std::string &filename);

std::string filecontents;

};

}

StringParserClass.h

#pragma once

#include <vector>

#include <string>

namespace KP_StringParserClass{

using ::std::string;

using ::std::vector;

class StringParserClass

{

public:

StringParserClass(void);

virtual ~StringParserClass(void);

//indicates the last error the library experienced

//resets error on completion, indicates a global variable that holds

//error state

int getLastError();

//these are the start tag and the end tags that we want to find,

//presumably the data of interest is between them

bool setTags(const char *pStartTag, const char *pEndTag);

//going to search thru pDataToSearchThru, looking for info bracketed by

//pStartTag and pEndTag, will add that data to myStrings

bool getDataBetweenTags(char *pDataToSearchThru, vector<string> &myVector);

private:

void cleanup();

//pStart points to where we want to search in array

bool findTag(char *pTagToLookFor, char *&pStart, char *&pEnd);

char *pStartTag;

char *pEndTag;

bool areTagsSet;

int lastError;

};

}

constants.h

#ifndef CONSTANTS_H_

#define CONSTANTS_H_

#include <string>

const int SUCCEEDED = 0;

const int USER_CHOSE_TO_EXIT = SUCCEEDED-1;

const int COULD_NOT_OPEN_FILE = SUCCEEDED-2;

const int COULD_NOT_CLOSE_FILE = SUCCEEDED-3;

const int COULD_NOT_READ_FILE_INTO_CONTAINER = SUCCEEDED-4;

const int COULD_NOT_WRITE_CONTAINER_TO_FILE = SUCCEEDED-5;

const int FILE_NOT_OPEN = SUCCEEDED-6;

//possible error conditions from parser

const int ERROR_NO_ERROR = SUCCEEDED -21;

const int ERROR_TAGS_NULL = SUCCEEDED -22;

const int ERROR_DATA_NULL = SUCCEEDED -23;

const double UNINITIALIZED = -1.0;

const std::string TEST_FILE_NAME = "..\include\Textfile.txt";

const std::string OUTPUTFILENAME = "..\include\OutputStrings.txt";

const std::string EXITCHAR = "X";

const std::string ENTER_FN_OR_X = "Please enter a filename or an X to eXit";

const char START_TAG[] ="<to>";

const char END_TAG[] ="</to>";

//enum whichTag{START_TAG,END_TAG};

#endif /* CONSTANTS_H_ */

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote