]> www.fi.muni.cz Git - evince.git/blob - backend/dvi/mdvi-lib/files.c
0ed893b6ef4662996d7c267764ff76694fb1b0ab
[evince.git] / backend / 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