1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#include <iostream>
#include <fstream>
#include <string>
#include "substMap.h"
using namespace std;
/*----------------------------------------------------------
GETINPUT
~SETS A NEW VARIABLE CALLED "userInput" AS A STRING
-GETS THE CONSTANT SUBSTITUE ITERATOR AND SETS IT TO
"it" THEN IS EQUAL TO THEMAP BEGINING AND "it" IS
NOT EQUAL TO THEMAP END FUNCTION AND "it" GOES UP
BY ONE
-PRINTS OUT THE FIRST PART OF THEMAP FROM "it"
~NEED TO SET THE DESCRIPTION OF EACH TOKEN STORED AND CHANGE
FIRST TO SECOND AND GET USERINPUT
------------------------------------------------------------*/
void getInput(SubstMap& theMap)
{
string userInput;
for(ConstSubstIter it = theMap.begin(); it != theMap.end(); it++)
{
cout << it->first << endl;
}
}
/*--------------------------------------------------------------------------
FINDSUBS
~SETS THREE NEW VARIABLE
-"token" TO A STRING
-SIZE_TYPE "start"(0) TO A STRING
-SIZE_TYPE "end" TO A STRING
~END IS EQUAL TO ONELINE AND FIND_FIRST_OF THE "@" AND START
-CHECKS TO SEE IF THE END IS NOT AT THE END OF THE STRING WITH "npos"
~TOKEN IS SET TO GET THE ONELINE FROM USING SUBSTR(SUBSTRING)
FROM START TO END MINUIS THE START
~START IS THEN SET TO END PLUS ONE
~THEN END IS RESET WITH THE UPDATED START POSITION
-CHECKS THEMAP TO SEE IF IT IF FINDS A TOKEN AND IS NOT EQUAL TO THE
TO THEMAP END FUNCTION
~IF FINDS THE TOKEN IN THEMAP, IT DOESNT DO ANYTHING, SINCE IT IS
ALREADY FOUND
~IF IT DOESN'T FIND THE TOKEN, IT THEN ADDS IT TO THEMAP
----------------------------------------------------------------------------*/
void findSubs(string& oneLine, SubstMap& theMap )
{
/* FINDS EACH SUB IN THE TEXT */
string token;
string::size_type start(0);
string::size_type end;
end = oneLine.find_first_of( "@", start);
/* WHILE IT IS NOT AT THE END OF THE STRING */
while (end != string::npos)
{
token = oneLine.substr( (start), (end - start) );
int pos = end + 1;
start = pos;
end = oneLine.find_first_of("@", start);
if (theMap.find(token) != theMap.end())
{
}
else
{
theMap[ token ] = "";
|
}
}
}
/*-----------------------------------------------------------------------------
FIRSTPASS
~SETS A NEW VARIABLE "oneLine" TO A STRING
~CHECKS TO SEE IF THE STREAM IS NOT AT THE END OF FILE
-USE GETLINE TO READ A LINE AT A TIME WITH ONELINE
-CHECKS IF THE STREAM FAILS
~IT THEN CLEARS THE FAIL
~THEN RETRIES THE FIRSTPASS AGAIN
-THEN IT CALLS THE FINDSUBS WITH ONELINE AND THEMAP AS THE PARAMETERS
-------------------------------------------------------------------------------*/
void firstPass( ifstream& strm, SubstMap& theMap )
{
string oneLine;
/* CHECKS TO SEE IF THE FILE IS NOT AT THE END */
while (!strm.eof())
{
getline(strm, oneLine);
/* GETS RID OF THE ERRORS */
if (strm.fail())
{
strm.clear();
strm.ignore(1024, '\n');
continue;
}
findSubs(oneLine, theMap);
}
}
/*------------------------------------------------------------------
MAIN
~SETS THE STREAM TO ADD IT TO THE COMMAND LINE
-CHECKS TO SEE IF THE FILE IS NOT GOOD
~PRINTS ERROR MESSAGE IF TRUE AND EXITS THE PROGRAM
~SETS THE SUBSTITUTION MAP TO "theMap"
~CALLS THE FIRSTPASS WITH THE STREAM AND THEMAP AS THE PARAMETERS
~THEN CALLS THE GETINPUT WITH THEMAP AS THE PARAMETERS
---------------------------------------------------------------------*/
int main(int argc, char* argv[])
{
ifstream strm( argv[1] );
if ( !strm.good() )
{
cout << "File is not good";
exit(1);
}
SubstMap theMap;
firstPass( strm, theMap );
getInput(theMap);
return 0;
} |