[go: up one dir, main page]

File: text_list.c

package info (click to toggle)
cctools 9.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 44,624 kB
  • sloc: ansic: 192,539; python: 20,827; cpp: 20,199; sh: 11,719; perl: 4,106; xml: 3,688; makefile: 1,224
file content (103 lines) | stat: -rw-r--r-- 1,813 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "stringtools.h"
#include "text_list.h"

struct text_list {
	char **items;
	int alloc_length;
	int used_length;
};

struct text_list *text_list_create()
{
	struct text_list *t = malloc(sizeof(*t));
	t->alloc_length = 8;
	t->used_length = 0;
	t->items = malloc(sizeof(*t->items) * t->alloc_length);
	return t;
}

struct text_list *text_list_load(const char *path)
{
	char line[1024];

	FILE *file = fopen(path, "r");
	if(!file)
		return 0;

	struct text_list *t = text_list_create();

	while(fgets(line, sizeof(line), file)) {
		string_chomp(line);
		text_list_append(t, line);
	}

	fclose(file);

	return t;
}

struct text_list *text_list_load_str(const char *inp_str)
{
	if (!inp_str) {
		return NULL;
	}

	struct text_list *t = text_list_create();
	char *pch = NULL;
	char * tmp_str = strdup(inp_str);
	pch = strtok(tmp_str, ",");
	while(pch != NULL) {
		text_list_append(t, pch);
		pch = strtok(NULL, ",");
	}	
	free(tmp_str);
	return t;
}

char *text_list_get(struct text_list *t, int i)
{
	if(i >= 0 && i < t->used_length) {
		return t->items[i];
	} else {
		return 0;
	}
}

int text_list_append(struct text_list *t, const char *str)
{
	if(t->used_length == t->alloc_length) {
		t->alloc_length *= 2;
		t->items = realloc(t->items, sizeof(*t->items) * t->alloc_length);
	}

	t->items[t->used_length] = strdup(str);
	return t->used_length++;
}

int text_list_size(struct text_list *t)
{
	return t->used_length;
}

void text_list_delete(struct text_list *t)
{
	int i;
	for(i = 0; i < t->used_length; i++) {
		free(t->items[i]);
	}
	free(t->items);
	free(t);
}

void text_list_set(struct text_list *t, const char *str, int i)
{
	char *tmp_str = strdup(str);
	free(t->items[i]);
	t->items[i] = tmp_str;
}

/* vim: set noexpandtab tabstop=4: */