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