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.
This commit is contained in:
Sylvain Pion 2003-10-30 15:53:50 +00:00
parent d26283384b
commit 2da6d201e5
1 changed files with 62 additions and 75 deletions

View File

@ -12,54 +12,26 @@
**************************************************************************/
#include <stdio.h>
#include <strings.h>
#include <iostream>
#include <fstream>
#include <string>
#include <functional>
#include <algorithm>
#include <list>
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<Lines, bool> {
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<Lines> list_of_lines;
std::list<Lines>::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,"</TD></TR>")!=0 &&
strcmp(name_link,"!!!")!=0) {
strcat(tmp," ");
strcat(tmp,name_link);
if (name_link.compare("???")!=0 &&
name_link.compare("</TD></TR>")!=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 << "<H2><A NAME=\"Index" << name_link
<< "\">";
strcat(name_link,"</A></H2>");
name_link+="</A></H2>";
}
else out_file << tmp <<" ";
}
end_name=1;
}
}
if (page) { //if link
in_file2 >> number;
out_link = search(list,number);
if (out_link) {
out_file <<"<A "<< out_link <<">" <<tmp << "</A> ";
} else out_file << tmp;
while (in_file2.get(ch) && ch!='<') {
if (ch=='|') {
in_file2 >> number;
if (out_link) {
out_file << "<A "<< search(list,number)<<">"
<< "<img SRC=\"" << "./index_arrow.gif\" ALT=\"reference\" WIDTH=\"14\" HEIGHT=\"12\" VALIGN=BOTTOM BORDER=0></A> ";
}
}
}
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 <<"<A "<< lli->text <<">" <<tmp << "</A> ";
}
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 << "<A "<< lli->text <<">"
<< "<img SRC=\"" << "./index_arrow.gif\" ALT=\"reference\" WIDTH=\"14\" HEIGHT=\"12\" VALIGN=BOTTOM BORDER=0></A> ";
}
}
}
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();