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