makefile
FLAGS = -Os MYAPP = appname all: $(MYAPP) $(MYAPP): $(MYAPP).o hbstring.o g++ $(MYAPP).o -o $(MYAPP) -static-libgcc -static-libstdc++ $(MYAPP).o: $(MYAPP).cpp g++ -c -g -std=c++0x $(MYAPP).cpp clean: rm *.o $(MYAPP)
map / hash / dictionary
#include <map> ... std::map <std::string, int> mymap; for(auto itr = mymap.begin(); itr != mymap.end(); itr++){ std::string key = itr->first; int value = itr->second; }
Determine type of File system Object a given string is, including "none" :
enum FSO_TYPE {none, file, dir, dot}; FSO_TYPE fsotype(std::string); ... ... ... FSO_TYPE fsotype(std::string str) { if(str == "." || str == "..") return dot; struct stat sb; if(stat(str.c_str(), &sb) == 0){ // the string does represent some kind of non-parent, non-self filesystem object if(S_ISDIR(sb.st_mode)){ return dir; } else{ return file; } } else{ return none; } }
Check for the existence of an image file with a given base name:
std::string getimage(std::string searchfolder, std::string basename); ... ... ... std::string getimage(std::string searchfolder, std::string basename) { std::vector <std::string> extensions; extensions.push_back(".jpg"); extensions.push_back(".jpeg"); extensions.push_back(".png"); extensions.push_back(".webp"); extensions.push_back(".tiff"); for(auto & ext: extensions){ std::string testpath = searchfolder + "/" + basename + ext; if(fsotype(testpath) == file){ return testpath; } } return "" }
Get the names of all of a folder's files and subfolders, sort them, and process the sorted vectors of names:
// you probably want to changedir to make life easier // remember, you are only getting back the base names of the FSOs // so you might want to be sitting in their parent folder // as you process them. // Either that, or you will need to build the path when you reference the FSO names in the file system chdir("root folder name"); DIR* folder = opendir("."); vector <std::string> folders; vector <std::string> files; struct dirent *folderitem; while((folderitem = readdir(folder)) != NULL){ std::string fsoname = folderitem->d_name; if(fsotype(fsoname) == file){ files.push_back(fsoname); } else if (fsotype(fsoname) == dir){ folders.push_back(fsoname); } } closedir(folder); sort(files.begin(), files.end()); sort(folders.begin(), folders.end()); for(auto & folder: folders){ cout << folder << endl; } for(auto & file: files){ cout << file << endl; }
Read a file and write to a file
#include <stdio> #include <iostream> #include <fstream> char line[9999]; FILE *infile; infile = fopen("TheMonstersKnow.txt", "r"); std::ofstream outfile; outfile.open("out.txt", ofstream::out); while(fgets(line, 9999, infile) != NULL){ line[std::strcspn(line, "\n")] = 0; // remove \n from the end hbtrim(line); outfile << line << std::endl; } fclose(infile); outfile.close(); ... ... ... void hbtrim(char* sz) { char temp[9999]; int n= 0; while(sz[n] == ' '){ n++; } strcpy(temp, sz + n); int l = strlen(temp); while(temp[l - 1] == ' '){ temp[l - 1] = 0; l--; } strcpy(sz, temp); }
ncurses
#include <ncurses.h> // init ncurses initscr(); clear(); // use ncurses, do not mix with cout output move(line, row); // zero-based printw("%d) string with printf formatting %s", n, "in this example menu entry"); move(line + 1, 0); // put the cursor for input on the next line refresh(); int ch = getch(); // gets the next keystroke char c = ch; // necessary? going to treat c/ch only a a char int a = c - '0'; if(a >= 0 && a <= 9){ move(line + 2, 0) ; printw("You entered a digit"); } // cleanup endwin(); makefile: -lncurses
mysql
I think executeQuery is only for non-update/insert queries (so they return database information), and executeUpdate is for update/insert queries. Maybe execute is generic for either?
compile linking: -lmysqlcppconn
#include <mysql/jdbc.h> ... ... ... sql::mysql::MySQL_Driver *driver; sql::Connection *connection; driver = sql::mysql::get_mysql_driver_instance(); connection = driver->connect("tcp://10.0.0.51:3306", "sean", "batman"); sql::Statement *statement; sql::ResultSet *queryresults; statement = connection->createStatement(); statement->execute("USE hubris"); queryresults = statement->executeQuery("select name, fullname from creatures where name like '%Dragon' order by name"); int resultcount = queryresults->rowsCount(); while(queryresults->next()){ string name = queryresults->getString("name"); string fullname = queryresults->getString("fullname"); } statement->executeUpdate("UPDATE hubris.creatures SET link = "some url" WHERE name = 'stormGiant'");
Remove the newline char at the end of a string from fgets
while(fgets(line, 9999, infile) != NULL){ line[strcspn(line, "\n")] = 0;
Remove unwanted characters from a string, and control capitalization.
enum hbcase {upper, lower, asis}; std::string styleString(std::string, char*, hbcase, hbcase, hbcase); ... ... ... std::string styleString(std::string strsrc, char* unwanted, hbcase firstchar, hbcase newword, hbcase therest) { char retstr[9999]; int c2 = 0; char src[9999]; strcpy(src, strsrc.c_str()); int l = strlen(src); switch(firstchar){ case upper: src[0] = toupper(src[0]); break; case lower: src[0] = tolower(src[0]); break; }; for(int c = 0; c < l; c++){ bool skip = false; for(int cc = 0; cc < strlen(unwanted); cc++){ if(src[c] == unwanted[cc]){ skip = true; } } if(!skip){ retstr[c2] = (src[c]); if(c && (src[c - 1] == ' ' || src[c - 1] == '-')){ switch(newword){ case upper: retstr[c2] = toupper(retstr[c2]); break; case lower: retstr[c2] = tolower(retstr[c2]); break; }; } else{ switch(therest){ case upper: retstr[c2] = toupper(retstr[c2]); break; case lower: retstr[c2] = tolower(retstr[c2]); break; }; } c2++; } } retstr[c2] = 0; return (std::string) retstr; }
DESC
CODE