Red Squirrel
No Lifer
So I sorta got this working but my work computer does not have enough ram to compile it with the entire list so have not tested it fully yet.
randword.cpp
genwordlist.cpp:
Essentially there's two parts to this, first there is the genwordlist.cpp program which generates the code that contains the list of words. It's basically just a bunch of entries that append the word to a global vector. This is included separately as words.cpp.
Once that file is generated then you compile randomword.cpp which then creates the binary which works stand alone.
The word list I used is the "csv" that has only the words that was linked here. Turns out it's not really a CSV and just a list of words one per line, but that made it easier to parse out. There is lot of garbage and duplicates though, I didn't do much to deal with that except remove characters that broke compilation. I only tested up to D so not sure if there may be other weird stuff going on the other files that I didn't account for.
There is probably a much better way to do this and trying to pack the data into the executable may not be the best approach or at least not the way I did it, but I was curious to try it anyway. It works, but you just need a computer built in this millennium to compile it on.
Though considering the whole thing is only like 50k lines of very simple code (only going up to D in the list) I am not sure why it's this hard on the ram to compile as I've compiled much larger projects before, some around 500k lines of code in a VM with about same amount of ram. Though maybe it has to do with trying to compile a single file that is that size.
The resulting file size is also kinda big, but using -O2 optimization seems to yield the smaller size of 998k for the A list only.
I spent a bit more time that I care to admit on this silly thing, but it passed some time at work and let me brush up on C++ a little. Been a while. I could have used python or php or something but was curious to try a compiled language.
randword.cpp
Code:
#include <iostream>
#include <vector>
#include <time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
using namespace std;
vector<string> m_words;
int main()
{
//add words from file generated by genwordlist:
#include "words.cpp"
//ex entry: m_words.push_back("word");
//pick random word:
int processid = getpid(); //this should be a diff number each time it's ran.
srand (time(NULL)+processid); //seed random number generator
int size = m_words.size(); //get size of array
int randnum = rand()%size; //generate the number
//display random word:
cout<<m_words[randnum]<<endl;
return 0;
}
genwordlist.cpp:
Code:
#include <iostream>
#include <fstream>
using namespace std;
void ProcessWordFile(string filename)
{
ofstream wordlistout;
ifstream wordlistin;
string tmpline="";
string tmpline_clean="";
wordlistout.open("words.cpp",ios::app);
wordlistin.open(filename);
while (getline(wordlistin,tmpline))
{
//Sanitize pass 1:
tmpline_clean="";
for(int i=0;i<(tmpline.length());i++)
{
//getline keeps the return, just remove both CR and NL to cover our bases:
if(tmpline[i]=='\r' || tmpline[i]=='\n')
{
continue;
}
//Remove invalid chars that cause problems with compile:
if(tmpline[i]=='\\' || tmpline[i]=='"')
{
continue;
}
//append char to a new string:
tmpline_clean.push_back(tmpline[i]);
}
//Sanitize pass 2:
tmpline=tmpline_clean;
tmpline_clean="";
for(int i=0;i<(tmpline.length());i++)
{
//the list seems to have a white space after each word, this gets rid of it:
//reasoning to do this as a 2nd pass is so it's easier to know if space is at the end.
if(tmpline[i]==0x20 && i==(tmpline.length()-1))
{
continue;
}
//append char to a new string:
tmpline_clean.push_back(tmpline[i]);
}
wordlistout<<"m_words.push_back(\""<<tmpline_clean<<"\");\r\n";
}
wordlistout.close();
wordlistin.close();
}
int main()
{
remove("words.cpp"); //delete word list cpp file
ProcessWordFile("csv/Aword.csv");
ProcessWordFile("csv/Bword.csv");
ProcessWordFile("csv/Cword.csv");
ProcessWordFile("csv/Dword.csv");
//ProcessWordFile("csv/Eword.csv");
//ProcessWordFile("csv/Fword.csv");
//ProcessWordFile("csv/Gword.csv");
//ProcessWordFile("csv/Hword.csv");
//ProcessWordFile("csv/Iword.csv");
//ProcessWordFile("csv/Jword.csv");
//ProcessWordFile("csv/Kword.csv");
//ProcessWordFile("csv/Lword.csv");
//ProcessWordFile("csv/Mword.csv");
//ProcessWordFile("csv/Nword.csv");
//ProcessWordFile("csv/Oword.csv");
//ProcessWordFile("csv/Pword.csv");
//ProcessWordFile("csv/Qword.csv");
//ProcessWordFile("csv/Rword.csv");
//ProcessWordFile("csv/Sword.csv");
//ProcessWordFile("csv/Tword.csv");
//ProcessWordFile("csv/Uword.csv");
//ProcessWordFile("csv/Vword.csv");
//ProcessWordFile("csv/Wword.csv");
//ProcessWordFile("csv/Xword.csv");
//ProcessWordFile("csv/Yword.csv");
//ProcessWordFile("csv/Zword.csv");
return 0;
}
Essentially there's two parts to this, first there is the genwordlist.cpp program which generates the code that contains the list of words. It's basically just a bunch of entries that append the word to a global vector. This is included separately as words.cpp.
Once that file is generated then you compile randomword.cpp which then creates the binary which works stand alone.
The word list I used is the "csv" that has only the words that was linked here. Turns out it's not really a CSV and just a list of words one per line, but that made it easier to parse out. There is lot of garbage and duplicates though, I didn't do much to deal with that except remove characters that broke compilation. I only tested up to D so not sure if there may be other weird stuff going on the other files that I didn't account for.
There is probably a much better way to do this and trying to pack the data into the executable may not be the best approach or at least not the way I did it, but I was curious to try it anyway. It works, but you just need a computer built in this millennium to compile it on.
Though considering the whole thing is only like 50k lines of very simple code (only going up to D in the list) I am not sure why it's this hard on the ram to compile as I've compiled much larger projects before, some around 500k lines of code in a VM with about same amount of ram. Though maybe it has to do with trying to compile a single file that is that size.
The resulting file size is also kinda big, but using -O2 optimization seems to yield the smaller size of 998k for the A list only.
I spent a bit more time that I care to admit on this silly thing, but it passed some time at work and let me brush up on C++ a little. Been a while. I could have used python or php or something but was curious to try a compiled language.