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