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