1 /*
2  *  cHelpType.cc
3  *  Avida
4  *
5  *  Called "help_type.cc" prior to 12/7/05.
6  *  Copyright 1999-2011 Michigan State University. All rights reserved.
7  *  Copyright 1993-2003 California Institute of Technology.
8  *
9  *
10  *  This file is part of Avida.
11  *
12  *  Avida is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
13  *  as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
14  *
15  *  Avida is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public License along with Avida.
19  *  If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #include "cHelpType.h"
24 
25 #include "cHelpAlias.h"
26 #include "cHelpEntry.h"
27 #include "cHelpFullEntry.h"
28 #include "cHelpManager.h"
29 
30 #include <fstream>
31 
32 using namespace std;
33 
34 
AddAlias(const cString & alias_name,cHelpFullEntry * entry)35 cHelpAlias * cHelpType::AddAlias(const cString & alias_name, cHelpFullEntry * entry) {
36   cHelpAlias * new_alias = new cHelpAlias(alias_name, entry);
37   entry_list.Push(new_alias);
38   return new_alias;
39 }
40 
AddEntry(const cString & _name,const cString & _desc)41 cHelpFullEntry * cHelpType::AddEntry(const cString & _name, const cString & _desc) {
42   cHelpFullEntry * new_entry = new cHelpFullEntry(_name, this, _desc);
43   entry_list.Push(new_entry);
44   num_entries++;
45   return new_entry;
46 }
47 
FindEntry(const cString & entry_name)48 cHelpEntry * cHelpType::FindEntry(const cString & entry_name) {
49   tListIterator<cHelpEntry> entry_it(entry_list);
50   while (entry_it.Next() != NULL) {
51     if (entry_it.Get()->GetName() == entry_name) {
52       return entry_it.Get(); // Found!
53     }
54   }
55   return NULL;  // Not found...
56 }
57 
~cHelpType()58 cHelpType::~cHelpType() { while (entry_list.GetSize() > 0) delete entry_list.Pop(); }
59 
PrintHTML()60 void cHelpType::PrintHTML()
61 {
62   if (manager->GetVerbose()) cout << "  Category: " << GetName() << endl;
63 
64   cHelpEntry * cur_entry = NULL;
65   tListIterator<cHelpEntry> entry_it(entry_list);
66 
67   while ( (cur_entry = entry_it.Next()) != NULL) {
68     // Only print non-alias entries...
69     if (cur_entry->IsAlias() == true) continue;
70 
71     ofstream fp(cur_entry->GetHTMLFilename());
72 
73     fp << "<html>" << endl
74        << "<title>" << GetName() << " : "
75        << cur_entry->GetName() << "</title>" << endl << endl
76        << "<h1>" << cur_entry->GetName() << "</h1>" << endl << endl;
77 
78     cString out_desc( cur_entry->GetDesc() );
79     int pos = 0;
80     while ((pos = out_desc.Find('!', pos)) != -1) {
81       // Grab the word we need to replace
82       cString found_word(out_desc.GetWordAt(pos));
83       cString new_word;
84 
85       // If we have a double '!' reduce it to a single one and continue...
86       if (found_word[1] == '!') {
87 	out_desc.Replace("!!", "!", pos);
88 	pos++;
89       }
90 
91       // Otherwise, do a proper replacement...
92       else {
93 	// Find the root keyword...
94 	cString keyword(found_word);
95 	keyword.ClipFront(1); // Clip off the '!' on the string.
96 
97 	// Clip end punctuation and save it if there is any.
98 	char end_char = ' ';
99 	const int last_pos = keyword.GetSize() - 1;
100 	if (keyword.IsNumeric(last_pos) == false &&
101 	    keyword.IsLetter(last_pos) == false) {
102 	  end_char = keyword[keyword.GetSize() - 1];
103 	  keyword.ClipEnd(1);
104 	}
105 	// User can end a word in a '!' to preserve other punctuation...
106 	if (end_char == '!') end_char = ' ';
107 
108 	// Determine what filename contains the new word...
109 	cHelpEntry * found_entry = manager->FindEntry(keyword);
110 	if (found_entry == NULL) {
111 	  if (manager->GetVerbose() == true) {
112 	    cerr << "  Warning: unknown help keyword \""
113 		 << keyword << "\"." << endl;
114 	  }
115 	  new_word.Set("<a href=\"help.%s.html\">%s</a>%c", static_cast<const char*>(keyword), static_cast<const char*>(keyword), end_char);
116 	}
117 	else {
118 	  new_word.Set("<a href=\"%s\">%s</a>%c", static_cast<const char*>(found_entry->GetHTMLFilename()), static_cast<const char*>(keyword), end_char);
119 	}
120 
121 	// Rebuild the description with the new word...
122 	out_desc.Replace(found_word, new_word, pos);
123 	pos += new_word.GetSize();
124       }
125     }
126 
127     fp << out_desc << endl;
128   }
129 }
130 
131