mirror of
https://github.com/johnkerl/miller.git
synced 2026-07-20 18:10:07 +00:00
move string_array from containers/ to lib/
This commit is contained in:
parent
01de701cf9
commit
1ac3fa1a15
13 changed files with 12 additions and 12 deletions
51
c/lib/string_array.c
Normal file
51
c/lib/string_array.c
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#include <stdlib.h>
|
||||
#include "lib/mlrutil.h"
|
||||
#include "string_array.h"
|
||||
|
||||
string_array_t* string_array_alloc(int length) {
|
||||
string_array_t* parray = mlr_malloc_or_die(sizeof(string_array_t));
|
||||
parray->length = length;
|
||||
parray->strings_need_freeing = FALSE;
|
||||
parray->strings = mlr_malloc_or_die(length * sizeof(char**));
|
||||
for (int i = 0; i < length; i++)
|
||||
parray->strings[i] = NULL;
|
||||
return parray;
|
||||
}
|
||||
|
||||
void string_array_free(string_array_t* parray) {
|
||||
if (parray == NULL)
|
||||
return;
|
||||
if (parray->strings_need_freeing) {
|
||||
for (int i = 0; i < parray->length; i++)
|
||||
free(parray->strings[i]);
|
||||
}
|
||||
free(parray->strings);
|
||||
free(parray);
|
||||
}
|
||||
|
||||
string_array_t* string_array_from_line(char* line, char ifs) {
|
||||
if (*line == 0) // empty string splits to empty array
|
||||
return string_array_alloc(0);
|
||||
|
||||
int num_commas = 0;
|
||||
|
||||
for (char* p = line; *p; p++)
|
||||
if (*p == ifs)
|
||||
num_commas++;
|
||||
|
||||
string_array_t* parray = string_array_alloc(num_commas + 1);
|
||||
|
||||
char* start = line;
|
||||
int i = 0;
|
||||
for (char* p = line; *p; p++) {
|
||||
if (*p == ifs) {
|
||||
*p = 0;
|
||||
p++;
|
||||
parray->strings[i++] = start;
|
||||
start = p;
|
||||
}
|
||||
}
|
||||
parray->strings[i++] = start;
|
||||
|
||||
return parray;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue