LCOV - code coverage report
Current view: top level - ini - ini_comment.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 174 226 77.0 %
Date: 2014-04-01 Functions: 19 20 95.0 %

          Line data    Source code
       1             : /*
       2             :     INI LIBRARY
       3             : 
       4             :     Object to handle comments
       5             : 
       6             :     Copyright (C) Dmitri Pal <dpal@redhat.com> 2010
       7             : 
       8             :     INI Library is free software: you can redistribute it and/or modify
       9             :     it under the terms of the GNU Lesser General Public License as published by
      10             :     the Free Software Foundation, either version 3 of the License, or
      11             :     (at your option) any later version.
      12             : 
      13             :     INI Library is distributed in the hope that it will be useful,
      14             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :     GNU Lesser General Public License for more details.
      17             : 
      18             :     You should have received a copy of the GNU Lesser General Public License
      19             :     along with INI Library.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include "config.h"
      23             : #include <errno.h>
      24             : #include <string.h>
      25             : #include <ctype.h>
      26             : #include "trace.h"
      27             : #include "ref_array.h"
      28             : #include "simplebuffer.h"
      29             : #include "ini_comment.h"
      30             : #include "ini_defines.h"
      31             : 
      32             : /* The lines will increment in this number */
      33             : #define INI_COMMENT_BLOCK 10
      34             : /* Default comment length */
      35             : #define INI_COMMENT_LEN 100
      36             : 
      37             : 
      38             : /***************************/
      39             : /* Internal comment states */
      40             : /***************************/
      41             : /* Empty - initial */
      42             : #define INI_COMMENT_EMPTY   0
      43             : /* Read - read from file */
      44             : #define INI_COMMENT_READ    1
      45             : /* Comment was altered */
      46             : #define INI_COMMENT_CHANGED 2
      47             : 
      48             : 
      49             : /*********************************/
      50             : /* Modes to wrap ref array calls */
      51             : /*********************************/
      52             : #define INI_COMMENT_MODE_BUILD      1
      53             : #define INI_COMMENT_MODE_APPEND     2
      54             : #define INI_COMMENT_MODE_INSERT     3
      55             : #define INI_COMMENT_MODE_REPLACE    4
      56             : #define INI_COMMENT_MODE_REMOVE     5
      57             : #define INI_COMMENT_MODE_CLEAR      6
      58             : 
      59             : /****************************************/
      60             : /* Internal structure to hold a comment */
      61             : /****************************************/
      62             : struct ini_comment {
      63             :     struct ref_array *ra;
      64             :     uint32_t state;
      65             : };
      66             : 
      67             : 
      68             : /****************************************/
      69             : 
      70             : /* Destroy the comment object */
      71      134921 : void ini_comment_destroy(struct ini_comment *ic)
      72             : {
      73             : 
      74             :     TRACE_FLOW_ENTRY();
      75      134921 :     if (ic) {
      76             :         /* Function will check for NULL */
      77       62832 :         ref_array_destroy(ic->ra);
      78       62832 :         free(ic);
      79             :     }
      80             :     TRACE_FLOW_EXIT();
      81      134921 : }
      82             : 
      83             : 
      84             : /* Cleanup callback */
      85       65670 : static void ini_comment_cb(void *elem,
      86             :                            ref_array_del_enum type,
      87             :                            void *data)
      88             : {
      89             : 
      90             :     TRACE_FLOW_ENTRY();
      91       65670 :     simplebuffer_free(*((struct simplebuffer **)elem));
      92             :     TRACE_FLOW_EXIT();
      93       65670 : }
      94             : 
      95             : 
      96             : /* Create a comment object */
      97       31683 : int ini_comment_create(struct ini_comment **ic)
      98             : {
      99       31683 :     int error = EOK;
     100       31683 :     struct ref_array *ra = NULL;
     101       31683 :     struct ini_comment *ic_new = NULL;
     102             : 
     103             :     TRACE_FLOW_ENTRY();
     104             : 
     105       31683 :     error = ref_array_create(&ra,
     106             :                              sizeof(struct simplebuffer *),
     107             :                              INI_COMMENT_BLOCK,
     108             :                              ini_comment_cb,
     109             :                              NULL);
     110       31683 :     if (error) {
     111             :         TRACE_ERROR_NUMBER("Error creating ref array", error);
     112             :         return error;
     113             :     }
     114             : 
     115       31683 :     ic_new = malloc(sizeof(struct ini_comment));
     116       31683 :     if (!ic_new) {
     117             :         TRACE_ERROR_NUMBER("Memory allocation error", ENOMEM);
     118           0 :         ref_array_destroy(ra);
     119           0 :         return ENOMEM;
     120             :     }
     121             : 
     122             :     /* Initialize members here */
     123       31683 :     ic_new->ra = ra;
     124       31683 :     ic_new->state = INI_COMMENT_EMPTY;
     125             : 
     126       31683 :     *ic = ic_new;
     127             : 
     128             :     TRACE_FLOW_EXIT();
     129       31683 :     return error;
     130             : }
     131             : 
     132             : /* Callback to copy comment */
     133       32318 : static int ini_comment_copy_cb(void *elem,
     134             :                                void *new_elem)
     135             : {
     136       32318 :     int error = EOK;
     137       32318 :     struct simplebuffer *sb = NULL;
     138       32318 :     struct simplebuffer *sb_new = NULL;
     139             : 
     140             :     TRACE_FLOW_ENTRY();
     141             : 
     142       32318 :     error = simplebuffer_alloc(&sb_new);
     143       32318 :     if (error) {
     144             :         TRACE_ERROR_NUMBER("Failed to allocate buffer", error);
     145             :         return error;
     146             :     }
     147             : 
     148       32318 :     sb = *((struct simplebuffer **)elem);
     149       64636 :     error = simplebuffer_add_str(sb_new,
     150       32318 :                                  (const char *)simplebuffer_get_buf(sb),
     151             :                                  simplebuffer_get_len(sb),
     152             :                                  INI_COMMENT_LEN);
     153       32318 :     if (error) {
     154             :         TRACE_ERROR_NUMBER("Failed to allocate buffer", error);
     155           0 :         simplebuffer_free(sb_new);
     156           0 :         return error;
     157             :     }
     158             : 
     159       32318 :     *((struct simplebuffer **)new_elem) = sb_new;
     160             : 
     161             :     TRACE_FLOW_EXIT();
     162       32318 :     return error;
     163             : }
     164             : 
     165             : 
     166             : /* Create a copy of the comment object */
     167       31149 : int ini_comment_copy(struct ini_comment *ic,
     168             :                      struct ini_comment **ic_copy)
     169             : {
     170       31149 :     int error = EOK;
     171       31149 :     struct ref_array *ra = NULL;
     172       31149 :     struct ini_comment *ic_new = NULL;
     173             : 
     174             :     TRACE_FLOW_ENTRY();
     175             : 
     176       31149 :     error = ref_array_copy(ic->ra,
     177             :                            ini_comment_copy_cb,
     178             :                            ini_comment_cb,
     179             :                            NULL,
     180             :                            &ra);
     181       31149 :     if (error) {
     182             :         TRACE_ERROR_NUMBER("Error creating a copy of ref array", error);
     183             :         return error;
     184             :     }
     185             : 
     186       31149 :     ic_new = malloc(sizeof(struct ini_comment));
     187       31149 :     if (!ic_new) {
     188             :         TRACE_ERROR_NUMBER("Memory allocation error", ENOMEM);
     189           0 :         ref_array_destroy(ra);
     190           0 :         return ENOMEM;
     191             :     }
     192             : 
     193             :     /* Initialize members here */
     194       31149 :     ic_new->ra = ra;
     195       31149 :     ic_new->state = ic->state;
     196             : 
     197       31149 :     *ic_copy = ic_new;
     198             : 
     199             :     TRACE_FLOW_EXIT();
     200       31149 :     return error;
     201             : }
     202             : 
     203             : /*
     204             :  * Modify the comment object
     205             :  */
     206       33311 : static int ini_comment_modify(struct ini_comment *ic,
     207             :                               int mode,
     208             :                               uint32_t idx,
     209             :                               const char *line,
     210             :                               uint32_t length)
     211             : {
     212       33311 :     int error = EOK;
     213       33311 :     struct simplebuffer *elem = NULL;
     214       33311 :     struct simplebuffer *empty = NULL;
     215       33311 :     char *input = NULL;
     216             : 
     217       33311 :     uint32_t i, len = 0;
     218       33311 :     uint32_t input_len = 0;
     219             : 
     220             :     TRACE_FLOW_ENTRY();
     221             : 
     222       33311 :     if (!ic) {
     223             :         TRACE_ERROR_NUMBER("Invalid comment object", EINVAL);
     224             :         return EINVAL;
     225             :     }
     226             : 
     227             : 
     228       33311 :     if (mode == INI_COMMENT_MODE_BUILD) {
     229             :         /*
     230             :          * Can use this function only if object is empty or
     231             :          * reading from the file.
     232             :          */
     233       33223 :         if ((ic->state != INI_COMMENT_EMPTY) &&
     234             :             (ic->state != INI_COMMENT_READ)) {
     235             :             TRACE_ERROR_NUMBER("Invalid use of the function", EINVAL);
     236             :             return EINVAL;
     237             :         }
     238             :     }
     239             : 
     240             :     /* Make sure that we ignore "line" in reset case */
     241       33311 :     if (mode != INI_COMMENT_MODE_CLEAR)
     242       33310 :         memcpy(&input, &line, sizeof(char *));
     243             : 
     244       33311 :     if (mode != INI_COMMENT_MODE_REMOVE) {
     245             : 
     246       33310 :         error = simplebuffer_alloc(&elem);
     247       33310 :         if (error) {
     248             :             TRACE_ERROR_NUMBER("Allocate buffer for the comment", error);
     249             :             return error;
     250             :         }
     251             : 
     252       33310 :         if (input) {
     253       33224 :             if (length == 0) input_len = strlen(input);
     254             :             else input_len = length;
     255             : 
     256       33224 :             error = simplebuffer_add_str(elem,
     257             :                                          input,
     258             :                                          input_len,
     259             :                                          INI_COMMENT_LEN);
     260             :         }
     261             :         else {
     262          86 :             error = simplebuffer_add_str(elem,
     263             :                                          "",
     264             :                                          0,
     265             :                                          INI_COMMENT_LEN);
     266             :         }
     267             : 
     268       33310 :         if (error) {
     269             :             TRACE_ERROR_NUMBER("Allocate buffer for the comment", error);
     270           0 :             simplebuffer_free(elem);
     271           0 :             return error;
     272             :         }
     273             :     }
     274             : 
     275             :     /* Do action depending on mode */
     276       33311 :     switch (mode) {
     277             :     case INI_COMMENT_MODE_BUILD:
     278             : 
     279             :         TRACE_INFO_STRING("BUILD mode", "");
     280       33223 :         error = ref_array_append(ic->ra, (void *)&elem);
     281       33223 :         if (error) {
     282             :             TRACE_ERROR_NUMBER("Failed to append line to an array", error);
     283           0 :             simplebuffer_free(elem);
     284           0 :             return error;
     285             :         }
     286             : 
     287             :         break;
     288             : 
     289             :     case INI_COMMENT_MODE_APPEND:
     290             : 
     291             :         TRACE_INFO_STRING("Append mode", "");
     292           3 :         error = ref_array_append(ic->ra, (void *)&elem);
     293           3 :         if (error) {
     294             :             TRACE_ERROR_NUMBER("Failed to append line to an array", error);
     295           0 :             simplebuffer_free(elem);
     296           0 :             return error;
     297             :         }
     298             : 
     299             :         break;
     300             : 
     301             :     case INI_COMMENT_MODE_INSERT:
     302             : 
     303             :         TRACE_INFO_STRING("Insert mode", "");
     304           2 :         len = ref_array_len(ic->ra);
     305           2 :         if (idx > len) {
     306             :             /* Fill in empty lines */
     307           0 :             for (i = 0; i < (idx-len); i++) {
     308           0 :                 error = simplebuffer_alloc(&empty);
     309           0 :                 if (error) {
     310             :                     TRACE_ERROR_NUMBER("Allocate buffer for the comment", error);
     311           0 :                     simplebuffer_free(elem);
     312           0 :                     return error;
     313             :                 }
     314           0 :                 error = simplebuffer_add_str(elem,
     315             :                                              NULL,
     316             :                                              0,
     317             :                                              INI_COMMENT_LEN);
     318           0 :                 if (error) {
     319             :                     TRACE_ERROR_NUMBER("Make comment empty", error);
     320           0 :                     simplebuffer_free(empty);
     321           0 :                     simplebuffer_free(elem);
     322           0 :                     return error;
     323             :                 }
     324           0 :                 error = ref_array_append(ic->ra, (void *)&empty);
     325           0 :                 if (error) {
     326             :                     TRACE_ERROR_NUMBER("Append problem", error);
     327           0 :                     simplebuffer_free(empty);
     328           0 :                     simplebuffer_free(elem);
     329           0 :                     return error;
     330             :                 }
     331             :             }
     332             :             /* Append last line */
     333           0 :             error = ref_array_append(ic->ra, (void *)&elem);
     334           0 :             if (error) {
     335             :                 TRACE_ERROR_NUMBER("Failed to append last line", error);
     336           0 :                 simplebuffer_free(elem);
     337           0 :                 return error;
     338             :             }
     339             :         }
     340             :         else {
     341             :             /* Insert inside the array */
     342           2 :             error = ref_array_insert(ic->ra, idx, (void *)&elem);
     343           2 :             if (error) {
     344             :                 TRACE_ERROR_NUMBER("Failed to append last line", error);
     345           0 :                 simplebuffer_free(elem);
     346           0 :                 return error;
     347             :             }
     348             : 
     349             :         }
     350             :         break;
     351             : 
     352             : 
     353             :     case INI_COMMENT_MODE_REPLACE:
     354             : 
     355             :         TRACE_INFO_STRING("Replace mode", "");
     356          81 :         error = ref_array_replace(ic->ra, idx, (void *)&elem);
     357          81 :         if (error) {
     358             :             TRACE_ERROR_NUMBER("Failed to replace", error);
     359           0 :             simplebuffer_free(elem);
     360           0 :             return error;
     361             :         }
     362             :         break;
     363             : 
     364             :     case INI_COMMENT_MODE_REMOVE:
     365             : 
     366             :         TRACE_INFO_STRING("Remove mode", "");
     367           1 :         error = ref_array_remove(ic->ra, idx);
     368           1 :         if (error) {
     369             :             TRACE_ERROR_NUMBER("Failed to remove", error);
     370             :             return error;
     371             :         }
     372             : 
     373             :         break;
     374             : 
     375             :     case INI_COMMENT_MODE_CLEAR:
     376             : 
     377             :         TRACE_INFO_STRING("Clear mode", "");
     378           1 :         error = ref_array_replace(ic->ra, idx, (void *)&elem);
     379           1 :         if (error) {
     380             :             TRACE_ERROR_NUMBER("Failed to replace", error);
     381           0 :             simplebuffer_free(elem);
     382           0 :             return error;
     383             :         }
     384             :         break;
     385             : 
     386             :     default :
     387             : 
     388             :         TRACE_ERROR_STRING("Coding error", "");
     389           0 :         simplebuffer_free(elem);
     390           0 :         return EINVAL;
     391             : 
     392             :     }
     393             : 
     394             : 
     395             :     /* Change state */
     396       33311 :     if (INI_COMMENT_MODE_BUILD) ic->state = INI_COMMENT_READ;
     397             :     else ic->state = INI_COMMENT_CHANGED;
     398             : 
     399             : 
     400             :     TRACE_FLOW_EXIT();
     401       33311 :     return error;
     402             : }
     403             : 
     404             : /*
     405             :  * Build up a comment object - use this when reading
     406             :  * comments from a file.
     407             :  */
     408         443 : int ini_comment_build(struct ini_comment *ic, const char *line)
     409             : {
     410         443 :     int error = EOK;
     411             : 
     412             :     TRACE_FLOW_ENTRY();
     413             : 
     414         443 :     error = ini_comment_modify(ic, INI_COMMENT_MODE_BUILD, 0, line, 0);
     415             : 
     416             :     TRACE_FLOW_NUMBER("ini_comment_build - Returning", error);
     417         443 :     return error;
     418             : }
     419             : 
     420             : /*
     421             :  * Build up a comment object - use this when reading
     422             :  * comments from a file.
     423             :  */
     424       32780 : int ini_comment_build_wl(struct ini_comment *ic,
     425             :                          const char *line,
     426             :                          uint32_t length)
     427             : {
     428       32780 :     int error = EOK;
     429             : 
     430             :     TRACE_FLOW_ENTRY();
     431             : 
     432       32780 :     error = ini_comment_modify(ic, INI_COMMENT_MODE_BUILD, 0, line, length);
     433             : 
     434             :     TRACE_FLOW_NUMBER("ini_comment_build - Returning", error);
     435       32780 :     return error;
     436             : }
     437             : 
     438             : /*
     439             :  * Modify comment by instering a line.
     440             :  */
     441           2 : int ini_comment_insert(struct ini_comment *ic,
     442             :                        uint32_t idx,
     443             :                        const char *line)
     444             : {
     445           2 :     int error = EOK;
     446             : 
     447             :     TRACE_FLOW_ENTRY();
     448             : 
     449           2 :     error = ini_comment_modify(ic, INI_COMMENT_MODE_INSERT, idx, line, 0);
     450             : 
     451             :     TRACE_FLOW_NUMBER("ini_comment_insert - Returning", error);
     452           2 :     return error;
     453             : }
     454             : 
     455             : /* Modify comment by appending a line. */
     456           3 : int ini_comment_append(struct ini_comment *ic, const char *line)
     457             : {
     458           3 :     int error = EOK;
     459             : 
     460             :     TRACE_FLOW_ENTRY();
     461             : 
     462           3 :     error = ini_comment_modify(ic, INI_COMMENT_MODE_APPEND, 0, line, 0);
     463             : 
     464             :     TRACE_FLOW_NUMBER("ini_comment_append - Returning", error);
     465           3 :     return error;
     466             : }
     467             : 
     468             : /* Remove line from the comment.*/
     469           1 : int ini_comment_remove(struct ini_comment *ic, uint32_t idx)
     470             : {
     471           1 :     int error = EOK;
     472             : 
     473             :     TRACE_FLOW_ENTRY();
     474             : 
     475           1 :     error = ini_comment_modify(ic, INI_COMMENT_MODE_REMOVE, idx, NULL, 0);
     476             : 
     477             :     TRACE_FLOW_NUMBER("ini_comment_remove - Returning", error);
     478           1 :     return error;
     479             : }
     480             : 
     481             : /* Clear line in the comment. Line is replaced with an empty line */
     482           1 : int ini_comment_clear(struct ini_comment *ic, uint32_t idx)
     483             : {
     484           1 :     int error = EOK;
     485             : 
     486             :     TRACE_FLOW_ENTRY();
     487             : 
     488           1 :     error = ini_comment_modify(ic, INI_COMMENT_MODE_CLEAR, idx, NULL, 0);
     489             : 
     490             :     TRACE_FLOW_NUMBER("ini_comment_clear - Returning", error);
     491           1 :     return error;
     492             : 
     493             : }
     494             : 
     495             : /* Replace a line in the comment */
     496          81 : int ini_comment_replace(struct ini_comment *ic,
     497             :                         uint32_t idx,
     498             :                         const char *line)
     499             : {
     500          81 :     int error = EOK;
     501             : 
     502             :     TRACE_FLOW_ENTRY();
     503             : 
     504          81 :     error = ini_comment_modify(ic, INI_COMMENT_MODE_REPLACE, idx, line, 0);
     505             : 
     506             :     TRACE_FLOW_NUMBER("ini_comment_replace - Returning", error);
     507          81 :     return error;
     508             : }
     509             : 
     510             : 
     511             : /* Reset the comment - clean all lines.*/
     512           0 : int ini_comment_reset(struct ini_comment *ic)
     513             : {
     514           0 :     int error = EOK;
     515             : 
     516             :     TRACE_FLOW_ENTRY();
     517             : 
     518           0 :     if (!ic) {
     519             :         TRACE_ERROR_NUMBER("Invalid comment object", EINVAL);
     520             :         return EINVAL;
     521             :     }
     522             : 
     523             :     /* Reset comment if it is not empty */
     524           0 :     if (ic->state != INI_COMMENT_EMPTY) {
     525           0 :         ref_array_reset(ic->ra);
     526           0 :         ic->state = INI_COMMENT_CHANGED;
     527             :     }
     528             : 
     529             :     TRACE_FLOW_EXIT();
     530             :     return error;
     531             : }
     532             : 
     533             : /* Get number of lines */
     534       31282 : int ini_comment_get_numlines(struct ini_comment *ic, uint32_t *num)
     535             : {
     536       31282 :     int error = EOK;
     537             : 
     538             :     TRACE_FLOW_ENTRY();
     539             : 
     540       31282 :     if ((!ic) || (!num)) {
     541             :         TRACE_ERROR_NUMBER("Invalid argument", EINVAL);
     542             :         return EINVAL;
     543             :     }
     544             : 
     545       31282 :     error = ref_array_getlen(ic->ra, num);
     546             : 
     547             :     TRACE_FLOW_NUMBER("ini_comment_get_numlines - Returning", error);
     548       31282 :     return error;
     549             : 
     550             : }
     551             : 
     552             : /* Get line */
     553       32776 : int ini_comment_get_line(struct ini_comment *ic, uint32_t idx,
     554             :                          char **line, uint32_t *line_len)
     555             : {
     556       32776 :     int error = EOK;
     557       32776 :     void *res = NULL;
     558       32776 :     struct simplebuffer *sb = NULL;
     559             :     const unsigned char *ln;
     560             : 
     561             :     TRACE_FLOW_ENTRY();
     562             : 
     563       32776 :     if ((!ic) || (!line)) {
     564             :         TRACE_ERROR_NUMBER("Invalid argument", EINVAL);
     565             :         return EINVAL;
     566             :     }
     567             : 
     568       32776 :     res = ref_array_get(ic->ra, idx, (void *)&sb);
     569       32776 :     if (!res) {
     570           0 :         error = EINVAL;
     571           0 :         *line = NULL;
     572             :         if (line_len) line_len = 0;
     573             :     }
     574             :     else {
     575       32776 :         ln = simplebuffer_get_buf(sb);
     576       32776 :         memcpy(line, &ln, sizeof(char *));
     577       32776 :         if (line_len) *line_len = simplebuffer_get_len(sb);
     578             :     }
     579             : 
     580             :     TRACE_FLOW_NUMBER("ini_comment_get_line - Returning", error);
     581       32776 :     return error;
     582             : }
     583             : 
     584             : /* Swap lines */
     585           3 : int ini_comment_swap(struct ini_comment *ic,
     586             :                      uint32_t idx1,
     587             :                      uint32_t idx2)
     588             : {
     589           3 :     int error = EOK;
     590             : 
     591             :     TRACE_FLOW_ENTRY();
     592             : 
     593           3 :     if (!ic) {
     594             :         TRACE_ERROR_NUMBER("Invalid argument", EINVAL);
     595             :         return EINVAL;
     596             :     }
     597             : 
     598           3 :     if (idx1 != idx2) {
     599           3 :         if ((error = ref_array_swap(ic->ra, idx1, idx2))) {
     600             :             TRACE_ERROR_NUMBER("Failed to swap", error);
     601             :             return error;
     602             :         }
     603           3 :         ic->state = INI_COMMENT_CHANGED;
     604             :     }
     605             : 
     606             :     TRACE_FLOW_EXIT();
     607           3 :     return error;
     608             : }
     609             : 
     610             : /* Add one comment to another */
     611          19 : int ini_comment_add(struct ini_comment *ic_to_add,
     612             :                     struct ini_comment *ic)
     613             : {
     614          19 :     int error = EOK;
     615          19 :     struct simplebuffer *sb = NULL;
     616          19 :     struct simplebuffer *sb_new = NULL;
     617          19 :     void *res = NULL;
     618          19 :     uint32_t len = 0;
     619             :     int i;
     620             : 
     621             :     TRACE_FLOW_ENTRY();
     622             : 
     623          19 :     len = ref_array_len(ic_to_add->ra);
     624             : 
     625          61 :     for (i = 0; i< len; i++) {
     626             : 
     627             :         /* Get data element */
     628          42 :         res = ref_array_get(ic_to_add->ra, i, (void *)(&sb));
     629          42 :         if (!res) {
     630             :             TRACE_ERROR_NUMBER("Failed to get comment element", error);
     631             :             return error;
     632             :         }
     633             : 
     634             :         /* Create a storage a for a copy */
     635          42 :         error = simplebuffer_alloc(&sb_new);
     636          42 :         if (error) {
     637             :             TRACE_ERROR_NUMBER("Allocate buffer for the comment", error);
     638             :             return error;
     639             :         }
     640             : 
     641             :         /* Copy actual data */
     642          84 :         error = simplebuffer_add_str(sb_new,
     643          42 :                                      (const char *)simplebuffer_get_buf(sb),
     644             :                                      simplebuffer_get_len(sb),
     645             :                                      INI_COMMENT_LEN);
     646          42 :         if (error) {
     647             :             TRACE_ERROR_NUMBER("Failed to append line to an array", error);
     648           0 :             simplebuffer_free(sb_new);
     649           0 :             return error;
     650             :         }
     651             : 
     652             :         /* Append it to the array */
     653          42 :         error = ref_array_append(ic->ra, (void *)&sb_new);
     654          42 :         if (error) {
     655             :             TRACE_ERROR_NUMBER("Failed to append element to an array", error);
     656           0 :             simplebuffer_free(sb_new);
     657           0 :             return error;
     658             :         }
     659             :     }
     660             : 
     661             :     TRACE_FLOW_EXIT();
     662             :     return error;
     663             : }
     664             : 
     665             : /* Serialize comment */
     666       31278 : int ini_comment_serialize (struct ini_comment *ic,
     667             :                            struct simplebuffer *sbobj)
     668             : {
     669       31278 :     int error = EOK;
     670       31278 :     uint32_t num = 0;
     671       31278 :     uint32_t i = 0;
     672       31278 :     uint32_t len = 0;
     673       31278 :     char *commentline = NULL;
     674             : 
     675             :     TRACE_FLOW_ENTRY();
     676             : 
     677             :     /* Get number of lines in the comment */
     678       31278 :     error = ini_comment_get_numlines(ic, &num);
     679       31278 :     if (error) {
     680             :         TRACE_ERROR_NUMBER("Failed to get number of lines", error);
     681             :         return error;
     682             :     }
     683             : 
     684       32744 :     for (i = 0; i < num; i++) {
     685             : 
     686       32744 :         len = 0;
     687       32744 :         commentline = NULL;
     688             : 
     689       32744 :         error = ini_comment_get_line(ic, i, &commentline, &len);
     690       32744 :         if (error) {
     691             :             TRACE_ERROR_NUMBER("Failed to get line", error);
     692             :             return error;
     693             :         }
     694             : 
     695       32744 :         error = simplebuffer_add_raw(sbobj,
     696             :                                      commentline,
     697             :                                      len,
     698             :                                      INI_VALUE_BLOCK);
     699       32744 :         if (error) {
     700             :             TRACE_ERROR_NUMBER("Failed to add comment", error);
     701             :             return error;
     702             :         }
     703             : 
     704       32744 :         error = simplebuffer_add_cr(sbobj);
     705       32744 :         if (error) {
     706             :             TRACE_ERROR_NUMBER("Failed to add CR", error);
     707             :             return error;
     708             :         }
     709             :     }
     710             : 
     711             :     TRACE_FLOW_EXIT();
     712             :     return error;
     713             : }
     714             : 
     715             : /* Internal function to print comment */
     716           1 : void ini_comment_print(struct ini_comment *ic, FILE *file)
     717             : {
     718             :     int len;
     719             :     int i;
     720           1 :     struct simplebuffer *sb = NULL;
     721             : 
     722             :     TRACE_FLOW_ENTRY();
     723             : 
     724           1 :     if (!file) {
     725             :         TRACE_ERROR_NUMBER("Invalid file argument", EINVAL);
     726           0 :         return;
     727             :     }
     728             : 
     729           1 :     if (ic) {
     730           1 :         len = ref_array_len(ic->ra);
     731           6 :         for (i = 0; i < len; i++) {
     732           5 :             ref_array_get(ic->ra, i, (void *)(&sb));
     733           5 :             fprintf(file, "%s\n", simplebuffer_get_buf(sb));
     734             :         }
     735             :     }
     736             : 
     737             :     TRACE_FLOW_EXIT();
     738             : }

Generated by: LCOV version 1.10