So I’ve been stuck at home sick the last few days. I had a bit of food poisoning I think, or I caught a piece of the stomach bug from someone else. Overall it didn’t affect me to terribly, just bad for one day and the next I was mostly better. While I was at home sick, I did some programming. I have been working to create a program that handles apache virtual host configuration files and while in the middle of that, I wrote myself a nice library that I decided I should share with everyone. It handles file input and output for you basically. It reads a file into a buffer of a length that you specify in the header file, and puts it into RAM. From there you have functions for inserting, finding, or replacing string text before it is written back to a file all at once.
I decided this would be handy because most of the work I do with files is configuration files, or files that are not exceedingly long and are almost always plain text. Now if I want to implement configuration files in a program it’s not so bad, I can just include the library and create a config file with the directives I need and off we go.
I don’t know if anyone else is really going to find this of use, but here you go. here is the source code for the header file:
// Filebuffer header file Version 1.0 2014 DoogieLabs.
// This is a header file that will read the entire contents of a plaintext file into
// an array of strings that you define the length of using the FILEBUFFLINES directive.
#define FILEBUFFLINES 24
// ———- Functions and how they’re used: ————
//
// readfile(filename) – Read the entire contents of the file passed to it and load it into filebuffer
// writefile(filename) – Write the contents of buffer to the specificed file name
// findstring(search) – Search for text, returns numbers in searchcolumn, and searchline vars
// replacestring(search, replace) – search for text, and replace it with text
// insertstring(search, text) – Search for text, and insert more text immediately after search result
// printbuffer() – Preview text file buffer filebuffer[n] before output is written
//
// ———– Variables and how they’re used: ———–
//
// filebuffer[linenumber] – Filebuffer is a series of strings [linenumber] corresponds to line
// numbers in a file. This is where you manipulate contents of files directly.
//
// searchline – The linenumber determined in a search function
// searchcolumn – The exact char location results of a search
// ———— Minimum required include files ———–
// iostream, fstream, cstdlib, string
//
using namespace std;// Variable Space
string filebuffer[FILEBUFFLINES];
size_t searchline; // X and Y coordinates for the search / replace
size_t searchcolumn;// Read file into buffer
void readfile(string filename){ // Read file into buffer
ifstream inf;
inf.open(filename.c_str() ); //Attempt to open file
if(!inf){
cerr << “Error – Couldn’t open file.\n”;
exit(1);
}
for (int i=0; i<FILEBUFFLINES; i++){ //As long as we are within the buffer size
if(inf.eof() == true){ // and we’re not at the end of the file
return;
}
getline (inf, filebuffer[i]); // Fill the next line in the buffer}
inf.close();
return;
}// Write enire file buffer contents to disk
void writefile(string filename){
ifstream chkf;
chkf.open(filename.c_str());
if(!chkf){
cerr << “File does not exist! Creating..\n”;
string createfile;
createfile = “touch ” + filename;
system(createfile.c_str());
}
ofstream outf;
outf.open(filename.c_str() ); //Open the file, error if it doesnt open
if(!outf){
cerr << “Error opening file for writing\n”;
exit(1);
}
for(int i=0; i<FILEBUFFLINES; i++){
outf << filebuffer[i] << endl;
}
outf.close();
return;
}//Find text within the buffer
void findstring(string search){
for(searchline = 0; (searchline<FILEBUFFLINES); searchline++){
searchcolumn = 0;
searchcolumn = filebuffer[searchline].find(search); //Determine the x coordinate
if(filebuffer[searchline].find(search) != search.npos){ //Stop after instance found and return
return;
}
}
return;
}// Replace text within the buffer
void replacestring(string search, string text){
findstring(search);
//Position and overwrite text
filebuffer[searchline].replace(searchcolumn, text.length(), text);
return;
}// Insert text into a part of the buffer
void insertstring(string search, string text){
findstring(search);
// Add positions together so it starts insterting just AFTER searched text
searchcolumn = searchcolumn + search.length();
filebuffer[searchline].insert(searchcolumn, text); // Perform the insert
return;
}//Dump the contents of the buffer to the screen
void printbuffer(){
for(int i=0; i<FILEBUFFLINES; i++){
if(filebuffer[i] != “”){
cout << filebuffer[i] << endl;
}
}return;
}
You can also download the header file here, if it doesn’t open in notepad formatted right, use wordpad: (Its in UNIX format) FileBuffer Header Download HERE
If anyone has any suggestions on streamlining this file, or would like to point out mistakes I definitely encourage that as I am what I’d consider a novice programmer. Your tips and help are always greatly appreciated!