GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Files | Functions

Files

file  file-utils.h
 Utility functions for file access.
 

Functions

int gncReadFile (const char *filename, char **data)
 
gint64 gnc_getline (gchar **line, FILE *file)
 

Detailed Description

Function Documentation

gint64 gnc_getline ( gchar **  line,
FILE *  file 
)

Read a line from the input file, up to and including the newline.

The caller MUST g_free() the line returned from this call in all cases where it is non-NULL!

Parameters
linepointer to hold the buffer for the whole line (allocated by this function)
filethe file from which to read
Returns
The number of bytes read

Definition at line 126 of file file-utils.c.

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 }
int gncReadFile ( const char *  filename,
char **  data 
)

Reads the contents of a file into a buffer for further processing.

If the filename is not an absolute filename, it will be searched for in the search path available to the program. This can be used to find for example help files,...

Uses the global xxxPath as the path to search

Parameters
filenamethe name of the html file to read
dataPointer to a buffer to store the file's content.
Returns
The file size

Definition at line 61 of file file-utils.c.

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 }
#define PERR(format, args...)
Definition: qoflog.h:237
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...