GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
file-utils.c
1 /********************************************************************\
2  * file-utils.c -- simple file utilities *
3  * Copyright (C) 1997 Robin D. Clark <[email protected]> *
4  * Copyright (C) 1998 Rob Browning *
5  * Copyright (C) 1998-2000 Linas Vepstas <[email protected]> *
6  * *
7  * This program is free software; you can redistribute it and/or *
8  * modify it under the terms of the GNU General Public License as *
9  * published by the Free Software Foundation; either version 2 of *
10  * the License, or (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License*
18  * along with this program; if not, write to the Free Software *
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
20 \********************************************************************/
21 
22 #include "config.h"
23 
24 #include <glib.h>
25 #include <glib/gstdio.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #else
34 # include <io.h>
35 # define close _close
36 # define lseek _lseek
37 # define read _read
38 #endif
39 
40 #include "file-utils.h"
41 #include "gnc-engine.h"
42 #include "gnc-filepath-utils.h"
43 #include "gnc-gkeyfile-utils.h"
44 #include "gnc-uri-utils.h"
45 
46 /* This static indicates the debugging module that this .o belongs to. */
47 static QofLogModule log_module = G_LOG_DOMAIN;
48 
49 /********************************************************************\
50 \********************************************************************/
51 
52 /********************************************************************\
53  * gncReadFile *
54  * *
55  * Args: filename - the name of the html file to read *
56  * data - pointer to set to the buffer of data read in *
57  * Return: size of data read *
58  * Global: helpPath - the path to the help files *
59 \********************************************************************/
60 int
61 gncReadFile (const char * filename, char ** data)
62 {
63  char *buf = NULL;
64  char *fullname;
65  int size = 0;
66  int fd;
67 
68  if (!filename || filename[0] == '\0') return 0;
69 
70  /* construct absolute path if we received a relative path */
71  fullname = gnc_path_find_localized_html_file (filename);
72 
73  if (!fullname) return 0;
74 
75  /* Open file: */
76  fd = g_open( fullname, O_RDONLY, 0 );
77 
78  g_free(fullname);
79  fullname = NULL;
80 
81  if ( fd == -1 )
82  {
83  int norr = errno;
84  PERR ("file %s: (%d) %s \n", filename, norr, strerror(norr));
85  return 0;
86  }
87 
88  /* Find size: */
89  size = lseek( fd, 0, SEEK_END );
90  lseek( fd, 0, SEEK_SET );
91 
92  /* Allocate memory */
93  buf = g_new(char, size + 1);
94 
95  /* read in file */
96  if ( read(fd, buf, size) == -1 )
97  {
98  g_free(buf);
99  buf = NULL;
100  }
101  else
102  {
103  buf[size] = '\0';
104  }
105 
106  close(fd);
107  *data = buf;
108 
109  return size;
110 }
111 
112 /***********************************************************************
113  * gnc_getline -- read a line from the input file, up to and including
114  * the newline.
115  *
116  * Args: line - pointer to hold the buffer for the whole line (allocated by
117  * this function)
118  * file - the file from which to read
119  * Return: the number of bytes read
120  *
121  * The caller MUST g_free() the line returned from this call in all
122  * cases where it is non-NULL!
123  */
124 
125 gint64
126 gnc_getline (gchar **line, FILE *file)
127 {
128  char str[BUFSIZ];
129  gint64 len;
130  GString *gs;
131 
132  g_return_val_if_fail(line, -1);
133  *line = NULL;
134  g_return_val_if_fail(file, -1);
135 
136  gs = g_string_new("");
137 
138  while (fgets(str, sizeof(str), file) != NULL)
139  {
140  g_string_append(gs, str);
141 
142  len = strlen(str);
143  if (str[len-1] == '\n')
144  break;
145  }
146 
147  len = gs->len;
148  *line = gs->str;
149  g_string_free(gs, FALSE);
150  return len;
151 }
152 
153 /* ----------------------- END OF FILE --------------------- */
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
GKeyFile helper routines.
#define PERR(format, args...)
Definition: qoflog.h:237
gint64 gnc_getline(gchar **line, FILE *file)
Definition: file-utils.c:126
All type declarations for the whole Gnucash engine.
Utility functions for file access.
gchar * gnc_path_find_localized_html_file(const gchar *file_name)
Find an absolute path to a localized version of a given relative path to a html or html related file...
Utility functions for convert uri in separate components and back.
File path resolution utility functions.
int gncReadFile(const char *filename, char **data)
Definition: file-utils.c:61
const gchar * QofLogModule
Definition: qofid.h:89