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