summaryrefslogtreecommitdiffstats
path: root/C++/String Processing/String Processing.cpp
blob: 4e35a8007973b892bf9d4a4ab3a4939a06f9c340 (plain) (blame)
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
78
79
80
81
82
83
84
85
86
87
88
// Name: msglm
// Date: September 2nd, 2022
// Program Name: HTML Parser
// Description: removes HTML from inputted string.

//any code with /// is merely commented out debug code.

#include <iostream>
#include <string>
using namespace std;

//Takes input from user  
string in() {
    //I personally dislike C++'s streaming capabilities
    //They aren't like Bash pipes and that's a blunder
    //I wish I could do getline(cin, '\n') >> return; or getline(cin, '\n') | return;
    string userInput;
    getline(cin, userInput, '\n');
    return userInput;
}

//processes the input data 
//this is the only process in the entire program thus the 
//term "proc" is accurate. We don't process data in in() or out().
//
//How this works is it takes a string, and goes through it char-by-char, 
//once it hits a < it notes its position and marks a flag
//then it does the same for the next >, marking its pos and marking a flag. 
//once the < and > flags have been raised, the total
//length of the tag that was detected is calculated, and then the string itself is sliced
//
//from there a small bit of clean up is necessary, restarting 
//the position counter to count from the start
//and resetting the flags.

string proc(string userInput) {
    bool insideATag = false;
    bool metTheTagTerminator = false;
    int beginBracketPos;
    int endBracketPos;
    int tagLength;

    for (int charPos=0; charPos<=userInput.length();charPos++) {
        if (insideATag && metTheTagTerminator) {
            ///cout << "tag found at " << beginBracketPos << " and " << endBracketPos << endl; 
            ///cout << "Full String:" << userInput << endl;
            
            //Tag length only exists because std::String.erase() doesn't accept
            //a range, but rather a start and the chars to go. it's accursed.
            tagLength = (endBracketPos-beginBracketPos)+1;
            userInput.erase(beginBracketPos, tagLength);
            charPos = 0;
            insideATag = false;
            metTheTagTerminator = false;
        }
        if (userInput[charPos] == '<') {
            insideATag = true;
            beginBracketPos = charPos;
        }

        if (userInput[charPos] == '>' && insideATag) {
            metTheTagTerminator = true;
            endBracketPos = charPos;
        }

        ///cout << "char pos: " << charPos << ";" << "char: " << userInput[charPos] << endl; 

    }
    return userInput;
}

//Outs to screen
void out(string processedString) {
    cout << "sanitized: " << processedString << endl;
}

int main() {
    //LISP called, they want their () back.
    out(proc(in()));
}

/*
   This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License Version 3 ONLY as published by the Free Software Foundation.

   This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License along with this program.  If not, see <https://www.gnu.org/licenses/>.
   */