-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyString.h
executable file
·80 lines (64 loc) · 1.97 KB
/
myString.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
#pragma once // Direttiva di ottimizzazione per #ifndef _MYSTRING_H ...
#ifndef _MYSTRING_H
#define _MYSTRING_H
#include <string.h>
// Definizione del Tipo STRING
typedef char* STRING;
// Definizione delle Operazioni possibili su STRING
STRING strCreate (void);
STRING strCreate (char* init);
void strAssign (STRING* dest, STRING newVal);
int strLength (STRING str);
void strAppend (STRING* dest, STRING toAppend);
void strDelete (STRING* str);
int strCompare (STRING str1, STRING str2);
/****************************************************************************
* IMPLEMENTAZIONE *
* (da spostare in un file c o cpp separato) *
****************************************************************************/
STRING strCreate (void){
STRING newString;
newString = NULL;
return newString;
}
STRING strCreate (STRING init){
if (init == NULL) return strCreate();
int len = strLength(init);
char* temp = (char*) malloc (sizeof(char) * (len + 1));
strcpy(temp, init);
STRING newString;
newString = temp;
return newString;
}
void strAssign (STRING* dest, STRING newVal){
// Realizzazione alternativa...
if (*dest != NULL) strDelete(dest);
*dest = strCreate(newVal);
}
int strLength (STRING str){
if (str == NULL) return -1;
else return strlen(str);
}
void strAppend (STRING* dest, STRING toAppend){
int lenD = strLength(*dest);
int lenS = strLength(toAppend);
if (lenS < 0) return;
if (lenD < 0) { strAssign(dest, toAppend); return; }
char* temp = (char*) malloc((lenD + lenS + 1) * sizeof(char));
strcpy(temp, *dest);
strDelete(dest);
strcat(temp, toAppend);
STRING tmpStr; tmpStr = temp;
strAssign(dest, tmpStr);
}
int strCompare (STRING str1, STRING str2){
return strcmp(str1, str2);
}
void strDelete (STRING* str){
if (*str!= NULL) {
free(*str);
*str = NULL;
}
}
//****************************************************************************
#endif /*_MYSTRING_H*/