forked from andrea-rosasco/shell_bush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
str.c
59 lines (52 loc) · 1.25 KB
/
str.c
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
/*
* bush--
*
* Programma sviluppato a supporto del laboratorio di
* Sistemi di Elaborazione e Trasmissione del corso di laurea
* in Informatica classe L-31 presso l'Universita` degli Studi di
* Genova, anno accademico 2016/2017.
*
* Copyright (C) 2015,2016 by Giovanni Lagorio <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdlib.h>
#include <string.h>
#include "utils.h"
#include "str.h"
struct str {
size_t len;
char *chars;
};
struct str *str_new()
{
struct str *s = my_malloc(sizeof(*s));
s->len = 0;
s->chars = my_malloc(1);
*(s->chars) = '\0';
return s;
}
void str_destroy(struct str *this)
{
free(this->chars);
free(this);
}
char *str_destroy_stealing_chars(struct str *this)
{
char *chars = this->chars;
free(this);
return chars;
}
void str_append(struct str *this, const char *chars)
{
size_t chars_len;
if (!chars)
return;
chars_len = strlen(chars);
this->chars = my_realloc(this->chars, this->len + chars_len + 1);
strcpy(this->chars + this->len, chars);
this->len += chars_len;
}