1 /*
  2  * =====================================================================================
  3  *
  4  *       Filename:  =linklist_main.cpp
  5  *
  6  *    Description:  Test Program for Link List with MYSQL
  7  *
  8  *        Version:  1.0
  9  *        Created:  03/29/2011 03:32:04 PM
 10  *       Revision:  none
 11  *       Compiler:  gcc
 12  *
 13  *         Author:  Ruben Safir,
 14  *        Company:  NYLXS
 15  *
 16  * =====================================================================================
 17  */
 18
 19 #ifndef  LINKLIST
 20 #define LINKLIST
 21 #include        "linklist.h"
 22 #include        "message.h"
 23 #endif     /* -----  not LINKLIST  ----- */
 24
 25 #include        <iostream>
 26 #include        <fstream>
 27 #include        <climits>
 28 #include        <cstdlib>
 29 #include        <cstdio>
 30 #include        <string>
 31 #include        <iomanip>
 32 #include        <mysql++.h>
 33
 34 template<typename T>
 35 void start_list_with_five_things(T,T,T,T,T);
 36
 37 template<typename T>
 38 void add_to_list_with_five_things(T,T,T,T,T);
 39
 40
 41 template<typename T>
 42 void add_to_list_with_five_things_then_find(T,T,T,T,T);
 43
 44
 45         template <class T>
 46 void find_all_examples (chainlist::List<T>&, T const &  );
 47
 48 chainlist::List<mess::MESSAGE> get_messages();
 49 chainlist::Node<std::string> * findtest(std::string);
 50 chainlist::List<int>& creating_int_list();
 51
 52 /* 
 53  * ===  FUNCTION  ======================================================================
 54  *         Name:  main
 55  *  Description:  
 56  * =====================================================================================
 57  */
 58 int main ( int argc, char *argv[] )
 59 {
 60         
 61         chainlist::Node<std::string> * string_node;
 62         chainlist::List<int> myintlist;
 63
 64
 65         std::string search_string = "His reputation for a ready-moneyed man, who would lend money out for a";
 66         string_node = findtest(search_string);
 67         if(string_node){
 68                 std::cout << "************************************************************FOND NODE***********" << std::endl;
 69                 std::cout       << "With Value ==> '" << search_string << "'" << std::endl;
 70                 
 71                 std::cout       << "Node is ==> '"<< string_node << "' Value is ==> '" << *(string_node->value()) << "'" << std::endl;
 72         }else{
 73                 std::cout << "***********************************Not Found************************************" << std::endl;
 74                 std::cout << search_string << std::endl;
 75         }
 76
 77         
 78         search_string = "throughout New England, of \"The devil and Tom Walker.\"";
 79
 80         string_node = findtest(search_string);
 81         if(string_node){
 82                 std::cout << "************************************************************FOND NODE***********" << std::endl;
 83                 std::cout       << "With Value ==> '" << search_string << "'" << std::endl;
 84                 
 85                 std::cout       << "Node is ==> '"<< string_node << "' Value is ==> '" << *(string_node->value()) << "'" << std::endl;
 86         }else{
 87                 std::cout << "***********************************Not Found************************************" << std::endl;
 88                 std::cout << search_string << std::endl;
 89         }
 90
 91         search_string = "This is never going to be found";
 92         string_node = findtest(search_string);
 93         if(string_node){
 94                 std::cout << "************************************************************FOND NODE***********" << std::endl;
 95                 
 96                 std::cout       << "With Value ==> '"<<  search_string << "'" << std::endl;
 97                 std::cout       << "Node is ==> '"<< string_node << "' Value is ==> '" << *(string_node->value()) << "'" << std::endl;
 98         }else{
 99                 std::cout << "***********************************Not Found************************************" << std::endl;
100                 std::cout << search_string << std::endl;
101         }
102
103         
104         
105         start_list_with_five_things<std::string>("See", "Spot", "Run", "Run", "Fast!!");
106         start_list_with_five_things("See", "Spot", "Run", "Run", "Fast!!");
107         start_list_with_five_things(5,7,0,6,9);
108         start_list_with_five_things('a','f','g','s','l');
109         start_list_with_five_things(2.4, 4.3445, 3.566, 7.33234, 3.14545654645646466 );
110         myintlist = creating_int_list();
111         find_all_examples(myintlist, 4);
112
113
114         chainlist::List<mess::MESSAGE> email = get_messages();
115         email.cursor() = email.front();
116         while(email.cursor() != email.endd()){
117                 std::cout << "Messages Outside of func:\n" << *(email.cursor()->value()) << std::endl;
118                 email.cursor() = email.cursor()->next();
119         }
120         add_to_list_with_five_things<std::string>("See", "Spot", "Run", "Run", "Fast!!");
121         add_to_list_with_five_things("See", "Spot", "Run", "Run", "Fast!!");
122         add_to_list_with_five_things(5,7,0,6,9);
123         add_to_list_with_five_things('a','f','g','s','l');
124         add_to_list_with_five_things(2.4, 4.3445, 3.566, 7.33234, 3.14545654645646466 );
125         
126         add_to_list_with_five_things_then_find<std::string>("See", "Spot", "Run", "Run", "Fast!!");
127         add_to_list_with_five_things_then_find("See", "Spot", "Run", "Run", "Fast!!");
128         add_to_list_with_five_things_then_find(5,7,0,6,9);
129         add_to_list_with_five_things_then_find('a','f','g','s','l');
130         add_to_list_with_five_things_then_find(2.4, 4.3445, 3.566, 7.33234, 3.14545654645646466 );
131         return EXIT_SUCCESS;
132
133 }                               /* ----------  end of function main  ---------- */
134
135 template<typename T>
136 void start_list_with_five_things(T a, T b, T c, T d, T e){
137
138         chainlist::List<T> mylist;
139         mylist.insert_front(a);
140         mylist.insert_front(b);
141         mylist.insert_front(c);
142         mylist.insert_front(d);
143         mylist.insert_front(e);
144
145 std::cout << "The size of List is ==> " << mylist.size() << std::endl;
146         mylist.display();
147 }
148
149
150
151 template<typename T>
152 void add_to_list_with_five_things(T a, T b, T c, T d, T e){
153
154         chainlist::List<T> mylist;
155         mylist.insert(a);
156         chainlist::Node<T> * mynode = mylist.cursor();
157         mylist.insert(b, mynode);
158         mynode = mylist.cursor();
159         mylist.insert(c, mynode);
160         mynode = mylist.cursor();
161         mylist.insert(d, mynode);
162         mynode = mylist.cursor();
163         mylist.insert(e, mynode);
164         mylist.insert(a);
165
166 std::cout << "The size of List is ==> " << mylist.size() << std::endl;
167         mylist.display();
168 }
169
170 template<typename T>
171 void add_to_list_with_five_things_then_find(T a, T b, T c, T d, T e){
172
173         chainlist::List<T> mylist;
174         mylist.insert(a);
175         chainlist::Node<T> * mynode = mylist.cursor();
176         mylist.insert(b, mynode);
177         mynode = mylist.cursor();
178         mylist.insert(c, mynode);
179         mynode = mylist.cursor();
180         mylist.insert(d, mynode);
181         mynode = mylist.cursor();
182         mylist.insert(e, mynode);
183         mylist.insert(a);
184
185         mylist.find_value(c);
186         mynode = mylist.cursor();
187         std::cout << "\n\n**********Searching for Value************" << std::cout;
188         std::cout << "Value Found ==> '" << *(mylist.cursor()->value()) << "' at Node ==> " << mylist.cursor() << std::endl;
189
190         std::cout << "The size of List is ==> " << mylist.size() << std::endl;
191         mylist.display();
192         mylist.cursor(mylist.front());
193         chainlist::Node<T> * place = mylist.cursor();
194         while(place != mylist.endd()){
195                 mylist.cursor()=NULL;
196                 place = place->next();
197         }
198         mylist.display();
199 }
200
201 chainlist::Node<std::string> * findtest(std::string search_string){
202        //open short story
203
204         const char *ifs_file_name = "/home/ruben/bin/tom_walker.txt";          /* input  file name       */
205         std::ifstream   ifs;                                                    /* create ifstream object */
206
207         ifs.open (ifs_file_name);               /* open ifstream          */
208         if (!ifs) {
209                 std::cerr << "\nERROR : failed to open input  file " << ifs_file_name << std::endl;
210                 exit (EXIT_FAILURE);
211         }
212         chainlist::List<std::string> * tom_walker = new chainlist::List<std::string>;
213         chainlist::Node<std::string> * found;
214         std::string tmp;
215         while (std::getline(ifs,tmp)){
216                         tom_walker->insert(tmp);
217         
218         }
219         ifs.close ();
220         /* close ifstream         */
221
222         tom_walker->find_value(search_string);
223         found = tom_walker->cursor();
224
225         
226
227         tom_walker->display();
228
229
230
231         return found;
232 }  // End of findtest
233
234 chainlist::List<mess::MESSAGE> get_messages()
235 {
236         chainlist::List<mess::MESSAGE > mylist3;
237         mysqlpp:: Connection handle("rubenmail", 0, "ruben", "*********");
238         if( handle.connected() ){
239                 std::cout << "connected\n";
240         }else{
241                 std::cout << "no Connection: " <<   handle.error()  << std::endl;
242                 exit(1);
243         }
244
245
246
247         mysqlpp::Query ask(&handle,true, "select id, day, fromit, subject, message  from postings where fromit like \"%francal%\"");
248         mysqlpp::Query ask2(&handle,true, "select id, day, fromit, subject, message  from postings where fromit like \"%2013156062%\"");
249         mysqlpp::StoreQueryResult::const_iterator place;
250         mysqlpp::Row row;
251         mysqlpp::StoreQueryResult fran;
252         mysqlpp::StoreQueryResult fran_text;
253         if((fran = ask.store()) && (fran_text = ask2.store())){
254
255                 mess::MESSAGE * onemail = new mess::MESSAGE();
256                 mess::MESSAGE * twomail = new mess::MESSAGE();
257                 for(place = fran.begin(); place != fran.end(); ++place){
258                         row = *place;
259                         (*onemail)(row[0].c_str(), row[1].c_str(), row[2].c_str(),row[3].c_str(),row[4].c_str());
260                         mylist3.insert_front(*onemail);
261                         mylist3.insert(*onemail);
262                 }
263                 twomail = onemail;
264                 std::cout << "Message Copy \n\n" << twomail << *twomail << std::endl;
265
266
267
268                 mylist3.display();
269                 std::cout << "Number of Messages is ==> "<<  mylist3.size();
270 //              int i=0;
271 //              const int fin = mylist3.size();
272 //              for( i = 0; i < fin; ++i){
273 //                      std::cout << mylist3[1].subject() << std::endl;
274 //              }
275         }
276
277
278         return mylist3 ;
279 }               /* -----  end of template function get_messages  ----- */
280
281
282         template <class T>
283 void find_all_examples ( chainlist::List<T> &glob, const T& search_value )
284 {
285         glob.find_value(search_value);
286         chainlist::Node<T> * found = glob.cursor();
287         std::cout << "Found first value ==> '" << *(found->value()) << "' at node =='" << glob.cursor() << std::endl;
288         found = &(glob.find_next_value(search_value, *found));
289         while( found ){
290                 std::cout << "Found next value ==> '" << *(found->value()) << "' at node =='" << glob.cursor() << std::endl;
291                 found = &(glob.find_next_value(search_value, *found));
292         }
293
294
295         return ;
296 }               /* -----  end of template function find_all_examples  ----- */
297
298 chainlist::List<int>& creating_int_list(){
299         chainlist::List<int> * set = new chainlist::List<int>;
300         srand(12345);
301         int ran;
302         for(int i = 0; i < 5000; ++i){
303                 ran=rand();
304                 set->insert(ran % 101);
305         }
306         return *set;
307 }
308
309
310