]> www.fi.muni.cz Git - evince.git/blob - dvi/mdvi-lib/files.c
Recent files support.
[evince.git] / dvi / mdvi-lib / files.c
1 /*
2  * Copyright (C) 2000, Matias Atria
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22
23 #include "common.h"
24
25 char    *dgets(Dstring *dstr, FILE *in)
26 {
27         char    buffer[256];
28         
29         dstr->length = 0;
30         if(feof(in))
31                 return NULL;
32         while(fgets(buffer, 256, in) != NULL) {
33                 int     len = strlen(buffer);
34                 
35                 if(buffer[len-1] == '\n') {
36                         dstring_append(dstr, buffer, len - 1);
37                         break;
38                 }
39                 dstring_append(dstr, buffer, len);
40         }
41         if(dstr->data)
42                 dstr->data[dstr->length] = 0;
43         return dstr->data;
44 }
45
46 /* some simple helper functions to manipulate file names */
47
48 const char *file_basename(const char *filename)
49 {
50         const char *ptr = strrchr(filename, '/');
51         
52         return (ptr ? ptr + 1 : filename);
53 }
54
55 const char *file_extension(const char *filename)
56 {
57         const char *ptr = strchr(file_basename(filename), '.');
58         
59         return (ptr ? ptr + 1 : NULL);
60 }
61
62 int     file_readable(const char *filename)
63 {
64         int     status = (access(filename, R_OK) == 0);
65         
66         DEBUG((DBG_FILES, "file_redable(%s) -> %s\n",
67                 filename, status ? "Yes" : "No"));
68         return status;
69 }
70
71 int     file_exists(const char *filename)
72 {
73         int     status = (access(filename, F_OK) == 0);
74         
75         DEBUG((DBG_FILES, "file_exists(%s) -> %s\n",
76                 filename, status ? "Yes" : "No"));
77         return status;
78 }
79
80 static char *xstrchre(const char *string, int c)
81 {
82         const char *ptr;
83         
84         for(ptr = string; *ptr && *ptr != c; ptr++);
85         return (char *)ptr;
86 }
87
88 char    *find_in_path(const char *filename, const char *path)
89 {
90         const char *p, *q;
91         char    *try;
92         int     len;
93         
94         /* if the file is readable as given, return it */
95         if(file_readable(filename))
96                 return xstrdup(filename);
97         
98         /* worst case scenario */
99         try = xmalloc(strlen(path) + strlen(filename) + 2);
100         try[0] = 0;
101
102         for(p = path; *p; p = q) {
103                 q = xstrchre(p, ':');
104                 len = q - p;
105                 xstrncpy(try, p, len);
106                 try[len] = '/';
107                 strcpy(try + len + 1, filename);
108                 if(file_readable(try))
109                         break;
110                 if(*q) q++;
111         }
112
113         if(*p)
114                 return try;
115         else {
116                 xfree(try);
117                 return NULL;
118         }
119 }
120
121 char    *read_into_core(const char *file, size_t *size)
122 {
123         FILE    *in;
124         struct stat st;
125         char    *ptr;
126         
127         in = fopen(file, "r");
128         if(in == NULL)
129                 return NULL;
130         if(fstat(fileno(in), &st) < 0) {
131                 /* I don't think this is possible, but who knows */
132                 fclose(in);
133                 return NULL;
134         }
135         if(st.st_size == 0) {
136                 warning("%s: file is empty\n", file);
137                 fclose(in);
138                 return NULL;
139         }
140         ptr = xmalloc(st.st_size + 1);
141         if(fread(ptr, st.st_size, 1, in) != 1) {
142                 fclose(in);
143                 xfree(ptr);
144                 return NULL;
145         }
146         fclose(in);
147         ptr[st.st_size] = 0;
148         if(size) *size = st.st_size;
149         return ptr;
150 }