LCOV - code coverage report
Current view: top level - libgnucash/app-utils - file-utils.c (source / functions) Coverage Total Hit
Test: gnucash.info Lines: 0.0 % 39 0
Test Date: 2025-02-07 16:25:45 Functions: 0.0 % 2 0
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       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.  */
      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                 :           0 : gncReadFile (const char * filename, char ** data)
      62                 :             : {
      63                 :           0 :     char *buf = NULL;
      64                 :             :     char  *fullname;
      65                 :           0 :     off_t   size = 0;
      66                 :             :     int   fd;
      67                 :             : 
      68                 :           0 :     if (!filename || filename[0] == '\0') return 0;
      69                 :             : 
      70                 :             :     /* construct absolute path if we received a relative path */
      71                 :           0 :     fullname = gnc_path_find_localized_html_file (filename);
      72                 :             : 
      73                 :           0 :     if (!fullname) return 0;
      74                 :             : 
      75                 :             :     /* Open file: */
      76                 :           0 :     fd = g_open( fullname, O_RDONLY, 0 );
      77                 :             : 
      78                 :           0 :     g_free(fullname);
      79                 :           0 :     fullname = NULL;
      80                 :             : 
      81                 :           0 :     if ( fd == -1 )
      82                 :             :     {
      83                 :           0 :         int norr = errno;
      84                 :           0 :         PERR ("file %s: (%d) %s\n", filename, norr, strerror(norr));
      85                 :           0 :         return 0;
      86                 :             :     }
      87                 :             : 
      88                 :             :     /* Find size: */
      89                 :           0 :     size = lseek( fd, 0, SEEK_END );
      90                 :           0 :     lseek( fd, 0, SEEK_SET );
      91                 :             : 
      92                 :           0 :     if (size < 0)
      93                 :             :     {
      94                 :           0 :         int norr = errno;
      95                 :           0 :         PERR ("file seek-to-end %s: (%d) %s\n", filename, norr, strerror(norr));
      96                 :           0 :         return 0;
      97                 :             :     }
      98                 :             :     
      99                 :             :     /* Allocate memory */
     100                 :           0 :     buf = g_new(char, (size_t)size + 1);
     101                 :             : 
     102                 :             :     /* read in file */
     103                 :           0 :     if ( read(fd, buf, (size_t)size) == -1 )
     104                 :             :     {
     105                 :           0 :         g_free(buf);
     106                 :           0 :         buf = NULL;
     107                 :             :     }
     108                 :             :     else
     109                 :             :     {
     110                 :           0 :         buf[(size_t)size] = '\0';
     111                 :             :     }
     112                 :             : 
     113                 :           0 :     close(fd);
     114                 :           0 :     *data = buf;
     115                 :             : 
     116                 :           0 :     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                 :             : 
     132                 :             : gint64
     133                 :           0 : gnc_getline (gchar **line, FILE *file)
     134                 :             : {
     135                 :             :     char str[BUFSIZ];
     136                 :             :     gint64 len;
     137                 :             :     GString *gs;
     138                 :             : 
     139                 :           0 :     g_return_val_if_fail(line, -1);
     140                 :           0 :     *line = NULL;
     141                 :           0 :     g_return_val_if_fail(file, -1);
     142                 :             : 
     143                 :           0 :     gs = g_string_new("");
     144                 :             : 
     145                 :           0 :     while (fgets(str, sizeof(str), file) != NULL)
     146                 :             :     {
     147                 :             :         g_string_append(gs, str);
     148                 :             : 
     149                 :           0 :         len = strlen(str);
     150                 :           0 :         if (str[len-1] == '\n')
     151                 :           0 :             break;
     152                 :             :     }
     153                 :             : 
     154                 :           0 :     len = gs->len;
     155                 :           0 :     *line = g_string_free (gs, FALSE);
     156                 :           0 :     return len;
     157                 :             : }
     158                 :             : 
     159                 :             : /* ----------------------- END OF FILE ---------------------  */
        

Generated by: LCOV version 2.0-1