0023024: Update headers of OCCT files
[occt.git] / src / Standard / Standard_osd_ffi.c
1 /*
2  Copyright (c) 1998-1999 Matra Datavision
3  Copyright (c) 1999-2012 OPEN CASCADE SAS
4
5  The content of this file is subject to the Open CASCADE Technology Public
6  License Version 6.5 (the "License"). You may not use the content of this file
7  except in compliance with the License. Please obtain a copy of the License
8  at http://www.opencascade.org and read it completely before using this file.
9
10  The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11  main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12
13  The Original Code and all software distributed under the License is
14  distributed on an "AS IS" basis, without warranty of any kind, and the
15  Initial Developer hereby disclaims all such warranties, including without
16  limitation, any warranties of merchantability, fitness for a particular
17  purpose or non-infringement. Please see the License for the specific terms
18  and conditions governing the rights and limitations under the License.
19
20 */
21
22 /**********************************************************
23 */ 
24 /**    RBA                  12/10/88
25 */ 
26 /**    ---                  --------
27 */ 
28 /**
29 */ 
30 /**  Fichier  : OSD_FFI.C    (SUN/UNIX)
31 */ 
32 /**  Titre    : osd_ffi_(dir, name, tab_res, max, l_dir, l_name, l_tab)
33 */ 
34 /**             char *dir, *nmae, *tab-res;
35 */ 
36 /**             int *max;
37 */ 
38 /**             int l_dir, l_name, l_tab;
39 */ 
40 /**  Retour:
41 */ 
42 /**            Nombre de fichier trouve (1 ou plus)
43 */ 
44 /**            0    sinon.
45 */ 
46 /**
47 */ 
48 /**  Role     : Trouver tous les fichier qui ont le meme nom que 'name' sachant
49 */ 
50 /**             que 'name' peut etre un nom de fichier comportant le caractere
51 */ 
52 /**             joker '*'.
53 */ 
54 /**  Exemple  :
55 */ 
56 /**             Appel en Fortran
57 */ 
58 /**
59 */ 
60 /**             integer nb_fichier, osd_ffi
61 */ 
62 /**             character*80 dir, name
63 */ 
64 /**             character*80 tab_res(100)
65 */ 
66 /**             ....
67 */ 
68 /**             ....
69 */ 
70 /**             name = 'TOTO*.*'
71 */ 
72 /**             ....
73 */ 
74 /**             nb_fichier = osd_ffi(dir, name, tab_res, 100)
75 */ 
76 /**
77 */ 
78 /**  Modifs : - sga 28/04/92 correction bug closedir
79 */ 
80 /**           - asi 11/04/90 correction de strcmp_joker
81 */ 
82 /**           - fsa 26/04/90 ajout du tri du tableau tab_res
83 */ 
84 /**             par ordre aphabethique
85 */ 
86 /**           - asi 06/09/90 ajout de la directorie aux noms de fichier
87 */ 
88 /**             retourne
89 */ 
90 /**           - J.P. TIRAULT le 3-Avril-1992 : Faire le close du directory
91 */ 
92 /**             apres l'avoir ouvert.
93 */ 
94 /**
95 */ 
96 /*************************************************************/ 
97 #if defined(_MSC_VER) && !defined(WNT)
98 #error "Wrong compiler options has been detected. Add /DWNT option for proper compilation!!!!!"
99 #endif
100
101 #ifndef WNT
102
103 #ifdef HAVE_CONFIG_H
104 # include <config.h>
105 #endif
106
107 /* This hack is required to get HP's c89 to include a definition for DIR */
108 #ifdef __hpux
109 # ifndef _INCLUDE_POSIX_SOURCE
110 #  define _INCLUDE_POSIX_SOURCE
111 # endif
112 #endif
113
114 #include <stdio.h>
115 #include <sys/types.h>
116 #include <sys/stat.h>
117 #include <stdlib.h>
118 #include <string.h>
119
120 #ifdef  HAVE_DIRENT_H
121 # include <dirent.h>
122 # define NAMLEN(dirent) strlen((dirent)->d_name)
123 # ifdef VMS
124 extern char *vmsify PARAMS ((char *name, int type));
125 # endif
126 #else
127 # define dirent direct
128 # define NAMLEN(dirent) (dirent)->d_namlen
129 # ifdef HAVE_SYS_NDIR_H
130 #  include <sys/ndir.h>
131 # endif
132 # ifdef HAVE_SYS_DIR_H
133 #  include <sys/dir.h>
134 # endif
135 # ifdef HAVE_NDIR_H
136 #  include <ndir.h>
137 # endif
138 # ifdef HAVE_VMSDIR_H
139 #  include "vmsdir.h"
140 # endif /* HAVE_VMSDIR_H */
141 #endif
142
143 /* In GNU systems, <dirent.h> defines this macro for us.  */
144 #ifdef _D_NAMLEN
145 # undef NAMLEN
146 # define NAMLEN(d) _D_NAMLEN(d)
147 #endif
148
149 #if (defined (POSIX) || defined (VMS) || defined (WINDOWS32)) && !defined (__GNU_LIBRARY__)
150 /* Posix does not require that the d_ino field be present, and some
151    systems do not provide it. */
152 # define REAL_DIR_ENTRY(dp) 1
153 # define FAKE_DIR_ENTRY(dp)
154 #else
155 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
156 # define FAKE_DIR_ENTRY(dp) (dp->d_ino = 1)
157 #endif /* POSIX */
158
159 DIR *dirp;
160 struct dirent *dp;
161
162 void tri(int taille, char *list,int l_tab);
163 int strcmp_joker(char *s1, char *s2);
164
165 int
166 osd_ffi(char *dir, char *name, char *tab_res, int *max, int l_dir, int l_name, int l_tab)
167 {
168      char file[255], directory[255];
169      int  ii, jj, kk, nb_file;
170
171      /***** Le Nom de la directory ******/
172       if (*dir == ' ') {
173           directory[0] = '.';
174           directory[1] = 0;
175       }
176       else {
177           for (ii=0; ii<l_dir && ii<255 && *dir != ' '; ii++)
178              directory[ii] = *(dir++);
179           directory[ii]=0;
180       }
181
182       /***** Le Nom de fichier recherche *******/
183       for (ii=0; ii<l_name  && ii<255 && *name != ' '; ii++)
184              file[ii] = *(name++);
185       file[ii]=0;
186
187       /****** On ouvre le directory pour trouver les fichiers ********/
188       nb_file=0;
189       if ((dirp = opendir(directory)) != NULL){
190           while ((nb_file < *max) && (dp=readdir(dirp))) {
191               /*====== Si on trouve un fichier qui lui ressemble ==========*/
192               if(strcmp_joker(file,dp->d_name) == 0) {
193                  /*======= On copie sa valeur ========*/
194                  /*        avec la directorie         */
195                  /* jj tab_res, kk la ligne ,ii le nom */
196                  jj = nb_file * l_tab;
197                  kk = 0;
198                  if (directory[0] != '.') {
199                     for (; kk<l_tab && directory[kk] != 0; kk++,jj++)
200                         *(tab_res + jj) = directory[kk];
201                     if (directory[kk-1] != '/')  *(tab_res + (jj++)) = '/';
202                  }
203                  for (ii=0; (kk)<l_tab && *(dp->d_name+ii); ii++,jj++,kk++)
204                      *(tab_res + jj) = *(dp->d_name + ii);
205                  /*======= En completant avec blanc ======*/
206                  for (; kk<l_tab ; jj++,kk++)
207                      *(tab_res + jj) = ' ';
208                  nb_file++;
209               }
210           }
211       }
212 /***** tri du tableau tab_res par ordre alpha *****/
213      if (dirp){
214        closedir(dirp);
215        tri( nb_file , tab_res ,l_tab );
216       }
217       return(nb_file);
218 }
219
220 /***********************************************
221 *  RBA                        Date: 27/06/88
222 *  ---                             ---------
223 *
224 * Title: STRCMP_JOKER(ch1,ch2)
225 * Role : Compare ch1 with ch2
226 *        ch1 can have the joker caracter '*'
227 *        res = 0 they are identical
228 *        res <>0 otherwise
229 * Modif: asi 11/04/90 correction
230 ***********************************************/
231
232
233 #define TRUE  1
234 #define FALSE 0
235 #define JOKER '*'
236
237 int
238 strcmp_joker(char *s1, char *s2)
239 {
240     char *c1=NULL,*c2=NULL;  /* sauvegardes */
241     int res;
242     int deb = 1;  /* 0 si 1er car est le joker, 1 sinon */
243
244     while (*s2) {
245         if (*s1 == JOKER) {
246             deb = 0;
247             while(*s1 == JOKER) s1++;
248             if (*s1) {
249                 while(*s2 && *s2 != *s1) s2++;
250                 c1 = s1, c2 = s2;
251             }
252             else return(0); /* s1 fini par un jocker */
253         }
254         else { /* comparaison de 2 car */
255             res = *s1++ - *s2++;
256             if (res != 0) {
257                 /* 1ere chaine pas de joker au debut */
258                 if (deb) return (res);
259                 else {  /* on recherche la meme chaine plus loin */
260                     s1 = c1, s2 = ++c2;
261                     while(*s2 && *s2 != *s1) s2++;
262                     c2 = s2;
263                 }
264             }
265         }
266     }
267     while(*s1 == JOKER) s1++;
268     return(*s1 - *s2);
269 }
270
271 /**************************************************
272 * FSA                               date: 26/04/90
273 * ---                                    ---------
274 *
275 * Titre : COMPAR (ch1,ch2)
276 * Role  : Comparer ch1 avec ch2
277 *         retourne :  0 si chaines identiques
278 *                    >0 si ch2 avant ch1
279 *                    <0 si ch1 avant ch2
280 * Modif :
281 **************************************************/
282
283 static int
284 compar(char *ch1, char *ch2)
285 {
286   /* int strcmp(char *cha, const char *chb); */
287
288  return(strcmp(ch1,ch2));
289 }
290
291 /**************************************************
292 * FSA                               date: 26/04/90
293 * ---                                    ---------
294 *
295 * Titre : TRI (taille,list,l_tab)
296 * Role  : Tri un tableau de chaines par ordre
297 *         alphabetique
298 * Modif :
299 **************************************************/
300
301 void
302 tri(int taille, char *list,int l_tab)
303 {
304  qsort( list , taille , l_tab ,(int (*)(const void *,const void *)) compar );
305 }
306
307 #else
308 // VERSION WNT
309 //
310 #include <io.h>
311 #include <stdlib.h>
312 #include <string.h>
313 #include <stddef.h>
314
315 #ifdef VAC
316 #include <windows.h> // added for VAC++ version  -ChG 07081997
317 #endif
318
319 #ifdef DOS
320 #include <dos.h>
321 static struct _find_t zeptr ;
322
323 #else
324
325 #ifdef VAC
326 static LPWIN32_FIND_DATA zeptr ;
327 #else
328 static struct _finddata_t zeptr ;
329 #endif
330
331 #endif
332 #ifdef VAC
333 HANDLE dirp;
334 #else
335 ptrdiff_t dirp;
336 #endif
337
338 char filespec[255];
339
340
341 int
342 osd_ffi_(char *dir, char *name, char *tab_res, int *max, int l_dir, int l_name, int l_tab)
343 {
344      char file[255], directory[255];
345      int  ii, jj, kk, nb_file;
346 #ifdef VAC
347      BOOL  fini ;
348 #else
349      int  fini ;
350 #endif
351
352      /***** Le Nom de la directory ******/
353       if (*dir == ' ') {
354           directory[0] = '.';
355           directory[1] = '/';
356           directory[2] = 0 ;
357       }
358       else {
359           for (ii=0; ii<l_dir && ii<255 && *dir != ' '; ii++) {
360              directory[ii] = *(dir++);
361              if ( directory [ii] == '\\' ) directory [ii] = '/' ;
362           }
363           directory[ii]=0;
364       }
365
366       /***** Le Nom de fichier recherche *******/
367       for (ii=0; ii<l_name  && ii<255 && *name != ' '; ii++)
368              file[ii] = *(name++);
369       file[ii]=0;
370
371       /****** On ouvre le directory pour trouver les fichiers ********/
372       nb_file=0;
373
374       strcpy(filespec, directory );
375       strcat(filespec, file );
376
377 #ifdef VAC
378       if ((dirp = FindFirstFile(filespec, zeptr)) != INVALID_HANDLE_VALUE ) {
379 #else
380       if ((dirp = _findfirst(filespec, &zeptr)) != -1 ) {
381 #endif
382           fini = 0 ;
383           while (nb_file < *max && (! fini ) ) {
384
385               /*====== Si on trouve un fichier qui lui ressemble ==========*/
386
387
388                  /*======= On copie sa valeur ========*/
389                  /*        avec la directorie         */
390                  /* jj tab_res, kk la ligne ,ii le nom */
391                  jj = nb_file * l_tab;
392                  kk = 0;
393                  if (directory[0] != '.') {
394                     for (; kk<l_tab && directory[kk] != 0; kk++,jj++)
395                         *(tab_res + jj) = directory[kk];
396
397                     if (directory[kk-1] != '/')  *(tab_res + (jj++)) = '/';
398                  }
399
400 #ifdef VAC
401                  for (ii=0; (kk)<l_tab && *((*zeptr).cFileName+ii); ii++,jj++,kk++)
402                      *(tab_res + jj) = *((*zeptr).cFileName + ii);
403 #else
404                  for (ii=0; (kk)<l_tab && *(zeptr.name+ii); ii++,jj++,kk++)
405                      *(tab_res + jj) = *(zeptr.name + ii);
406 #endif
407
408                  /*======= En completant avec blanc ======*/
409
410                  for (; kk<l_tab ; jj++,kk++)
411                      *(tab_res + jj) = ' ';
412                  nb_file++;
413
414 #ifdef VAC
415               fini = FindNextFile(dirp, zeptr );
416 #else
417               fini = _findnext(dirp, &zeptr );
418 #endif
419           }
420       }
421 /***** pas de tri du tableau tab_res par ordre alpha *****/
422
423 #ifdef VAC
424           if (dirp != INVALID_HANDLE_VALUE) FindClose(dirp);
425 #else
426           if (dirp) _findclose(dirp);
427 #endif
428
429       return(nb_file);
430 }
431 #endif
432