File: util.cpp

package info (click to toggle)
photoprint 0.4.1-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 10,568 kB
  • ctags: 4,517
  • sloc: cpp: 46,643; sh: 10,360; ansic: 4,755; makefile: 543; sed: 16
file content (329 lines) | stat: -rw-r--r-- 5,960 bytes parent folder | download
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
/*
 * util.cpp - miscellaneous support functions.
 *
 * Copyright (c) 2004 by Alastair M. Robinson
 * Distributed under the terms of the GNU General Public License -
 * see the file named "COPYING" for more details.
 *
 */

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

#include "debug.h"
#include "searchpath.h"
#include "pathsupport.h"

using namespace std;

bool CreateDirIfNeeded(const char *path)
{
	struct stat s;
	if(stat(path,&s)==0)
		return(true);

	if(errno==ENOENT)
	{
#ifdef WIN32
		return(mkdir(path)==0);
#else
		return(mkdir(path,0755)==0);
#endif
	}
	return(false);
}


bool CheckFileExists(const char *file)
{
	struct stat s;
	return(stat(file,&s)==0);
}


bool CheckSettingsDir(const char *dirname)
{
	const char *homedir=get_homedir();
	if(homedir)
	{
		char *path=(char *)malloc(strlen(homedir)+strlen(dirname)+2);
		sprintf(path,"%s%c%s",homedir,SEARCHPATH_SEPARATOR,dirname);

		Debug[TRACE] << "Settings directory: " << path << endl;
		CreateDirIfNeeded(path);

		free(path);
		return(true);
	}
	else
		return(false);
}


char *BuildAbsoluteFilename(const char *fname)
{
	char *result=NULL;
	char cwdbuf[1024];
	int l;

	if(!(getcwd(cwdbuf,1023)))
		throw "Can't get curent working directory";

	l=strlen(fname)+strlen(cwdbuf)+3;	
	result=(char *)malloc(l);
	
	sprintf(result,"%s%c%s",cwdbuf,SEARCHPATH_SEPARATOR,fname);	
	return(result);
}


char *BuildFilename(const char *root,const char *suffix,const char *fileext)
{
	// Build a filename like <imagename><channel>.<extension>
	// Must take care not to treat any . characters before the last slash as
	// a file-extension point!

	char *extension;

	char *filename=NULL;
	if(fileext)
		filename=(char *)malloc(strlen(root)+strlen(suffix)+strlen(fileext)+3);
	else
		filename=(char *)malloc(strlen(root)+strlen(suffix)+3);

	char *root2=strdup(root);
	extension = root2 + strlen (root2) - 1;
	while (extension >= root2)
	{
		if(*extension == '/' || *extension == '\\')
			extension=root2;
		if (*extension == '.') break;
		extension--;
	}
	if (extension >= root2 && fileext && strlen(fileext))
	{
		*(extension++) = '\0';
		sprintf(filename,"%s%s.%s", root2, suffix, fileext);
	}
	else
	{
		if(extension>=root2)
			*(extension++) = '\0';
		sprintf(filename,"%s%s", root2, suffix);
	}
	free(root2);

	return(filename);
}


char *SerialiseFilename(const char *fname,int serialno,int max)
{
	int digits=0;
	while(max)
	{
		++digits;
		max/=10;
	}
	char *ftmp=strdup(fname);
	const char *extension="";
	int idx=strlen(ftmp)-1;
	while(idx>0)
	{
		if(ftmp[idx]=='.')
			break;
		--idx;
	}
	if(idx)
	{
		extension=ftmp+idx+1;
		ftmp[idx]=0;
	}

	char *result=(char *)malloc(strlen(ftmp)+strlen(extension)+digits+4);
	if(digits)
		sprintf(result,"%s_%0*d.%s",ftmp,digits,serialno,extension);
	else
		sprintf(result,"%s_%d.%s",ftmp,serialno,extension);
	free(ftmp);
	return(result);
}



int TestNumeric(char *str)
{
	int result=1;
	int c;
	while((c=*str++))
	{
		if((c<'0')||(c>'9'))
			result=0;
	}
	return(result);
}


bool TestHostName(char *str,char **hostname,int *port)
{
	int c;
	char *src=str;
	while((c=*src++))
	{
		if(c==':')
		{
			if(TestNumeric(src))
			{
				int hnl=src-str;
				*port=atoi(src);
				*hostname=(char *)malloc(hnl+1);
				strncpy(*hostname,str,hnl);
				(*hostname)[hnl-1]=0;
				return(true);
			}
		}

	}
	return(false);
}


bool CompareFiles(const char *fn1,const char *fn2)
{
	bool result=true;
	int l1,l2;
	char *buf1,*buf2;
	ifstream i1,i2;
 	i1.open(fn1,ios::binary);
	i2.open(fn2,ios::binary);

	i1.seekg(0, ios::end);
	l1= i1.tellg();
	i1.seekg (0, ios::beg);

	i2.seekg(0, ios::end);
	l2= i2.tellg();
	i2.seekg (0, ios::beg);

	if(l1==l2)
	{
		buf1 = new char [l1];
		buf2 = new char [l2];

		i1.read (buf1,l1);
		i2.read (buf2,l2);

		for(int i=0;i<l1;++i)
		{
			if(buf1[i]!=buf2[i])
			{
				result=false;
				i=l1;
			}
		}
		delete[] buf1;
		delete[] buf2;
	}
	else
		result=false;
	i1.close();
	i2.close();

	return(result);
}


// A "safe" version of strdup which returns a valid pointer to an empty string
// rather than NULL if src is NULL

char *SafeStrdup(const char *src)
{
	if(src)
		return(strdup(src));
	else
		return(strdup(""));
}


// A "safe" version of strcat which returns a valid pointer to an empty string
// if both parameters are null, a copy of the non-null parameter if the other is
// null, or a newly-allocated string containing the concatenated parameters if
// both are valid.
// In all cases the result is in newly-allocated storage and may (and must!) be
// free()ed when no longer needed.

char *SafeStrcat(const char *str1,const char *str2)
{
	if(str1 && str2)
	{
		int l=strlen(str1)+strlen(str2)+1;
		char *result=(char *)malloc(l);
		sprintf(result,"%s%s",str1,str2);
		return(result);
	}
	if(str1)
		return(strdup(str1));
	if(str2)
		return(strdup(str2));
	return(strdup(""));
}


// Utility function to compare two strings, but ignoring differences
// due to spaces.
int StrcasecmpIgnoreSpaces(const char *str1,const char *str2)
{
	while((*str1!=0) && (*str2!=0))
	{
		while(*str1==32)
			++str1;
		while(*str2==32)
			++str2;
		int s1=(*str1++) & ~32;
		int s2=(*str2++) & ~32;
		if(s1!=s2)
		{
			if(s1<s2)
				return(-1);
			else
				return(1);
		}
	}
	// If we reach here, the strings must be equal.
	return(0);
}


// Dummy class for seeding the random number generator.  Declaring a global instance
// of this class ensures that the seed is set once and only once for the entire program.

class RandomSeededClass
{
	public:
	RandomSeededClass()
	{
		srand(time(NULL));
	}
	inline int Random(int max)
	{
		return(rand() % max);
	}
};

RandomSeededClass globalrandomseeded;

// By deferring the call to the class's function we ensure that the global
// is instantiated, and thus the seed is set.

int RandomSeeded(int max)
{
	return(globalrandomseeded.Random(max));
}