summaryrefslogtreecommitdiff
path: root/src/strarray.c
blob: 12ddbf86923eb203ca7f90c81892dca74bcea46d (plain)
    1 // GPGEdit edits GPG encrypted files
    2 // Copyright (C) 2018  Aaron Ball <nullspoon@oper.io>
    3 //
    4 // This program 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 // This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
   16 #include "strarray.h"
   17 
   18 
   19 // strarray_new:
   20 // Pretty much useless constructor. Here mostly for consistency. This can
   21 // easily be replaced with your own malloc statement provided index 0 is set to
   22 // NULL.
   23 //
   24 // NOTE: If not using this, initialized memory *must* be allocated in the heap,
   25 //       as growing the string array requires reallocating the memory, which
   26 //       can't be done in the stack.
   27 char** strarray_new() {
   28   char** out = malloc(sizeof(char*));
   29   out[0] = NULL;
   30   return out;
   31 }
   32 
   33 
   34 // strarray_add:
   35 // Dynamically grows a string array (pointer to char array containing pointers
   36 // to char arrays - sorry). Reallocates storage for the original array plus
   37 // room for one additional pointer. Once data is copied in, the source array
   38 // pointer is updated to point to the new storage.
   39 //
   40 // @strorig Source string array pointer
   41 // @newstr  New string to add to the string array.
   42 void strarray_add(char*** strorig, char* newstr) {
   43   int i = 0;
   44   char** newstrarr;
   45 
   46   // Count the number of existing strings
   47   while(*strorig[i] != NULL)
   48     i++;
   49 
   50   // Reallocate old array + 1
   51   newstrarr = (char**) realloc(*strorig, sizeof(char*) * (i + 1));
   52 
   53   // Append new string to the array
   54   newstrarr[i] = malloc(strlen(newstr) + 1);
   55   strcpy(newstrarr[i], newstr);
   56   // Don't forget the null terminator!
   57   newstrarr[i + 1] = NULL;
   58 
   59   // Repoint strorig to newstrarr memory
   60   *strorig = newstrarr;
   61 }
   62 
   63 
   64 // strarray_release:
   65 // Destructor for a string array. Frees each item in the array before freeing
   66 // the array itself.
   67 //
   68 // @strarray Array to free
   69 void strarray_release(char*** strarray) {
   70   int i = 0;
   71 
   72   while(*strarray[i] != NULL) {
   73     free(*strarray[i]);
   74     i++;
   75   }
   76   free(*strarray);
   77 }

Generated by cgit