Welcome to mirror list, hosted at ThFree Co, Russian Federation.

_String.cpp « Shared « Src - github.com/moses-smt/salm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 153a9806da593102355d7031f310d444eab77b68 (plain)
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
/**
* _String.cpp: implementation of the C_String class.
* Revision $Rev: 3794 $
* Last Modified $LastChangedDate: 2007-06-29 02:17:32 -0400 (Fri, 29 Jun 2007) $
**/

#include "_String.h"
#include "string.h"
#include "stdio.h"
#include <stdlib.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

C_String::C_String()
{
	this->content = (char *) malloc(sizeof(char));
	this->content[0]='\0';
	this->hasContent = true;
}

void C_String::freeContent()
{
	if(this->hasContent){
		this->hasContent = false;
		free(this->content);
	}
}

C_String::~C_String()
{
	this->freeContent();
}

/**
* Copy constructor from a char string
**/
C_String::C_String(char * str1)
{

	this->content = (char *) malloc(sizeof(char)*strlen(str1)+1);
	if(this->content==NULL){
		fprintf(stderr,"Memory allocation error, Quit.\n");
	}

	strcpy(this->content, str1);

	this->hasContent = true;
}


C_String::C_String(C_String const &strObj1)
{
	this->hasContent = false;
	copy(strObj1);
}

C_String::C_String(const C_String & obj1, const C_String & obj2)
{
	this->freeContent();

	int len1 = strlen(obj1.content);
	int len2 = strlen(obj2.content);

	int fullLen = len1+len2;
	this->content = (char *)  malloc(sizeof(char)*len1 + sizeof(char)*len2 + 1);

	if(this->content==NULL){
		fprintf(stderr,"Memory allocation error, Quit.\n");
	}

	char * pointer = (char*) this->content; 
	strcpy(pointer, obj1.content);	//copy first part
	pointer += len1;
	strcpy(pointer, obj2.content);	//copy second part
	
	this->content[fullLen]='\0';

	this->hasContent = true;
}

void C_String::operator=(const C_String &strObj2)
{
	copy(strObj2);
}

void C_String::copy(const C_String &strObj)
{
	this->freeContent();

	this->content = (char *)  malloc(sizeof(char)*strlen(strObj.content)+1);
	if(this->content==NULL){
		fprintf(stderr,"Memory allocation error, Quit.\n");
	}

	strcpy(this->content, strObj.content);
	this->hasContent = true;
}

void C_String::copy(const C_String &strObj, int copyLen)
{
	this->freeContent();

	this->content = (char *)  malloc(sizeof(char)*(copyLen+1) );
	if(this->content==NULL){
		fprintf(stderr,"Memory allocation error, Quit.\n");
	}
	
	for(int i=0;i<copyLen;i++){
		this->content[i]=strObj.getCharAtPos(i);
	}

	this->content[copyLen]='\0';

	this->hasContent = true;
	
}

void C_String::print2stream(FILE *stream)
{
	fprintf(stream, content);
}


int C_String::length() const
{
	if(this->hasContent){
		return strlen(this->content);
	}

	return 0;
}

bool C_String::operator==(const C_String &obj1) const
{
	if(strcmp(this->content, obj1.content)==0){
		return true;
	};

	return false;
}

bool C_String::operator!=(const C_String &obj1) const
{
	if(strcmp(this->content, obj1.content)!=0){
		return true;
	};

	return false;
}

bool C_String::operator<(const C_String &obj1) const
{
	if(strcmp(this->content, obj1.content)<0){
		return true;
	};

	return false;
}

char * C_String::toString() const
{
	return this->content;
}

void C_String::clear()
{
	this->freeContent();

	this->content = (char *) malloc(sizeof(char));
	this->content[0]='\0';
	this->hasContent = true;
}


char C_String::getCharAtPos(int pos) const
{
	if(pos>=this->length()){
		fprintf(stderr,"Can not get char at pos %d, out of bound! Exit.\n", pos);
		exit(0);
	}

	return this->content[pos];
}


void C_String::appending(const C_String &obj)
{
	int len1 = 0;
	
	if(this->hasContent){
		len1 = strlen(this->content);
	}
	
	int len2 = strlen(obj.content);

	int fullLen = len1+len2;

	char * newContent = (char *)  malloc(sizeof(char)*fullLen + 1);
	
	if(newContent==NULL){
		fprintf(stderr,"Memory allocation error, Quit.\n");
	}

	char * pointer = newContent; 
	if(this->hasContent){
		strcpy(pointer, content);	//copy first part
		pointer += len1;
	}

	strcpy(pointer, obj.content);	//copy second part
	newContent[fullLen]='\0';

	//free old content
	this->freeContent();

	//point to new content
	this->content = newContent;

	this->hasContent = true;
}

void C_String::appending(const char nextChar)
{
	int len1 = 0;
	
	if(this->hasContent){
		len1 = strlen(this->content);
	}

	int fullLen = len1+1;

	char * newContent = (char *)  malloc(sizeof(char)*fullLen + 1);
	
	if(newContent==NULL){
		fprintf(stderr,"Memory allocation error, Quit.\n");
	}
	
	strcpy(newContent, content);	//copy first part	

	newContent[len1]=nextChar;	//copy second part
	newContent[fullLen]='\0';

	//free old content
	this->freeContent();

	//point to new content
	this->content = newContent;

	this->hasContent = true;
}