GnuCash  5.6-150-g038405b370+
Files | Functions

Files

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

Functions

int gncReadFile (const char *filename, char **data)
 Reads the contents of a file into a buffer for further processing. More...
 
gint64 gnc_getline (gchar **line, FILE *file)
 Read a line from the input file, up to and including the newline. More...
 

Detailed Description

Function Documentation

◆ gnc_getline()

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 133 of file file-utils.c.

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 }

◆ gncReadFile()

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  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 }
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
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...