-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmystring.c
517 lines (517 loc) · 10.4 KB
/
mystring.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#include <stdio.h>
#include <stdlib.h>
#include "mystring.h"
#include <ctype.h>
#include <string.h>
struct mystring
{
int size;
int capacity;
char* data;
};
typedef struct mystring Mystring;
void my_swap(char* a, char* b);
MYSTRING mystring_init_default(void)
{
Mystring* temp = (Mystring*) malloc(sizeof(Mystring));
if(temp != NULL)
{
temp->size = 0;
temp->capacity = MYSTRING_STARTING_CAPACITY;
temp->data = (char*) malloc(sizeof(char) * MYSTRING_STARTING_CAPACITY);
if(temp->data == NULL)
{
free(temp);
temp = NULL;
}
}
return (MYSTRING) temp;
}
MYSTRING mystring_init_c_string(const char * const c_string)
{
int i;
int size = 0;
Mystring* def = (Mystring*) malloc(sizeof(Mystring));
if(def != NULL && c_string != NULL)
{
for(i = 0; c_string[i] != '\0'; i++)
{
size++;
}
def->data = (char*) malloc(sizeof(char) * size);
if(def->data == NULL)
{
free(def);
return NULL;
}
for(i = 0; i < size; i++)
{
if(c_string[i] != '\0')
{
def->data[i] = c_string[i];
}
}
def->size = size;
def->capacity = size;
}
return (MYSTRING) def;
}
int mystring_size(MYSTRING hString)
{
Mystring* string = (Mystring*) hString;
if(string == NULL)
{
return -1;
}
return string->size ;
}
int mystring_capacity(MYSTRING hString)
{
Mystring* string = (Mystring*) hString;
if(string == NULL)
{
return -1;
}
return string->capacity;
}
MyString_Status mystring_output(MYSTRING hString, FILE* out)
{
Mystring* string = (Mystring*) hString;
int i;
if(string == NULL || out == NULL)
{
return MYSTRING_STATUS_ERROR;
}
for(i = 0; i < string->size;i++)
{
fprintf(out,"%c",string->data[i]);
}
fprintf(out,"\n");
return MYSTRING_STATUS_SUCCESS;
}
MyString_Status mystring_concatenate_c_string(MYSTRING hString, char * c_string)
{
Mystring* string = (Mystring*) hString;
int i ;
int size = 0;
char* data;
if(string == NULL || c_string == NULL)
{
return MYSTRING_STATUS_ERROR;
}
for(i = 0; c_string[i] != '\0';i++)
{
size++;
}
if(string->capacity >= string->size + size)
{
for(i = string->size ; i < string->size + size; i++)
{
string->data[i] = c_string[i - string->size ];
}
string->size += size;
}
else
{
data = (char*) malloc(sizeof(char) * (string->size + size) );
if(data == NULL)
{
return MYSTRING_STATUS_ERROR;
}
for(i = 0; i < string->size; i++)
{
data[i] = string->data[i];
}
for(i = string->size ; i < string->size + size; i++)
{
data[i] = c_string[i - string->size];
}
free(string->data);
string->data = data;
string->size += size;
string->capacity = string->size;
}
return MYSTRING_STATUS_SUCCESS;
}
MyString_Status mystring_concatenate_mystring(MYSTRING hStringDest,
MYSTRING hStringSource)
{
Mystring* dest = (Mystring*) hStringDest;
Mystring* source = (Mystring*) hStringSource;
int i;
if(dest == NULL || source == NULL)
{
return MYSTRING_STATUS_ERROR;
}
if(dest->capacity >= source->size + dest->size)
{
for(i = dest->size; i < dest->size + source->size;i++)
{
dest->data[i] = source->data[i-dest->size];
}
dest->size +=source->size;
}
else
{
char* new = (char*) malloc(sizeof(char) * dest->size + source->size);
if(new == NULL)
{
return MYSTRING_STATUS_ERROR;
}
for(i = 0; i < dest->size ; i++)
{
new[i] = dest->data[i];
}
for(i = dest->size; i < (dest->size + source->size); i++)
{
new[i] = source->data[i-dest->size];
}
free(dest->data);
dest->data = new;
dest->size += source->size;
dest->capacity = dest->size;
}
return MYSTRING_STATUS_SUCCESS;
}
MyString_Status mystring_truncate(MYSTRING hString, int newMaxLen)
{
Mystring* string = (Mystring*) hString;
if(string == NULL || newMaxLen > string->size || newMaxLen < 0)
{
return MYSTRING_STATUS_ERROR;
}
string->size = newMaxLen;
return MYSTRING_STATUS_SUCCESS;
}
void mystring_destroy(MYSTRING* p_hString)
{
if(p_hString != NULL)
{
Mystring* string = (Mystring*) *p_hString;
if(string != NULL)
{
//printf("Address of data, %d\n",string->data);
if(string->data != NULL)
{
free(string->data);
}
free(string);
}
*p_hString = NULL;
}
}
MyString_Status mystring_push(MYSTRING hString, char ch)
{
Mystring* string = (Mystring*) hString;
if(string == NULL)
{
return MYSTRING_STATUS_ERROR;
}
char* new;
int i;
int newsize = string->size + 1;
if(newsize <= string->capacity)
{
string->data[string->size] = ch;
string->size++;
}
else
{
new = (char*) malloc(sizeof(char) * newsize);
if(new == NULL)
{
return MYSTRING_STATUS_ERROR;
}
for(i = 0; i < string->size;i++)
{
new[i] = string->data[i];
}
new[string->size] = ch;
free(string->data);
string->data = new;
string->size++;
string->capacity = string->size;
}
return MYSTRING_STATUS_SUCCESS;
}
char mystring_pop(MYSTRING hString)
{
Mystring* string = (Mystring*) hString;
int size = string->size;
char c;
if(size > 0 && string != NULL)
{
c = string->data[string->size-1];
string->size--;
return c;
}
return '\0';
}
char mystring_peek(MYSTRING hString)
{
Mystring* string = (Mystring*) hString;
if(string->size > 0 && string != NULL)
{
return string->data[string->size - 1];
}
return '\0';
}
char mystring_get(MYSTRING hString, int index)
{
Mystring* string = (Mystring*) hString;
if(string != NULL && index + 1 <= string->size && index >= 0)
{
return string->data[index];
}
return '\0';
}
MyString_Status mystring_put(MYSTRING hString, int index, char ch)
{
Mystring* string = (Mystring*) hString;
if(string != NULL && index + 1 <= string->size && index >= 0)
{
string->data[index] = ch;
return MYSTRING_STATUS_SUCCESS;
}
return MYSTRING_STATUS_ERROR;
}
MYSTRING mystring_init_substring(MYSTRING hStringSource, int index, int length)
{
Mystring* string = (Mystring*) hStringSource;
int i;
char* new;
MYSTRING ms;
if(hStringSource == NULL || index < 0 || length <= 0 || length > string->size || index > string->size - 1)
{
return NULL;
}
new = (char*) malloc((sizeof(char) * length) + 1);
if(new == NULL)
{
return NULL;
}
for(i = index; i < length + index;i++)
{
new[i - index] = string->data[i];
}
new[length] = '\0';
ms = mystring_init_c_string(new);
free(new);
return ms;
}
char * mystring_to_c_string(MYSTRING hString, char c_string_arr[], int arrSize)
{ Mystring* string = (Mystring*) hString;
int i;
if(string == NULL || c_string_arr == NULL || arrSize < 1)
{
return NULL;
}
for(i = 0; i < string->size && i < arrSize - 1; i++)
{
c_string_arr[i] = string->data[i];
}
c_string_arr[i] = '\0';
return c_string_arr;
}
MyString_Status mystring_input(MYSTRING hString, FILE* hFile,int bIgnoreLeadingWhiteSpace, int (* fTerminate)(char ch, int * pbDiscardChar))
{
Mystring* string = (Mystring*) hString;
char c;
int discard = 0;
int realchar = 0;
if(hFile == NULL || string == NULL || fTerminate == NULL)
{
return MYSTRING_STATUS_ERROR;
}
string->size = 0;
while(fscanf(hFile,"%c",&c) != EOF)
{
if(bIgnoreLeadingWhiteSpace)
{
if(!realchar && !isspace(c) && !isblank(c))
{
realchar = 1;
}
if(realchar)
{
if(fTerminate(c,&discard))
{
break;
}
if(discard == 0)
{
mystring_push(hString,c);
}
}
}
else
{
if(fTerminate(c,&discard))
{
break;
}
if(discard == 0)
{
mystring_push(hString,c);
}
}
}
return MYSTRING_STATUS_SUCCESS;
}
void string_destructor(void** object)
{
mystring_destroy((MYSTRING*)object);
}
void string_assignment(void** left, void* right)
{
char* temp;
int i;
Mystring* real_left = (Mystring*) *left;
Mystring* real_right = (Mystring*) right;
if(real_left == NULL)
{
*left = real_left = (Mystring*) mystring_init_default();
}
if(real_left != NULL && real_right != NULL)
{
//both objects "supposedly" exist so try to do a deep copy
//if the left side is not big enough to store the right's data then
//make it bigger.
if(real_left->capacity < real_right->size)
{
real_left->capacity = real_right->size;
temp = (char*)malloc(sizeof(char)*real_left->capacity);
if(temp == NULL)
{
fprintf(stderr, "I ran out of memory and I give up...\n");
exit(1);
}
free(real_left->data);
real_left->data = temp;
}
//fix left's size to match right's
real_left->size = real_right->size;
//copy over all the data
for(i=0; i<real_right->size; i++)
{
real_left->data[i] = real_right->data[i];
}
}
else
{
//if right is NULL we can't really do the right thing so let's just crash for now
fprintf(stderr, "Tried to do string_assignment but the right hand side was NULL\n");
exit(1);
}
}
int mystring_contains(MYSTRING hString, char c, int index)
{
Mystring* string = (Mystring*) hString;
if(string != NULL)
{
if(tolower(string->data[index]) == tolower(c))
{
return 1;
}
}
return 0;
}
int mystring_contains_at_all(MYSTRING hString, char c)
{
int i;
Mystring* string = (Mystring*) hString;
if(string != NULL)
{
for(i = 0; i < mystring_size(hString); i++)
{
if(tolower(string->data[i]) == tolower(c))
{
return 1;
}
}
}
return 0;
}
void mystring_sort(MYSTRING hString)
{
int i,j;
int min;
Mystring* string = (Mystring*) hString;
for(i = 0; i < string->size - 1; i++)
{
min = i;//selection sort
for(j = i + 1; j < string->size; j++ )
{
if(string->data[j] < string->data[min])
{
min = j;
}
}
if(min != i)
{
my_swap(&string->data[i],&string->data[min]);
}
}
return;
}
void my_swap(char* a, char* b)
{
char temp;
temp = *a;
*a = *b;
*b = temp;
return;
}
int mystring_compare(MYSTRING left, MYSTRING right)
{
int i;
Mystring* real_left = (Mystring*) left;
Mystring* real_right = (Mystring*) right;
if(left == NULL && right != NULL)
{
return 1;
}
if(left != NULL && right == NULL)
{
return -1;
}
if(left == NULL && right == NULL)
{
return 0;
}
if(real_left->size == real_right->size)
{
for(i = 0; i < real_left->size; i++)
{
if(real_left->data[i] != real_right->data[i])
{
//return real_left->data[i] - real_right->data[i];
return real_right->data[i] - real_left->data[i];
}
}
return 0;
}
else if(real_left->size > real_right->size)
{
for(i = 0; i < real_right->size; i++)
{
if(real_left->data[i] != real_right->data[i])
{
return real_right->data[i] - real_left->data[i];
}
}
return 1;
}
else if(real_left->size < real_right->size)
{
for(i = 0; i < real_left->size; i++)
{
if(real_left->data[i] != real_right->data[i])
{
return real_right->data[i] - real_left->data[i];
}
}
return -1;
}
return 0;
}