LCOV - code coverage report
Current view: top level - experimental/utility - LAGraph_SWrite.c (source / functions) Hit Total Coverage
Test: LAGraph code coverage report. Commit id: 7b9d2ee. Current time (UTC): 2025-06-03T21:57:17Z Lines: 35 35 100.0 %
Date: 2025-06-03 22:02:40 Functions: 4 4 100.0 %

          Line data    Source code
       1             : //------------------------------------------------------------------------------
       2             : // LAGraph_SWrite: write a sequence of serialized objects to a file
       3             : //------------------------------------------------------------------------------
       4             : 
       5             : // LAGraph, (c) 2019-2022 by The LAGraph Contributors, All Rights Reserved.
       6             : // SPDX-License-Identifier: BSD-2-Clause
       7             : //
       8             : // For additional details (including references to third party source code and
       9             : // other files) see the LICENSE file or contact permission@sei.cmu.edu. See
      10             : // Contributors.txt for a full list of contributors. Created, in part, with
      11             : // funding and support from the U.S. Government (see Acknowledgments.txt file).
      12             : // DM22-0790
      13             : 
      14             : // Contributed by Timothy A. Davis, Texas A&M University
      15             : 
      16             : //------------------------------------------------------------------------------
      17             : 
      18             : #include "LG_internal.h"
      19             : #include "LAGraphX.h"
      20             : 
      21             : #define FPRINT(params)                                  \
      22             : {                                                       \
      23             :     int result = fprintf params ;                       \
      24             :     LG_ASSERT_MSG (result >= 0, -1002, "file not written") ;  \
      25             : }
      26             : 
      27             : //------------------------------------------------------------------------------
      28             : // LAGraph_SWrite_HeaderStart
      29             : //------------------------------------------------------------------------------
      30             : 
      31         208 : int LAGraph_SWrite_HeaderStart  // write the first part of the JSON header
      32             : (
      33             :     FILE *f,                    // file to write to
      34             :     const char *name,           // name of this collection of matrices
      35             :     char *msg
      36             : )
      37             : {
      38             :     // check inputs
      39         208 :     LG_CLEAR_MSG ;
      40         208 :     LG_ASSERT (f != NULL && name != NULL, GrB_NULL_POINTER) ;
      41             : 
      42             :     // write the first part of the JSON header to the file
      43         206 :     FPRINT ((f, "{\n    \"LAGraph\": [%d,%d,%d],\n    \"GraphBLAS\": [ ",
      44             :         LAGRAPH_VERSION_MAJOR, LAGRAPH_VERSION_MINOR, LAGRAPH_VERSION_UPDATE)) ;
      45             : 
      46             :     char library [256] ;
      47             :     int32_t ver [3] ;
      48         206 :     GRB_TRY (GrB_get (GrB_GLOBAL, library, GrB_NAME)) ;
      49         206 :     GRB_TRY (GrB_get (GrB_GLOBAL, &(ver [0]), GrB_LIBRARY_VER_MAJOR)) ;
      50         206 :     GRB_TRY (GrB_get (GrB_GLOBAL, &(ver [1]), GrB_LIBRARY_VER_MINOR)) ;
      51         206 :     GRB_TRY (GrB_get (GrB_GLOBAL, &(ver [2]), GrB_LIBRARY_VER_PATCH)) ;
      52         206 :     FPRINT ((f, "\"%s\", [%d,%d,%d] ],\n", library,
      53             :         ver [0], ver [1], ver [2])) ;
      54             : 
      55             :     // write name of this collection and start the list of items
      56         206 :     FPRINT ((f, "    \"%s\":\n    [\n", name)) ;
      57         206 :     return (GrB_SUCCESS) ;
      58             : }
      59             : 
      60             : //------------------------------------------------------------------------------
      61             : // LAGraph_SWrite_HeaderItem
      62             : //------------------------------------------------------------------------------
      63             : 
      64         259 : int LAGraph_SWrite_HeaderItem   // write a single item to the JSON header
      65             : (
      66             :     // inputs:
      67             :     FILE *f,                    // file to write to
      68             :     LAGraph_Contents_kind kind, // matrix, vector, or text
      69             :     const char *name,           // name of the matrix/vector/text; matrices from
      70             :                                 // sparse.tamu.edu use the form "Group/Name"
      71             :     const char *type,           // name of type of the matrix/vector
      72             :     // todo: vectors and text not yet supported by LAGraph_SWrite_HeaderItem
      73             :     int compression,            // text compression method
      74             :     GrB_Index blob_size,        // exact size of serialized blob for this item
      75             :     char *msg
      76             : )
      77             : {
      78             :     // check inputs
      79         259 :     LG_CLEAR_MSG ;
      80         259 :     LG_ASSERT (f != NULL, GrB_NULL_POINTER) ;
      81             : 
      82             :     // write the JSON information for this item
      83         257 :     FPRINT ((f, "        { \"")) ;
      84         257 :     switch (kind)
      85             :     {
      86         256 :         case LAGraph_matrix_kind :
      87         256 :             FPRINT ((f, "GrB_Matrix\": \"%s\", \"type\": \"%s", name, type)) ;
      88         256 :             break ;
      89             : 
      90             :         // todo: handle vectors and text
      91             : #if 0
      92             :         case LAGraph_vector_kind :
      93             :             FPRINT((f, "GrB_Vector\": \"%s\", \"type\": \"%s", name, type)) ;
      94             :             break ;
      95             : 
      96             :         case LAGraph_text_kind :
      97             :             FPRINT ((f, "text\": \"%s\", \"compression\": \"", name)) ;
      98             :             switch (compression)
      99             :             {
     100             :                 case -1   : FPRINT ((f, "none"   )) ; break ;
     101             :                 case 0    : FPRINT ((f, "default")) ; break ;
     102             :                 case 1000 : FPRINT ((f, "lz4"    )) ; break ;
     103             :                 case 2000 : FPRINT ((f, "lz4hc:0")) ; break ;
     104             :                 case 2001 : FPRINT ((f, "lz4hc:1")) ; break ;
     105             :                 case 2002 : FPRINT ((f, "lz4hc:2")) ; break ;
     106             :                 case 2003 : FPRINT ((f, "lz4hc:3")) ; break ;
     107             :                 case 2004 : FPRINT ((f, "lz4hc:4")) ; break ;
     108             :                 case 2005 : FPRINT ((f, "lz4hc:5")) ; break ;
     109             :                 case 2006 : FPRINT ((f, "lz4hc:6")) ; break ;
     110             :                 case 2007 : FPRINT ((f, "lz4hc:7")) ; break ;
     111             :                 case 2008 : FPRINT ((f, "lz4hc:8")) ; break ;
     112             :                 case 2009 : FPRINT ((f, "lz4hc:9")) ; break ;
     113             :                 default   : LG_ASSERT_MSG (false, GrB_INVALID_VALUE,
     114             :                     "invalid compression") ; break ;
     115             :             }
     116             :             break ;
     117             : #endif
     118             : 
     119           1 :         default :
     120           1 :             LG_ASSERT_MSG (false, GrB_INVALID_VALUE, "invalid kind") ;
     121             :             break ;
     122             :     }
     123             : 
     124         256 :     FPRINT ((f, "\", \"bytes\": %" PRIu64 " },\n", blob_size)) ;
     125         256 :     return (GrB_SUCCESS) ;
     126             : }
     127             : 
     128             : //------------------------------------------------------------------------------
     129             : // LAGraph_SWrite_HeaderEnd
     130             : //------------------------------------------------------------------------------
     131             : 
     132         207 : int LAGraph_SWrite_HeaderEnd    // write the end of the JSON header
     133             : (
     134             :     FILE *f,                    // file to write to
     135             :     char *msg
     136             : )
     137             : {
     138             :     // check inputs
     139         207 :     LG_CLEAR_MSG ;
     140         207 :     LG_ASSERT (f != NULL, GrB_NULL_POINTER) ;
     141             : 
     142             :     // finalize the JSON header string
     143         206 :     FPRINT ((f, "        null\n    ]\n}\n")) ;
     144             : 
     145             :     // write a final zero byte to terminate the JSON string
     146         206 :     fputc (0, f) ;
     147         206 :     return (GrB_SUCCESS) ;
     148             : }
     149             : 
     150             : //------------------------------------------------------------------------------
     151             : // LAGraph_SWrite_Item
     152             : //------------------------------------------------------------------------------
     153             : 
     154         258 : int LAGraph_SWrite_Item  // write the serialized blob of a matrix/vector/text
     155             : (
     156             :     // input:
     157             :     FILE *f,                // file to write to
     158             :     const void *blob,       // serialized blob from G*B_Matrix_serialize
     159             :     GrB_Index blob_size,    // exact size of the serialized blob
     160             :     char *msg
     161             : )
     162             : {
     163             :     // check inputs
     164         258 :     LG_CLEAR_MSG ;
     165         258 :     LG_ASSERT (f != NULL && blob != NULL, GrB_NULL_POINTER) ;
     166             : 
     167             :     // write the blob
     168         256 :     size_t blob_written = fwrite (blob, sizeof (uint8_t), blob_size, f) ;
     169         256 :     LG_ASSERT_MSG (blob_written == blob_size, -1001,
     170             :         "file not written properly") ;
     171         256 :     return (GrB_SUCCESS) ;
     172             : }

Generated by: LCOV version 1.14