1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 #include <syshelp.hxx>
26 
27 
28 // NOT FULLY DEFINED SERVICES
29 #include <string.h>
30 #include "sistr.hxx"
31 #include "list.hxx"
32 
33 #ifdef WNT
34 #include <io.h>
35 #elif defined(UNX) || defined(OS2)
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <dirent.h>
39 #define stricmp strcasecmp
40 #else
41 #error Must run under unix or windows, please define UNX or WNT.
42 #endif
43 
44 
45 char C_sSpaceInName[] = "&nbsp;&nbsp;&nbsp;";
46 
47 void
WriteName(std::ostream & o_rFile,const Simstr & i_rIdlDocuBaseDir,const Simstr & i_rName,E_LinkType i_eLinkType)48 WriteName( std::ostream & 		o_rFile,
49 		   const Simstr &	i_rIdlDocuBaseDir,
50 		   const Simstr &   i_rName,
51 		   E_LinkType		i_eLinkType )
52 {
53 	if (i_rName.l() == 0)
54 		return;
55 
56 
57 	const char * pNameEnd = strstr( i_rName.str(), " in " );
58 
59 	// No link:
60 	if ( i_eLinkType == lt_nolink )
61 	{
62 		if ( pNameEnd != 0 )
63 		{
64 			const char * pStart = i_rName.str();
65 			o_rFile.write( pStart, pNameEnd - pStart );
66 			WriteStr( o_rFile, C_sSpaceInName );
67 			WriteStr( o_rFile, pNameEnd );
68 		}
69 		else
70 		{
71 			WriteStr( o_rFile, i_rName );
72 		}
73 		return;
74 	}
75 
76 	if ( i_eLinkType == lt_idl )
77 	{
78 		Simstr sPath(i_rName);
79 		sPath.replace_all('.','/');
80 		int nNameEnd = sPath.pos_first(' ');
81 		int nPathStart = sPath.pos_last(' ');
82 		WriteStr( o_rFile, "<A HREF=\"" );
83 
84 		if ( nNameEnd > -1 )
85 		{
86 			WriteStr( o_rFile, "file:///" );
87 			WriteStr( o_rFile, i_rIdlDocuBaseDir );
88 			WriteStr( o_rFile, "/" );
89 			WriteStr( o_rFile, sPath.str() + 1 + nPathStart );
90 			WriteStr( o_rFile, "/" );
91 			o_rFile.write( sPath.str(), nNameEnd );
92 			WriteStr( o_rFile, ".html\">" );
93 		}
94 		else
95 		{  	// Should not be reached:
96 			WriteStr(o_rFile, i_rName);
97 			return;
98 		}
99 	}
100 	else if ( i_eLinkType == lt_html )
101 	{
102 		int nKomma = i_rName.pos_first(',');
103 		int nEnd = i_rName.pos_first(' ');
104 		if ( nKomma > -1 )
105 		{
106 			o_rFile.write( i_rName.str(), nKomma );
107 			WriteStr( o_rFile, ": " );
108 
109 			WriteStr( o_rFile, "<A HREF=\"" );
110 
111 			o_rFile.write( i_rName.str(), nKomma );
112 			WriteStr( o_rFile, ".html#" );
113 			if ( nEnd > -1 )
114 				o_rFile.write( i_rName.str() + nKomma + 1, nEnd - nKomma );
115 			else
116 				WriteStr( o_rFile, i_rName.str() + nKomma + 1 );
117 			WriteStr( o_rFile, "\">" );
118 
119 			o_rFile.write( i_rName.str() + nKomma + 1, nEnd - nKomma );
120 		}
121 		else
122 		{
123 			WriteStr( o_rFile, "<A HREF=\"" );
124 			WriteStr( o_rFile, i_rName );
125 			WriteStr( o_rFile, ".html\">" );
126 
127 			WriteStr( o_rFile, i_rName );
128 		}
129 		WriteStr( o_rFile, "</A>" );
130         return;
131 	}
132 
133 	if ( pNameEnd != 0 )
134 	{
135 		const char * pStart = i_rName.str();
136 		if ( pNameEnd > pStart )
137 			o_rFile.write( pStart, pNameEnd - pStart );
138 		WriteStr( o_rFile, "</A>" );
139 
140 		WriteStr( o_rFile, C_sSpaceInName );
141 		WriteStr( o_rFile, pNameEnd );
142 	}
143 	else
144 	{
145 		WriteStr( o_rFile, i_rName );
146 		WriteStr( o_rFile, "</A>" );
147 	}
148 }
149 
150 
151 void
WriteStr(std::ostream & o_rFile,const char * i_sStr)152 WriteStr( std::ostream & 	o_rFile,
153 		  const char *		i_sStr )
154 {
155 	o_rFile.write( i_sStr, (int) strlen(i_sStr) );
156 }
157 
158 void
WriteStr(std::ostream & o_rFile,const Simstr & i_sStr)159 WriteStr( std::ostream & 	  o_rFile,
160 		  const Simstr &      i_sStr )
161 {
162 	o_rFile.write( i_sStr.str(), i_sStr.l() );
163 }
164 
165 
166 const char C_sXML_END[] = "\\*.xml";
167 
168 void
GatherFileNames(List<Simstr> & o_sFiles,const char * i_sSrcDirectory)169 GatherFileNames( List<Simstr> &   	o_sFiles,
170 				 const char *		i_sSrcDirectory )
171 {
172 	static int 	 nAliveCounter = 0;
173 
174 	char * 		 sNextDir = 0;
175 	Simstr       sNew = 0;
176 
177 #ifdef WNT
178 	struct _finddata_t aEntry;
179 	long hFile = 0;
180 	int bFindMore = 0;
181 	char * sFilter = new char[ strlen(i_sSrcDirectory) + sizeof C_sXML_END ];
182 
183 	// Stayingalive sign
184 	if (++nAliveCounter % 100 == 1)
185 		std::cout << "." << std::flush;
186 
187 	strcpy(sFilter, i_sSrcDirectory);       // STRCPY SAFE HERE
188 	strcat(sFilter,C_sXML_END);             // STRCAT SAFE HERE
189 
190 	hFile = _findfirst( sFilter, &aEntry );
191 	for ( bFindMore = hFile == -1;
192 		  bFindMore == 0;
193 		  bFindMore = _findnext( hFile, &aEntry ) )
194 	{
195 		sNew = i_sSrcDirectory;
196 		sNew += "\\";
197 		sNew += aEntry.name;
198 		o_sFiles.push_back(sNew);
199 	}	// end for
200 
201 	_findclose(hFile);
202 	delete [] sFilter;
203 #elif defined(UNX) || defined(OS2)
204 	DIR * pDir = opendir( i_sSrcDirectory );
205 	dirent * pEntry = 0;
206 	char * sEnding;
207 
208 	// Stayingalive sign
209 	if (++nAliveCounter % 100 == 1)
210 		std::cout << "." << std::flush;
211 
212 	while ( (pEntry = readdir(pDir)) != 0 )
213 	{
214 		sEnding = strrchr(pEntry->d_name,'.');
215 		if (sEnding != 0 ? stricmp(sEnding,".xml") == 0 : 0 )
216 		{
217 			sNew = i_sSrcDirectory;
218 			sNew += "/";
219 			sNew += pEntry->d_name;
220 			o_sFiles.push_back(sNew);
221 		}
222 	}	// end while
223 
224 	closedir( pDir );
225 #else
226 #error Must run on unix or windows, please define UNX or WNT.
227 #endif
228 
229 	//  gathering from subdirectories:
230 	List<Simstr> aSubDirectories;
231 	GatherSubDirectories( aSubDirectories, i_sSrcDirectory );
232 
233 	unsigned d_max = aSubDirectories.size();
234 	for ( unsigned d = 0; d < d_max; ++d )
235 	{
236 		sNextDir = new char[ strlen(i_sSrcDirectory) + 2 + aSubDirectories[d].l() ];
237 
238 		strcpy(sNextDir, i_sSrcDirectory);
239 		strcat(sNextDir, C_sSLASH);
240 		strcat(sNextDir, aSubDirectories[d].str());
241 		GatherFileNames(o_sFiles, sNextDir);
242 
243 		delete [] sNextDir;
244 	}
245 }
246 
247 
248 const char * C_sANYDIR = "\\*.*";
249 
250 void
GatherSubDirectories(List<Simstr> & o_sSubDirectories,const char * i_sParentdDirectory)251 GatherSubDirectories( List<Simstr> &   	o_sSubDirectories,
252 					  const char *	   	i_sParentdDirectory )
253 {
254 	Simstr sNew;
255 
256 #ifdef WNT
257 	struct _finddata_t aEntry;
258 	long hFile = 0;
259 	int bFindMore = 0;
260 	char * sFilter = new char[strlen(i_sParentdDirectory) + sizeof C_sANYDIR];
261 
262 	strcpy(sFilter, i_sParentdDirectory);
263 	strcat(sFilter,C_sANYDIR);
264 
265 	hFile = _findfirst( sFilter, &aEntry );
266 	for ( bFindMore = hFile == -1;
267 		  bFindMore == 0;
268 		  bFindMore = _findnext( hFile, &aEntry ) )
269 	{
270 		if (aEntry.attrib == _A_SUBDIR)
271 		{
272 			// Do not gather . .. and outputtree directories
273 			if ( strchr(aEntry.name,'.') == 0
274 				 && strncmp(aEntry.name, "wnt", 3) != 0
275 				 && strncmp(aEntry.name, "unx", 3) != 0 )
276 			{
277 				sNew = aEntry.name;
278 				o_sSubDirectories.push_back(sNew);
279 			}
280 		}   // endif (aEntry.attrib == _A_SUBDIR)
281 	}   // end for
282 	_findclose(hFile);
283 	delete [] sFilter;
284 
285 #elif defined(UNX) || defined(OS2)
286 	DIR * pDir = opendir( i_sParentdDirectory );
287 	dirent * pEntry = 0;
288 	struct stat 	aEntryStatus;
289 
290 	while ( ( pEntry = readdir(pDir) ) != 0 )
291 	{
292 		stat(pEntry->d_name, &aEntryStatus);
293 		if ( ( aEntryStatus.st_mode & S_IFDIR ) == S_IFDIR )
294 		{
295 			// Do not gather . .. and outputtree directories
296 			if ( strchr(pEntry->d_name,'.') == 0
297 				 && strncmp(pEntry->d_name, "wnt", 3) != 0
298 				 && strncmp(pEntry->d_name, "unx", 3) != 0 )
299 			{
300 				sNew = pEntry->d_name;
301 				o_sSubDirectories.push_back(sNew);
302 			}
303 		}   // endif (aEntry.attrib == _A_SUBDIR)
304 	}	// end while
305 	closedir( pDir );
306 #else
307 #error Must run on unix or windows, please define UNX or WNT.
308 #endif
309 }
310 
311