File: utilities.c

package info (click to toggle)
sumalibs 1.0.36-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 412 kB
  • sloc: ansic: 3,154; lex: 174; sh: 45; makefile: 26
file content (230 lines) | stat: -rwxr-xr-x 4,410 bytes parent folder | download | duplicates (7)
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
/**
 * FileName:    utilities.c
 * Author:      Tiayyba Riaz
 * Description: C file for miscellenious functions and macros
 * **/

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

/*
 * Function Name: errorAbort(int errorCode, char* errorMsg, char* fileName, int lineNumber)
 * Description:   Reports an error on standard error and aborts
 */
void errorAbort(int32_t errorCode, char* errorMsg, char* fileName, int32_t lineNumber)
{
	fprintf(stderr,"Error %d in file %s line %d : %s\n",
			errorCode,
			fileName,
	        lineNumber,
	        errorMsg);
	
	abort();
}

void   *util_malloc(size_t chunksize, const char *filename, int32_t line)
{
	void * chunk;
	
	chunk = calloc(1,chunksize);
	
	if (!chunk)
		errorAbort(MEM_ALLOC_ERROR,"Could not allocate memory.",filename,line);
	
	return chunk;
}

/*
 * Function Name: util_realloc(void *chunk, int32_t newsize, const char *filename, int32_t line)
 * Description:   Overloading realloc funstion, changes the size of the memory object pointed to by chunk 
 * to the size specified by newsize. If memory cannot be allocated, gives the error on stderr and aborts.
 */
void   *util_realloc(void *chunk, size_t newsize, const char *filename, int32_t    line)
{
	void *newchunk;
	
	newchunk = realloc(chunk,newsize);
	
	if (!newchunk)
	{
		errorAbort(MEM_ALLOC_ERROR,"Could not allocate memory.",filename,line);
	}

	return newchunk;	
}

/*
 * Function Name: util_free(void *chunk)
 * Description:   Returns the memory specified by chunk back to operating syste.
 */
void    util_free(void *chunk)
{
	free(chunk);
}

BOOL util_findInArr(int32_t tempArr[], int seqNo, int32_t noOfSeqs)
{
	int index;
	
	for(index = 0; index < noOfSeqs; index++)
	{
		if(tempArr[index] == seqNo) return TRUE;
	}
	
	return FALSE;
}


/**
 * 
 * String handling utilities
 * 
 **/

/*
 * Function Name: str_chopAtDelim(char *dest, char *src, char *delim, BOOL includeDelim)
 * Description:   chops the string startig from source to the delimeter specified.
 */
char *str_chopAtDelim(char *dest, char *src, char *delim, BOOL includeDelim)
{
	char *temp;
	int32_t len;
	
	/* returns a pointer to the first occurance of delim in src*/
	temp = strstr(src, delim); 
	
	if (temp == NULL)
		return NULL;
	
	if (includeDelim)
	{
		/* temp - src + strlen(delim) -> a string between src and delimeter including delimeter*/
		len = temp - src + strlen(delim);
		strncpy(dest, src, len);
	}
	else
	{
		len = temp - src;
		strncpy(dest, src, temp - src);
	}
	dest[len] = '\0';
	
	return dest;
}

/*
 * Function Name: str_sepNameValue(char *name, char *value, char *src, char *delim)
 * Description:   .
 */
void str_sepNameValue(char *name, char *value, char *src, char *delim)
{
	char *temp;
	
	temp = strstr(src, delim);
	
	if(temp != NULL)
	{
		strncpy(name, src, temp - src);
		strcpy(value, temp + strlen(delim));
	}
	else
	{
		strcpy(name, src);
		strcpy(value, "");
	}
}

/*
 * Function Name: str_removeSpaces(char *src)
 * Description:   Removes the spaces from the start and end of the string.
 */
int str_isSpace (char ch)
{
	switch (ch)
	{
	case ' ':
	case '\t':
	case '\n':
		return 1;
	}
	return 0;
}

void str_removeSpaces(char *src)
{
	int32_t start = 0, end = strlen(src) - 1;
	int32_t index = 0;
	
	if (src == NULL || end < 0) return;
	
	while(str_isSpace(src[start]) && start < end) start++;
	while(str_isSpace(src[end]) && end > start) end--;
	
	if ( start == end && src[start] == ' ')
	{
		src[0] = '\0';
		return;
	}
	if (start > 0)
	{
		while(start <= end)
		{
			src[index] = src[start];
			index++;
			start++;
		}
		src[index] = '\0';
		return;
	}
	src[end+1] = '\0';
}

/*
 * Function Name: str_strrstr(char *src, char *delim)
 * Description:   Searches the position of last occurence of string delim in src.
 */
char *str_strrstr(char *src, char *delim)
{
	char *last, *next;
	next = strstr(src, delim);
	last = next;
	while(next != NULL)
	{
		last = next;
		next = strstr(last + 1, delim);
	}
	return last;
}


void* getA16Address(int size)
{
	void* address;
	address = (void*) malloc(size);
	while ((((long long unsigned int) (address))%16) != 0)
		address++;
	return(address);
}


void** reallocA16Address(void** address, int size)
{
	if (*(address) == NULL)
		*(address) = malloc(size);
	*(address) = realloc(address, size);
	while ((((long long unsigned int) (*(address)))%16) != 0)
		(*(address))++;
	return(address);
}