From 2da6d201e51144fc255429ab62f9ab2e0ced3e4d Mon Sep 17 00:00:00 2001 From: Sylvain Pion Date: Thu, 30 Oct 2003 15:53:50 +0000 Subject: [PATCH] took out self constructed lists and self allocated arrays (which have not been clean up properly after usage) and replaced them with STL lists, iterators and std::strings. --- Packages/Manual_tools/src/cc_index_link.C | 137 ++++++++++------------ 1 file changed, 62 insertions(+), 75 deletions(-) diff --git a/Packages/Manual_tools/src/cc_index_link.C b/Packages/Manual_tools/src/cc_index_link.C index 0ed5e3776a0..f5f99bc8a6c 100644 --- a/Packages/Manual_tools/src/cc_index_link.C +++ b/Packages/Manual_tools/src/cc_index_link.C @@ -12,54 +12,26 @@ **************************************************************************/ - -#include -#include #include #include +#include +#include +#include +#include - -typedef struct Lines{ +struct Lines{ int number; - char *text; - struct Lines *next; -} *pList; - - -pList list=NULL; - - -pList AddToList(pList list , const int n, const char *l) { - if (list==NULL) { - list = new Lines; - list->number = n; - list->text= new char[strlen(l)+1]; - strcpy(list->text,l); - list->next = NULL; - } else { - pList p = new Lines; - p->number = n; - p->text= new char[strlen(l)+1]; - strcpy(p->text,l); - p->next = list; - list = p; - } - return list; -} - - - -char* search(pList list, int number) { - if (list==NULL) return '\0'; - while ((list!=NULL) ) { - if (list->number==number) { - return list->text; - } else list=list->next; - } - return '\0'; -} + std::string text; + Lines(int n, const std::string& s) : number(n), text(s) {} +}; +class Lines_eq : public unary_function { + int ref_number; +public: + explicit Lines_eq(int number) : ref_number(number) {} + bool operator() (const Lines& l) const {return l.number == ref_number;} +}; /* >main: main function with standard unix parameter input */ /* ------------------------------------------------------- */ @@ -72,6 +44,9 @@ main( int argc, char **argv) { std::cerr << "*** Error: program needs an additional parameter"<<'\n'; exit(1); } + + std::cerr << "cc_index_link:" << std::endl; + char* file_name1 = argv[1]; char* file_name2 = argv[2]; char* out_file_name = argv[3]; @@ -79,27 +54,33 @@ main( int argc, char **argv) { std::ofstream out_file(out_file_name); if (!out_file) std::cerr<<"*** Error: cannot open file "<< out_file_name <<'\n'; + else + std::cerr << " output file opened" << std::endl; std::ifstream in_file1(file_name1); if (!in_file1) std::cerr<<"*** Error: cannot open file "<< file_name1 <<'\n'; + else + std::cerr << " first input file opened" << std::endl; + + std::list list_of_lines; + std::list::const_iterator lli; int number; - char link[400]; + std::string link; while (in_file1) { in_file1 >> number >> link; - list=AddToList(list, number, link); + list_of_lines.push_front(Lines(number, link)); } - std::ifstream in_file2(file_name2); if (!in_file2) std::cerr<<"*** Error: cannot open file "<< file_name2 <<'\n'; - + else + std::cerr << " second input file opened" << std::endl; char ch; - char* out_link; - char name_link[300]; + std::string name_link; bool end_name; while (in_file2.get(ch)) { @@ -109,57 +90,63 @@ main( int argc, char **argv) { if (in_file2.get(ch)) { if (ch=='?') { // if index entry end_name=0; - char tmp[300]=""; + std::string tmp; bool page = 0; while (in_file2 && !end_name) { in_file2>> name_link; - if (strcmp(name_link,"???")!=0 && - strcmp(name_link,"")!=0 && - strcmp(name_link,"!!!")!=0) { - strcat(tmp," "); - strcat(tmp,name_link); + + if (name_link.compare("???")!=0 && + name_link.compare("")!=0 && + name_link.compare("!!!")!=0) { + tmp+=" "; + tmp+=name_link; } else { - if (strcmp(name_link,"???")==0) page = 1; + if (name_link.compare("???")==0) page = 1; else { - if (strcmp(name_link,"!!!")==0) { + if (name_link.compare("!!!")==0) { in_file2 >> name_link; out_file << "

"; - strcat(name_link,"

"); + name_link+=""; } else out_file << tmp <<" "; } end_name=1; } } - if (page) { //if link in_file2 >> number; - out_link = search(list,number); - if (out_link) { - out_file <<"" < "; - } else out_file << tmp; - while (in_file2.get(ch) && ch!='<') { - if (ch=='|') { - in_file2 >> number; - if (out_link) { - out_file << "" - << "\"reference\" "; - } - } - } - out_file << ch; + lli = find_if(list_of_lines.begin(), list_of_lines.end(), Lines_eq(number)); + bool number_found = (lli != list_of_lines.end()); + if (number_found) { + out_file <<"text <<">" < "; + } + else + out_file << tmp; + + while (in_file2.get(ch) && ch!='<') { + if (ch=='|') { + in_file2 >> number; + if (number_found) { + lli = find_if(list_of_lines.begin(), list_of_lines.end(), Lines_eq(number)); + out_file << "text <<">" + << "\"reference\" "; + } + } + } + + out_file << ch; } else out_file << name_link; - } else out_file << ch; + } else out_file << ch; \end{description} + + } } else out_file << ch; } } else out_file << ch; } - - in_file1.close(); in_file2.close(); out_file.close();