LCOV - code coverage report
Current view: top level - lib/formats - csv.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 16 17 94.1 %
Date: 2015-09-30 14:09:30 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  This file is part of rmlint.
       3             :  *
       4             :  *  rmlint is free software: you can redistribute it and/or modify
       5             :  *  it under the terms of the GNU General Public License as published by
       6             :  *  the Free Software Foundation, either version 3 of the License, or
       7             :  *  (at your option) any later version.
       8             :  *
       9             :  *  rmlint is distributed in the hope that it will be useful,
      10             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  *  GNU General Public License for more details.
      13             :  *
      14             :  *  You should have received a copy of the GNU General Public License
      15             :  *  along with rmlint.  If not, see <http://www.gnu.org/licenses/>.
      16             :  *
      17             :  * Authors:
      18             :  *
      19             :  *  - Christopher <sahib> Pahl 2010-2015 (https://github.com/sahib)
      20             :  *  - Daniel <SeeSpotRun> T.   2014-2015 (https://github.com/SeeSpotRun)
      21             :  *
      22             :  * Hosted on http://github.com/sahib/rmlint
      23             :  *
      24             :  */
      25             : 
      26             : #include "../formats.h"
      27             : #include "../utilities.h"
      28             : 
      29             : #include <glib.h>
      30             : #include <stdio.h>
      31             : #include <string.h>
      32             : 
      33             : #define CSV_SEP ","
      34             : #define CSV_QUOTE "\""
      35             : #define CSV_FORMAT \
      36             :     "%s" CSV_SEP "" CSV_QUOTE "%s" CSV_QUOTE "" CSV_SEP "%" LLU "" CSV_SEP "%s\n"
      37             : 
      38             : typedef struct RmFmtHandlerCSV {
      39             :     /* must be first */
      40             :     RmFmtHandler parent;
      41             : } RmFmtHandlerProgress;
      42             : 
      43          56 : static void rm_fmt_head(_U RmSession *session, _U RmFmtHandler *parent, FILE *out) {
      44          56 :     if(rm_fmt_get_config_value(session->formats, "csv", "no_header")) {
      45           0 :         return;
      46             :     }
      47             : 
      48          56 :     fprintf(out, "%s%s%s%s%s%s%s\n", "type", CSV_SEP, "path", CSV_SEP, "size", CSV_SEP,
      49             :             "checksum");
      50             : }
      51             : 
      52         168 : static void rm_fmt_elem(_U RmSession *session, _U RmFmtHandler *parent, FILE *out,
      53         168 :                         RmFile *file) {
      54         168 :     char checksum_str[rm_digest_get_bytes(file->digest) * 2 + 1];
      55         168 :     memset(checksum_str, '0', sizeof(checksum_str));
      56         168 :     checksum_str[sizeof(checksum_str) - 1] = 0;
      57             : 
      58         168 :     if(file->digest) {
      59         168 :         rm_digest_hexstring(file->digest, checksum_str);
      60             :     }
      61             : 
      62             :     /* Escape quotes in the path (refer http://tools.ietf.org/html/rfc4180, item 6)*/
      63         168 :     RM_DEFINE_PATH(file);
      64         168 :     char *clean_path = rm_util_strsub(file_path, CSV_QUOTE, CSV_QUOTE "" CSV_QUOTE);
      65             : 
      66         168 :     fprintf(out, CSV_FORMAT, rm_file_lint_type_to_string(file->lint_type), clean_path,
      67         168 :             file->file_size, checksum_str);
      68             : 
      69         168 :     g_free(clean_path);
      70         168 : }
      71             : 
      72             : static RmFmtHandlerProgress CSV_HANDLER_IMPL = {
      73             :     /* Initialize parent */
      74             :     .parent = {
      75             :         .size = sizeof(CSV_HANDLER_IMPL),
      76             :         .name = "csv",
      77             :         .head = rm_fmt_head,
      78             :         .elem = rm_fmt_elem,
      79             :         .prog = NULL,
      80             :         .foot = NULL,
      81             :         .valid_keys = {"no_header", NULL},
      82             :     }};
      83             : 
      84             : RmFmtHandler *CSV_HANDLER = (RmFmtHandler *)&CSV_HANDLER_IMPL;

Generated by: LCOV version 1.11