GnuCash c935c2f+
Loading...
Searching...
No Matches
file-utils.c
1/********************************************************************\
2 * file-utils.c -- simple file utilities *
3 * Copyright (C) 1997 Robin D. Clark <rclark@cs.hmc.edu> *
4 * Copyright (C) 1998 Rob Browning *
5 * Copyright (C) 1998-2000 Linas Vepstas <linas@linas.org> *
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. */
47static 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\********************************************************************/
60int
61gncReadFile (const char * filename, char ** data)
62{
63 char *buf = NULL;
64 char *fullname;
65 off_t 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 if (size < 0)
93 {
94 int norr = errno;
95 PERR ("file seek-to-end %s: (%d) %s\n", filename, norr, strerror(norr));
96 return 0;
97 }
98
99 /* Allocate memory */
100 buf = g_new(char, (size_t)size + 1);
101
102 /* read in file */
103 if ( read(fd, buf, (size_t)size) == -1 )
104 {
105 g_free(buf);
106 buf = NULL;
107 }
108 else
109 {
110 buf[(size_t)size] = '\0';
111 }
112
113 close(fd);
114 *data = buf;
115
116 return size;
117}
118
119/***********************************************************************
120 * gnc_getline -- read a line from the input file, up to and including
121 * the newline.
122 *
123 * Args: line - pointer to hold the buffer for the whole line (allocated by
124 * this function)
125 * file - the file from which to read
126 * Return: the number of bytes read
127 *
128 * The caller MUST g_free() the line returned from this call in all
129 * cases where it is non-NULL!
130 */
131
132gint64
133gnc_getline (gchar **line, FILE *file)
134{
135 char str[BUFSIZ];
136 gint64 len;
137 GString *gs;
138
139 g_return_val_if_fail(line, -1);
140 *line = NULL;
141 g_return_val_if_fail(file, -1);
142
143 gs = g_string_new("");
144
145 while (fgets(str, sizeof(str), file) != NULL)
146 {
147 g_string_append(gs, str);
148
149 len = strlen(str);
150 if (str[len-1] == '\n')
151 break;
152 }
153
154 len = gs->len;
155 *line = g_string_free (gs, FALSE);
156 return len;
157}
158
159/* ----------------------- END OF FILE --------------------- */
Utility functions for file access.
All type declarations for the whole Gnucash engine.
File path resolution utility functions.
GKeyFile helper routines.
Utility functions for convert uri in separate components and back.
#define PERR(format, args...)
Log a serious error.
Definition qoflog.h:244
int gncReadFile(const char *filename, char **data)
Reads the contents of a file into a buffer for further processing.
Definition file-utils.c:61
gint64 gnc_getline(gchar **line, FILE *file)
Read a line from the input file, up to and including the newline.
Definition file-utils.c:133