Fix sample PQF
[yaz-moved-to-github.git] / util / tpath.c
1 /*
2  * Copyright (c) 1995-2003, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: tpath.c,v 1.9 2003-01-06 08:20:28 adam Exp $
7  */
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <yaz/tpath.h>
17 #include <yaz/log.h>
18
19 FILE *yaz_path_fopen(const char *path, const char *name, const char *mode)
20 {
21     return yaz_fopen (path, name, mode, 0);
22 }
23
24 int yaz_fclose (FILE *f)
25 {
26     return fclose (f);
27 }
28
29 FILE *yaz_fopen(const char *path, const char *name, const char *mode,
30                 const char *base)
31 {
32     char spath[1024];
33
34     for(;;)
35     {
36         FILE *f;
37
38         const char *path_sep = 0;
39         size_t len = 0;
40         size_t slen = 0;
41         
42         *spath = '\0';
43         if (path)
44         {
45             /* somewhat dirty since we have to consider Windows
46              * drive letters..
47              */
48             if (strchr ("/\\.", *path))
49             {
50                 path_sep = strchr (path+1, ':');
51             }
52             else if (path[0] && path[1])
53                 path_sep = strchr (path+2, ':');
54             if (path_sep)
55                 len = path_sep - path;
56             else
57                 len = strlen(path);
58             if (!strchr ("/\\", *path) && base)
59             {
60                 strcpy (spath, base);
61                 slen = strlen(spath);
62                 spath[slen++] = '/';
63             }
64             memcpy (spath+slen, path, len);
65             slen += len;
66             if (!strchr("/\\", spath[slen-1]))
67                 spath[slen++] = '/';
68         }
69         strcpy (spath+slen, name);
70         if ((f = fopen(spath, mode)))
71             return f;
72         
73         if (!path_sep)
74             break;
75         path = path_sep+1;
76     }
77     return 0;
78 }
79
80 int yaz_is_abspath (const char *p)
81 {
82     if (*p == '/')
83         return 1;
84 #ifdef WIN32
85     if (*p == '\\')
86         return 1;
87     if (*p && p[1] == ':' && isalpha(*p))
88         return 1;
89 #endif
90     return 0;
91 }