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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#include "TText.h"
using namespace std;
Indexed_string::Indexed_string()
{
}
Indexed_string::Indexed_string(std::string i_string, unsigned int i_Index, bool i_is_data)
{ iString = i_string;
Index = i_Index;
is_data = i_is_data;
}
Indexed_string::~Indexed_string()
{ iString.clear();
}
TText::TText(void)
{
}
TText::TText(std::string exe_path, std::string file_offset)
{ data_index = 0;
is_read = false;
file_path = "NULL";
Load_file(exe_path.append(file_offset));
}
void TText::Load_file(std::string file_offset)
{ ifstream file (file_offset);
if (file.is_open())
{ is_read = true; // Indicate that the file is now copied to memory, and all has not gone to hell in a handbasket
file_path = file_offset;
std::string line;
unsigned int counter = 0;
while (std::getline(file,line))
{ Process_input(line, counter, false);
counter++; // Nom, nom, nom, tasty bytes of data
} line.clear();
}
file.close();
}
void TText::Process_input(std::string line, unsigned int counter, bool Override_flag)
{ std::string comp_line = line;
std::vector<Indexed_string>::iterator it;
transform (comp_line.begin (), comp_line.end (), comp_line.begin (), toupper);
if(Override_flag == true)
{ text_index.emplace_back(Indexed_string(line, counter, true));
}
else
{ if (((comp_line == "BEGIN"))&&(i_flag == false))
{ i_flag = true; // Flip the read switch on, now the program will begin to accept text values each line & push their index back
text_index.emplace_back(Indexed_string(line, counter, false));
}
else if (((comp_line == "END"))&&(i_flag == true))
{ i_flag = false; // Flip the read switch off at the end of the text block to close off the input flow
text_index.emplace_back(Indexed_string(line, counter, false)); // Note that the Begin and End statements are ignored by the parser as well, assuming they are flipping i_flag
} // its like water, except the parts of it which are not like water, which is all of it
else if ((comp_line.front() == '/')&&(comp_line.at(1) == '/'))
{ text_index.emplace_back(Indexed_string(line, counter, false));
} // Commented!!! Sadly only effective on full line comments, but who cares!
else if (i_flag == true)
{ text_index.emplace_back(Indexed_string(line, counter, true));
data_index++;
} // The good stuff :)
else
{ text_index.emplace_back(Indexed_string(line, counter, false));
} // Not commented or a parser flag, but not relevant data if not included in a read block
}
file_index++;
}
void TText::Insert_data(std::vector<std::string> i)
{ Process_input("Begin", (file_index+1), false);
for(std::vector<std::string>::iterator it = i.begin(); it != i.end(); ++it)
{ Process_input(*it,(file_index+1), true);
}
Process_input("End", (file_index+1), false);
}
void TText::Insert_data(std::string i)
{ Process_input("Begin", (file_index+1), false);
Process_input(i,(file_index+1), true);
Process_input("End", (file_index+1), false);
}
std::string TText::Access_data(unsigned int index)
{ unsigned int cy = 0;
for(std::vector<Indexed_string>::iterator it = text_index.begin(); it != text_index.end(); ++it)
{ if (it->is_data == true)
{ if (cy == index)
{ const std::string output = it->iString;
return output;
}
else
{ cy++;
}
}
}
return "BAD_INDEX";
}
void TText::Output_Data()
{ for(std::vector<Indexed_string>::iterator it = text_index.begin(); it != text_index.end(); ++it)
{ cout << it->Index << " " << it->is_data << " " << it->iString << endl;
}
}
std::string TText::Get_path()
{ return file_path;
}
unsigned int TText::Get_data_index()
{ return data_index;
} // Pretty much what it says on the box.
unsigned int TText::Get_file_index()
{ return file_index;
} // Pretty much what it says on the llama
bool TText::Read_state()
{ return is_read;
} // A check on whether the read was successful
bool TText::Check_file_exists()
{ bool Check_return = false;
ifstream file (file_path);
if(file.is_open())
{ Check_return = true;
} file.close();
return Check_return;
}
bool TText::Save_file(std::string file_path, bool selfdestruct)
{ switch(Check_file_exists())
{ case true:
{ std::remove(file_path.c_str());
ofstream file (file_path);
if (file.is_open())
{ for (std::vector<Indexed_string>::iterator it = text_index.begin(); it != text_index.end(); ++it)
{ file << ((it->iString).append("\n"));
}
}
file.close();
// It read funny without a comment in here.
if (selfdestruct == true)
{ this->~TText();
}
else
{ return true;
}
}
case false:
{ return false;
}
}
}
TText::~TText(void)
{ text_index.clear();
data_index = 0;
i_flag = false; // I honestly have no idea why
is_read = false;
}
|