-
Notifications
You must be signed in to change notification settings - Fork 0
/
str.cpp
84 lines (68 loc) · 1.51 KB
/
str.cpp
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
//
// Created by toxad on 01.11.2016.
//
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "str.h"
unsigned int strLength( string *s )
{
return s->size;
}
string* strCreate()
{
string *str = ( string * )malloc( sizeof( string ) );
str->size = STR_INIT_SIZE;
str->str = ( char * )calloc( str->size, sizeof( char ) );
return str;
}
string** strMasCreate( int size )
{
string **s = ( string ** )malloc( size * sizeof( string * ) );
for (int i = 0; i < size; i++ )
s[i] = strCreate();
return s;
}
void strDestroy( string* strDelete )
{
free( strDelete->str );
free( strDelete );
}
void strScan( string* s, FILE *in )
{
if (strLength(s) > 0) {
free(s->str);
s->size = STR_INIT_SIZE;
s->str = (char*) calloc(s->size, sizeof(char));
}
char c;
unsigned int length = 0;
while ((c = getc( in )) != '\n') {
if (length + 1 > s->size) {
s->size += 100;
s->str = (char*) realloc(s->str, s->size);
}
s->str[length] = c;
length++;
}
s->str[length] = '\0';
}
void strPrint( string* s)
{
printf("%s\n", s->str );
}
void strCopy ( string* s, string* p )
{
if (s->size != p->size){
p->size = s->size;
p->str = ( char *)realloc( p->str, s->size );
}
strcpy(p->str, s->str);
}
void strMasDestroy( string** strDelete, int size )
{
for (int i = 0; i < size; i++ )
strDestroy( strDelete[i] );
free( strDelete );
}