Diary 2: Looking at vectors, parsing strings and a command line interface that satisfies John's initial requirement. I dunno why I call this Diary lol - but its like, I am jotting down notes for myself so it feels like a Diary :) Ok now for what matters: Vectors, parsing strings and Command Line is something I have to revise to be able to write the code. VECTORS ------- Ok briefly, For those accustomed to arrays, vectors are dynamic arrays that increases and decreases in size without much coding from our part. They are analogous to "Collections" in VB (but not quite). Vectors are part of C++'s STL (Standard Template Library) Which I will assume is available on any/all C++ compilers. Templates is something unique to C++, apparently they are generic functions or classes (or something) that saves you typing :) Probably that is all I need to know for the time being. But definitely need to find the template class. The following snippet (based on C++'s complete reference) shows how to use vectors. #include #include #include // really don't know what this is for! maybe it goes with any STL // using namespace std; // why namespace is important here i don't know // int main() { vector v(0); // I want to test to see if we can start with length=0 // int i; for(i=0;i<10;i++) {v.push_back(i+10+'a');} cout << "Size = " << v.size() << endl; cout << "Contents:\n"; for(i=0;i >& std ::endl >(std::basic_ostream >&)' /home/henry/tmp/cc4rZNKG.o(.text+0x78): In function `main': : undefined reference to `std::cout' /home/henry/tmp/cc4rZNKG.o(.text+0x7d): In function `main': : undefined reference to `std::basic_ostream >& std ::operator<< >(std::basic_ostream >&, char const*)' [...... you get the picture .......] So our challenge is not only a coding one, but a practical essential one: How to compile a C++ program on Linux - which i never done! google search: Linux C++ newbie first program compiling Nice, I got a link about STL and lists (which are like vectors): http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html g++ is the compiling instruction! So the steps are: >g++ vectortest.cpp >./a.out [henry@localhost henry]$ ./a.out Size = 10 Contents: k l m n o p q r s t Wahhheyy!! Something happened! :) I was sanity-checking only to see if we are on the right track. All seems fine I have my template ------------------------------------ PARSING STRINGS: ---------------- Basically I need this: "Sentence_01|Sentence_02|Sentence_03|Sentence_04" gets split into element(0)="Sentence_01"; element(1)="Sentence_02"; element(2)="Sentence_03"; element(3)="Sentence_04"; Something very easy to do in other languages, but have to find out how to achieve that in C++ There are probably functions like that in STL that not only splits a sentence into array elements but also "eat up blank spaces"; From the yolink site I went into the LINKS section and the following links might be of interest, they just might have what I want: http://www.xraylith.wisc.edu/~khan/software/stl/STL.newbie.html http://www.davethehat.com/articles/tiny_stl.htm http://www.cs.brown.edu/people/jak/proglang/cpp/stltut/ And remember what I want is (eat_up_unecessary_spaces) and a function like array[] = split(string,delimiter) Not good - can't find anything there to parse There is a message: "Never use char* if you can help it!" - which i blindly took note. He explains why such procedure leads to bugs (of course I didn't read) search google: C++ parsing string array vector I found this site: http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html There I found a last snippet of code in the webpage which seemed suitable to experiment: I editted it: #include #include #include using namespace std; // again noted this line - its probably a standard thing in C++ int main() { vector tokens; string str("Sentence_01|Sentence_02|Sentence_03|Sentence_04"); Tokenize(str, tokens, "|"); int i; for(i=0;i& tokens, const string& delimiters = " ") { // Skip delimiters at beginning. string::size_type lastPos = str.find_first_not_of(delimiters, 0); // Find first "non-delimiter". string::size_type pos = str.find_first_of(delimiters, lastPos); while (string::npos != pos || string::npos != lastPos) { // Found a token, add it to the vector. tokens.push_back(str.substr(lastPos, pos - lastPos)); // Skip delimiters. Note the "not_of" lastPos = str.find_first_not_of(delimiters, pos); // Find next "non-delimiter" pos = str.find_first_of(delimiters, lastPos); } } I copied and pasted on my file parse.cpp let's see now: parse.cpp: In function `int main()': parse.cpp:37: error: `cout' undeclared (first use this function) parse.cpp:37: error: (Each undeclared identifier is reported only once for each function it appears in.) parse.cpp:39:2: warning: no newline at end of file I am clearly not paying much attention (after all just woke up!) Ok need and to fix the warning just add a new line at the end of the code. Brilliant no errors now. Lets try ./a.out Sentence_01 Sentence_02 Sentence_03 Sentence_04 Ok lets change the code so that it does that per line (which was my initial intention) Also took out the redundant last line So the int main will look like this now: int main() { vector tokens; string str("Sentence_01|Sentence_02|Sentence_03|Sentence_04"); Tokenize(str, tokens, "|"); int i; for(i=0;i are my notes): Command Line Arguments grim -c ::: Display the number of messages only. (e.g. 6 messages. 3 unread.) > Oh dear .. read and unread messages forgot about that :S > OK will carry on with the simplest route and then expand with extras > Otherwise the castle of cards will collapse (ie my brain will go tilt!) grim -v [all (default); read; unread] ::: View message titles only. Options self-explanitory. > Similar thing as above grim -r [all (default); number of message(s); unread; read] ::: Lists the message titles and bodies. With number of message (or messages.. e.g grim -r 1 3). Can use with range options e.g. grim -r 1-3 6 8-10 (reads 1,2,3,6,8,9,10). Can't use numbers with unread - will list all unread. > confused here ... Oh, wait I think I get it: grim -r 1 2 4 will list: 1 Cook Dinner 2 Phone Betty 4 Buy beer this will be a very complex implementation - specially the range part. So first will *try* and make the basic program (laying somewhat the foundations) and then add the extra functionalities later. grim -w ::: Write new message. > ok grim -e [number of message] ::: Edit message. e.g grim -e 2 (edit message 2) > Need cin and cout .. OK grim -d [all; number of message(s); read; unread] ::: Delete message. With 'all', delete all messages; '[un]read', delete all [un]read messages; number, that message or range of messages (much like grim -r) > ok, and possibly need a confirmation before deleting. grim --help ::: Display help. > :) grim --version ::: Display version number. > :o grim --about ::: Display about message. > :D I spent a lot of time today on this. More than the alotted 15-30 minutes. So will try and review this last part "Command Line Interface" in the next set of Notes: Diary3