Also, for anyone working with the code base, I wrote a small application that reads through a text file you provide and spruces the file up (convert tabs to spaces, trim whitespace from the end of lines, etc). If you want to keep consistency, we have our lobby.lua set to a tab size of 4 spaces (the default in most editors). The current diff is giant, because of how inconsistent our week of work has made it, but hopefully it stays consistent from here on out.
- trimmer.jpg (37.87 KiB) Viewed 3730 times
The code, for the suspicious:
- Code: Select all
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int option, sCount, seqCount, spacesPerTab, tabCount;
vector<string> lines;
string convertSpacesToTabs(int spacesPerTab, string str) {
int curSpaceSeq;
string spaces;
// Get a string of spaces of length spacesPerTab
for (int i = 0; i < spacesPerTab; i++) {
spaces += " ";
}
// Try to find a space sequence in the string
curSpaceSeq = str.find(spaces);
while (curSpaceSeq > -1 ) {
str = str.replace(curSpaceSeq, spaces.length(), "\t");
curSpaceSeq = str.find(spaces);
seqCount++;
}
return str;
}
string convertTabsToSpaces(int spacesPerTab, string str) {
int currentTab;
string spaces;
// Get a string of spaces of length spacesPerTab
for (int i = 0; i < spacesPerTab; i++) {
spaces += " ";
}
// Try to find a tab in the string
currentTab = str.find("\t");
// Ensure tab is found, do the replacement
if (tabCount > -1) {
while (currentTab != -1) {
str = str.replace(currentTab, 1, spaces);
currentTab = str.find("\t");
tabCount++;
}
}
return str;
}
string trim(string str) {
int sLoc = str.find_last_of(' ');
int lineSpaceCount = 0;
int origStrLength = str.length();
// If there is a space at the end of str, run the loop:
while (sLoc == str.length() - 1 && sLoc != 0 && sLoc != -1) {
str = str.substr(0, sLoc);
sLoc = str.find_last_of(' ');
lineSpaceCount++;
}
if (sLoc == 0 && lineSpaceCount == origStrLength - 1) {
str = "";
lineSpaceCount++;
}
sCount += lineSpaceCount;
return str;
}
void readFile(char fn[]) {
fstream FI(fn, fstream::in);
string line;
while (FI.good()) {
getline(FI, line);
line = trim(line);
if (option == 1) {
line = convertTabsToSpaces(spacesPerTab, line);
} else if (option == 2) {
line = convertSpacesToTabs(spacesPerTab, line);
} else {
cout << "You must choose option 1 or 2." << endl << "Option was: ";
cout << option << endl;
}
lines.push_back(line);
}
cout << "File " << endl << " " << fn << endl << "read successfully.";
cout << endl;
return;
}
void writeFile(char fn[]) {
fstream FO(fn, fstream::out);
for (vector<string>::iterator i = lines.begin(); i != lines.end(); i++) {
FO << *i << endl;
}
cout << "File" << endl << " " << fn << endl;
cout << "written successfully." << endl;
}
int main() {
char *fn;
string fN;
sCount = 0;
option = 0;
cout << "Enter the number of spaces per tab: ";
cin >> spacesPerTab;
cout << "Select an option:" << endl << " 1-Replace tabs with spaces";
cout << endl << " 2-Replace spaces with tabs" << endl;
cout << "Option: ";
cin >> option;
cout << "Enter a file name to read (can include path): " << endl;
getline(cin, fN);
getline(cin, fN);
fn = (char*)fN.c_str();
readFile(fn);
if (sCount == 0 && tabCount == 0 && seqCount == 0) {
cout << "There were no changes made to your file." << endl;
cout << "You do not need to save a new file!" << endl;
} else {
cout << "Spaces removed: " << sCount << endl;
if (option == 1) {
cout << "Tabs replaced: " << tabCount << endl;
} else if (option == 2) {
cout << "Space sequences replaced: " << seqCount << endl;
}
cout << "End of processing." << endl << endl;
cout << "Enter a file name to write to (can include path): " << endl;
getline(cin, fN);
fn = (char*)fN.c_str();
writeFile(fn);
}
return 0;
}
EDIT: Fixed my derp with the trim() function (even better now) - thanks rootbeer.
EDIT (again): Debug strings are awesome.
EDIT (yet again): Adding missing MinGW compiler library