Avoid ctype.h .
[yaz-moved-to-github.git] / src / tpath.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file tpath.c
7  * \brief File Path utilities
8  */
9
10 #if HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13
14
15 #include <stdio.h>
16 #include <string.h>
17 #include <yaz/tpath.h>
18 #include <yaz/log.h>
19 #if HAVE_SYS_TYPES_H
20 #include <sys/types.h>
21 #endif
22 #if HAVE_SYS_STAT_H
23 #include <sys/stat.h>
24 #endif
25 #if WIN32
26 #include <sys/stat.h>
27 #endif
28
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
33 FILE *yaz_path_fopen(const char *path, const char *name, const char *mode)
34 {
35     return yaz_fopen (path, name, mode, 0);
36 }
37
38 int yaz_fclose (FILE *f)
39 {
40     return fclose (f);
41 }
42
43
44 size_t yaz_filepath_comp(const char **path_p, const char **comp)
45 {
46     const char *path = *path_p;
47     size_t len;
48     const char *path_sep;
49
50     /* somewhat dirty since we have to consider Windows
51      * drive letters..
52      */
53     if (path[0] && strchr ("/\\.", path[0]))
54         path_sep = strchr (path+1, ':');
55     else if (path[0] && path[1])
56         path_sep = strchr (path+2, ':');
57     else
58         path_sep = 0;
59     
60     if (path_sep)
61     {
62         len = path_sep - path;
63         *path_p = path + len + 1;
64     }
65     else
66     {
67         len = strlen(path);
68         *path_p = path + len;
69     }
70     *comp = path;
71     return len;
72 }
73
74 char *yaz_filepath_resolve(const char *fname, const char *path,
75                            const char *base, char *fullpath)
76 {
77     for(;;)
78     {
79         struct stat stat_buf;
80         size_t slen = 0;
81        
82         *fullpath = '\0';
83         if (path)
84         {
85             const char *comp;
86             size_t len = 0;
87
88             len = yaz_filepath_comp(&path, &comp);
89             if (!len)
90                 break;
91
92             if (!strchr ("/\\", *comp) && base)
93             {
94                 /* yes: make base the first part */
95                 strcpy (fullpath, base);
96                 slen = strlen(fullpath);
97                 fullpath[slen++] = '/';
98             }
99             memcpy (fullpath+slen, comp, len);
100             slen += len;
101             if (slen > 0 && !strchr("/\\", fullpath[slen-1]))
102                 fullpath[slen++] = '/';
103         }
104         strcpy (fullpath+slen, fname);
105         if (stat(fullpath, &stat_buf) == 0)
106             return fullpath;
107         if (!path)
108             break;
109     }
110     return 0;
111 }
112
113 FILE *yaz_fopen(const char *path, const char *fname, const char *mode,
114                 const char *base)
115 {
116     char fullpath[1024];
117
118     if (!yaz_filepath_resolve(fname, path, base, fullpath))
119         return 0; /* failure */
120     return fopen(fullpath, mode);
121 }
122
123 int yaz_is_abspath (const char *p)
124 {
125     if (*p == '/')
126         return 1;
127 #ifdef WIN32
128     if (*p == '\\')
129         return 1;
130     if (*p && p[1] == ':' && isalpha(*p))
131         return 1;
132 #endif
133     return 0;
134 }
135 /*
136  * Local variables:
137  * c-basic-offset: 4
138  * c-file-style: "Stroustrup"
139  * indent-tabs-mode: nil
140  * End:
141  * vim: shiftwidth=4 tabstop=8 expandtab
142  */
143