1 /* gEDA - GPL Electronic Design Automation
2 * libgeda - gEDA's library
3 * Copyright (C) 1998-2010 Ales Hvezda
4 * Copyright (C) 1998-2010 gEDA Contributors (see ChangeLog for details)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 /*! slib stands for source (project/schematic/hdl/model source) library */
25 #include <sys/types.h>
35 #include "libgeda_priv.h"
37 #ifdef HAVE_LIBDMALLOC
41 /* need to test everything at boundary conditions (exceed cache size etc...) */
49 static int slib_index
=0;
55 * and eventually make this unlimited
58 static struct st_slib slib
[MAX_SLIBS
];
60 /*! \todo Finish function documentation!!!
62 * \par Function Description
65 int s_slib_add_entry(char *new_path
)
67 if (new_path
== NULL
) {
71 if (slib_index
>= MAX_SLIBS
) {
75 slib
[slib_index
].dir_name
= g_strdup (new_path
);
81 /*! \todo Finish function documentation!!!
83 * \par Function Description
85 * \return 1 if directory is found, zero otherwise.
87 int s_slib_search_for_dirname(char *dir_name
)
91 for (i
= 0; i
< slib_index
; i
++) {
92 if (strcmp(slib
[i
].dir_name
, dir_name
) == 0) {
100 /*! \todo Finish function documentation!!!
102 * \par Function Description
105 * Caller must g_free returned pointer.
107 char *s_slib_search_dirs(const char *basename
)
112 char *slib_path
=NULL
;
114 /* search slib paths backwards */
115 for (i
= slib_index
-1 ; i
>= 0; i
--) {
116 /* for (i = 0 ; i < slib_index; i++) {*/
119 printf("searching: %d %s\n", i
, slib
[i
].dir_name
);
122 ptr
= opendir(slib
[i
].dir_name
);
124 g_return_val_if_fail ((ptr
!= NULL
), NULL
);
128 while(dptr
!= NULL
) {
130 /* Search for an exact match */
131 if (strcmp(dptr
->d_name
, basename
) == NULL
) {
132 slib_path
= g_strdup (slib
[i
].dir_name
);
154 /*! \todo Finish function documentation!!!
156 * \par Function Description
159 * Caller must g_free returned pointer.
161 char *s_slib_search_lowlevel(const char *basename
)
163 char *slib_path
=NULL
;
164 char *full_path
=NULL
;
166 slib_path
= s_slib_search_dirs(basename
);
169 full_path
= g_build_filename (slib_path
, basename
, NULL
);
179 /*! \todo Finish function documentation!!!
180 * \brief Get the base file name from a raw file name string.
181 * \par Function Description
182 * This function takes a raw file name and returns a processed file name.
183 * It takes the raw file name and copies everything up to the first period
184 * and removes any _# (where # is any number of digits.
186 * \param [in] rawname Character string with the raw file name to parse.
187 * \return The base file name in a character string.
190 * Caller must g_free returned pointer.
192 char *s_slib_getbasename(const char *rawname
)
194 char *return_filename
;
200 int seen_underscore
=0;
205 len
= strlen(rawname
)+1;
207 return_filename
= (char *) g_malloc(sizeof(char)*len
);
210 /* first get everything up to the leading dot */
211 while(rawname
[i
] != '\0' && rawname
[i
] != '.') {
212 return_filename
[i
] = rawname
[i
];
217 return_filename
[i
] = '\0';
219 /* skip null terminator */
224 /* this is a quick and dirty state machine to */
225 /* go back and strip off any _#'s */
226 /* if there is a better way let me know */
227 while (i
>= 0 && !done
) {
229 /* first we need to check to see if we have seen the first '_' */
230 /* if we have then we already removing chars, continue with that */
231 if ( seen_underscore
) {
232 if (return_filename
[i
] == '_') {
236 return_filename
[i
] = '\0';
238 /* we are still searching for the first underscore */
240 /* first make sure char is a number */
241 if (isdigit((int) return_filename
[i
])) {
243 } else if (return_filename
[i
] == '_' && valid
) {
244 /* yes it is okay to delete the chars */
246 /* incremented, since it is then */
258 /* be sure to g_free this somewhere */
259 return(return_filename
);
262 /*! \todo Finish function documentation!!!
263 * \brief Search SLIB for a particular file name.
264 * \par Function Description
265 * This function will search the SLIB for a particular file name starting
266 * at a location specified by the <B>flag</B> parameter.
268 * \param [in] filename Character string with file name to search for.
270 * Filename is the raw symbol/whatever file name. This function only looks
271 * for the file name as is and does no other changes to it.
274 * Caller must g_free returned pointer.
276 char *s_slib_search_single(const char *filename
)
280 string
= s_slib_search_lowlevel(filename
);
282 /* don't forget to g_free this string */
286 /*! \todo Finish function documentation!!!
288 * \par Function Description
295 for (i
= 0; i
< slib_index
; i
++) {
296 g_free(slib
[i
].dir_name
);
302 /*! \todo Finish function documentation!!!
304 * \par Function Description
310 for (i
= 0; i
< MAX_SLIBS
; i
++) {
311 slib
[i
].dir_name
= NULL
;
315 /*! \todo Finish function documentation!!!
317 * \par Function Description
320 * Caller must not free the returned pointer.
323 char *s_slib_getdir(int index
)
325 if (slib
[index
].dir_name
!= NULL
)
326 return(slib
[index
].dir_name
);
331 /*! \todo Finish function documentation!!!
333 * \par Function Description
335 * \param [in] directory Character string with directory to get files from.
336 * \param [in] flag Search control flag. (See below...)
337 * \return A file name if one is found, NULL otherwise.
340 * Caller must g_free returned pointer.
342 * The flag parameter can be one of the following values:
344 * <DT>OPEN_DIR</DT><DD>Opens the directory and returns NULL.
345 * <DT>READ_DIR</DT><DD>Returns the next non "." entry.
346 * <DT>CLOSE_DIR</DT><DD>Closes the directory.
348 * \bug This is TOTTALLY BROKEN!
349 * statics are not allowed anymore
351 * this function is not reentrant
353 char *s_slib_getfiles(char *directory
, int flag
)
356 static struct dirent
*dptr
;
357 static char *whole_dir
[256]; /* make this dynamic hack */
359 static int current
=0;
372 for (j
= 0 ; j
< count
;j
++) {
373 g_free(whole_dir
[j
]);
375 count
= current
= 0 ;
380 /* open the directory and return first element (after if) */
389 for (j
= 0 ; j
< count
;j
++) {
390 g_free(whole_dir
[j
]);
392 count
= current
= 0 ;
394 ptr
= opendir(directory
); /* hack check for existance */
400 /* now read the entire directory */
403 while (dptr
!= NULL
) {
406 while (dptr
!= NULL
) {
407 if (dptr
->d_name
[0] == '.') {
421 whole_dir
[count
] = g_strdup (dptr
->d_name
);
424 g_error ("uggg. too many files in s_slib_getfiles!\n");
436 if (whole_dir
[current
] && current
< count
) {
437 return(whole_dir
[current
++]);
449 for (j
= 0;j
< count
; j
++) {
450 printf("string: %s\n", whole_dir
[j
]);
456 /*! \todo Finish function documentation!!!
458 * \par Function Description
461 void s_slib_print(void)
465 for (i
= 0; i
< slib_index
; i
++) {
466 printf("%s\n", slib
[i
].dir_name
);
470 /*! \todo Finish function documentation!!!
472 * \par Function Description
475 int s_slib_uniq(char *path
)
477 if (s_slib_search_for_dirname(path
)) {
486 printf("NOT found\n");
493 /*! \todo Finish function documentation!!!
495 * \par Function Description
498 void s_slib_print_dirs(void)
505 string
= s_slib_getdir(i
);
506 while(string
!= NULL
) {
508 s_slib_getfiles(string
, OPEN_DIR
);
509 printf("Opened %s\n", string
);
511 file
= (char *) s_slib_getfiles(string
, READ_DIR
);
513 while(file
!= NULL
) {
514 printf("file: %s\n", file
);
515 file
= (char *) s_slib_getfiles(string
, READ_DIR
);
518 printf("Closed %s\n", string
);
519 s_slib_getfiles(string
, CLOSE_DIR
);
521 string
= s_slib_getdir(i
);