-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdstring.h
159 lines (126 loc) · 3.99 KB
/
dstring.h
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/******************************************************************************
* IFJ21
* dstring.h
*
* Authors: Vojtech Dvorak (xdvora3o)
* Purpose: Declaration of basic functions to process (dynamic) string
* Inspired by 'str' module from example IFJ project
*
* Last change: 8. 12. 2021
*****************************************************************************/
/**
* @file dstring.h
* @brief Declaration of basic functions to process (dynamic) string
*
* @authors Vojtech Dvorak (xdvora3o)
*/
#ifndef DSTRING_H
#define DSTRING_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#define STR_INIT_SPACE 8 /**< Initial allocated size for string */
//Return codes of string processing functions
#define STR_SUCCESS 0
#define STR_FAILURE 1
//Dnamic string is implemented as dynamic array
typedef struct string {
size_t length; /**< Length of string */
size_t alloc_size; /**< Allocated space for string */
char * str; /**< String data */
} string_t;
//Character classes
enum char_type {
ALPHA, DIGIT, WHITESPACE, CONTROL, OTHER_CHARACTER
};
/**
* @brief Initializes string_t structure
* @param string dynamic string to initialize
* @return STR_SUCCESS if everything goes well
*/
int str_init(string_t *string);
int extend_string(string_t *string);
/**
* @brief Appends character to end of string
* @param c character to append
* @param string target string
* @return STR_SUCCESS if everything goes well
*/
int app_char(char c, string_t *string);
/**
* @brief Appends cstring after dynamic string
*/
int app_str(string_t *dst, const char *src);
/**
* @brief Puts chracter c at the start of the dynimic string
*/
int prep_char(char c, string_t *string);
/**
* @brief Puts cstring at the start of the dynimic string
*/
int prep_str(string_t *dst, const char *src);
/**
* @brief Cuts string to given length
* @note if given length is same or greater than current length of string it does nothing
*/
void cut_string(string_t *string, size_t new_length);
/**
* @brief Brings string to the state after initialization
* @note Allocated space doesn't change!
* @param string target string
*/
void str_clear(string_t *string);
/**
* @brief Destroys string and frees all resources
*/
void str_dtor(string_t *string);
/**
* @brief Returns pointer to array of characters
*/
char * to_str(string_t *string);
/**
* @brief Tries to find character in string
* @return true if character was found
*/
bool str_search(const char c, const char *str);
/**
* @brief Copies array of characters to another place in memory
* @param dst addres of pointer to target destination (IT SHOULD BE NULL!)
* @param src source array of characters
* @param length length of final string located at dst pointer
* @return STR_SUCCESS if everything goes well
*/
int str_cpy(char **dst, const char *src, size_t length);
/**
* @brief Copies array of characters (cstr) to string structure
* @param length length of source cstr from start that will be copied to string
*/
int str_cpy_tostring(string_t* dst, const char *src, size_t length);
/**
* @brief Makes hard copy of two strings
* @param zero_term says if src string is terminated by '\0' character
*/
int cpy_strings(string_t* dst, string_t *src, bool zero_term);
/**
* @brief returns type of character (character class, @see char_type)
*/
int get_chtype(const char c);
/**
* @brief Compares to strings and returns result as an integer (same as strcmp)
* @return 0 if strings are same
*/
int str_cmp(const char *str1, const char *str2);
/**
* @brief Returns length attribute of string_t structure
* @return length of string
*/
size_t len(string_t *str);
/**
* @brief Compares two dynamic strings
* @return 0 if strings are equivalent
*/
int dstring_cmp(string_t* str1, string_t* str2);
#endif
/*** End of dstring.h ***/