~ubuntu-branches/ubuntu/karmic/photoprint/karmic

« back to all changes in this revision

Viewing changes to util.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Milan Zamazal
  • Date: 2007-05-01 16:32:13 UTC
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20070501163213-k0gaendx7grjlmk5
Tags: upstream-0.3.5
ImportĀ upstreamĀ versionĀ 0.3.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * util.cpp - miscellaneous support functions.
3
 
 *
4
 
 * Copyright (c) 2004 by Alastair M. Robinson
5
 
 * Distributed under the terms of the GNU General Public License -
6
 
 * see the file named "COPYING" for more details.
7
 
 *
8
 
 */
9
 
 
10
 
#include <iostream>
11
 
#include <stdio.h>
12
 
#include <stdlib.h>
13
 
#include <string.h>
14
 
#include <unistd.h>
15
 
#include <sys/types.h>
16
 
#include <sys/stat.h>
17
 
#include <errno.h>
 
1
/*
 
2
 * util.cpp - miscellaneous support functions.
 
3
 *
 
4
 * Copyright (c) 2004 by Alastair M. Robinson
 
5
 * Distributed under the terms of the GNU General Public License -
 
6
 * see the file named "COPYING" for more details.
 
7
 *
 
8
 */
 
9
 
 
10
#include <iostream>
 
11
#include <fstream>
 
12
#include <stdio.h>
 
13
#include <stdlib.h>
 
14
#include <string.h>
 
15
#include <unistd.h>
 
16
#include <sys/types.h>
 
17
#include <sys/stat.h>
 
18
#include <errno.h>
18
19
 
19
20
#include "support/searchpath.h"
20
21
#include "support/pathsupport.h"
21
22
 
22
23
using namespace std;
23
24
 
24
 
bool CheckSettingsDir(const char *dirname)
25
 
{
26
 
//      const char *homedir=getenv("HOME");
 
25
bool CheckSettingsDir(const char *dirname)
 
26
{
 
27
//      const char *homedir=getenv("HOME");
27
28
        const char *homedir=get_homedir();
28
 
        if(homedir)
29
 
        {
30
 
                char *path=(char *)malloc(strlen(homedir)+strlen(dirname)+2);
31
 
                sprintf(path,"%s%c%s",homedir,SEARCHPATH_SEPARATOR,dirname);
 
29
        if(homedir)
 
30
        {
 
31
                char *path=(char *)malloc(strlen(homedir)+strlen(dirname)+2);
 
32
                sprintf(path,"%s%c%s",homedir,SEARCHPATH_SEPARATOR,dirname);
32
33
 
33
34
                cerr << "Settings directory: " << path << endl;
34
 
                
35
 
                struct stat s;
36
 
                if(stat(path,&s)!=0)
 
35
                
 
36
                struct stat s;
 
37
                if(stat(path,&s)!=0)
37
38
                {
38
 
                        cerr << "Need to create directory... " << errno << endl;
39
 
                        if(errno==ENOENT)
40
 
#ifdef WIN32
41
 
                                mkdir(path);
42
 
#else
43
 
                                mkdir(path,0755);
44
 
#endif
45
 
                }
46
 
                free(path);
47
 
                return(true);
48
 
        }
49
 
        else
50
 
                return(false);
51
 
}
52
 
 
53
 
 
54
 
char *BuildAbsoluteFilename(const char *fname)
55
 
{
56
 
        char *result=NULL;
57
 
        char cwdbuf[1024];
58
 
        int l;
59
 
 
60
 
        getcwd(cwdbuf,1023);
61
 
 
62
 
        l=strlen(fname)+strlen(cwdbuf)+3;       
63
 
        result=(char *)malloc(l);
64
 
        
65
 
        sprintf(result,"%s%c%s",cwdbuf,SEARCHPATH_SEPARATOR,fname);     
66
 
        return(result);
67
 
}
 
39
                        cerr << "Need to create directory... " << errno << endl;
 
40
                        if(errno==ENOENT)
 
41
#ifdef WIN32
 
42
                                mkdir(path);
 
43
#else
 
44
                                mkdir(path,0755);
 
45
#endif
 
46
                }
 
47
                free(path);
 
48
                return(true);
 
49
        }
 
50
        else
 
51
                return(false);
 
52
}
 
53
 
 
54
 
 
55
char *BuildAbsoluteFilename(const char *fname)
 
56
{
 
57
        char *result=NULL;
 
58
        char cwdbuf[1024];
 
59
        int l;
 
60
 
 
61
        getcwd(cwdbuf,1023);
 
62
 
 
63
        l=strlen(fname)+strlen(cwdbuf)+3;       
 
64
        result=(char *)malloc(l);
 
65
        
 
66
        sprintf(result,"%s%c%s",cwdbuf,SEARCHPATH_SEPARATOR,fname);     
 
67
        return(result);
 
68
}
68
69
 
69
70
 
70
71
char *SerialiseFilename(const char *fname,int serialno,int max)
101
102
 
102
103
 
103
104
 
104
 
int TestNumeric(char *str)
105
 
{
106
 
        int result=1;
107
 
        int c;
108
 
        while((c=*str++))
109
 
        {
110
 
                if((c<'0')||(c>'9'))
111
 
                        result=0;
112
 
        }
113
 
        return(result);
114
 
}
115
 
 
116
 
 
117
 
int TestHostName(char *str,char **hostname,int *port)
118
 
{
119
 
        int result=0;
120
 
        int c;
121
 
        char *src=str;
122
 
        while((c=*src++))
123
 
        {
124
 
                if(c==':')
125
 
                {
126
 
                        if(TestNumeric(src))
127
 
                        {
128
 
                                int hnl=src-str;
129
 
                                *port=atoi(src);
130
 
                                *hostname=(char *)malloc(hnl+1);
131
 
                                strncpy(*hostname,str,hnl);
132
 
                                (*hostname)[hnl-1]=0;
133
 
                                result=1;       
134
 
                        }
135
 
                }
136
 
 
137
 
        }
138
 
        return(result);
139
 
}
 
105
int TestNumeric(char *str)
 
106
{
 
107
        int result=1;
 
108
        int c;
 
109
        while((c=*str++))
 
110
        {
 
111
                if((c<'0')||(c>'9'))
 
112
                        result=0;
 
113
        }
 
114
        return(result);
 
115
}
 
116
 
 
117
 
 
118
int TestHostName(char *str,char **hostname,int *port)
 
119
{
 
120
        int result=0;
 
121
        int c;
 
122
        char *src=str;
 
123
        while((c=*src++))
 
124
        {
 
125
                if(c==':')
 
126
                {
 
127
                        if(TestNumeric(src))
 
128
                        {
 
129
                                int hnl=src-str;
 
130
                                *port=atoi(src);
 
131
                                *hostname=(char *)malloc(hnl+1);
 
132
                                strncpy(*hostname,str,hnl);
 
133
                                (*hostname)[hnl-1]=0;
 
134
                                result=1;       
 
135
                        }
 
136
                }
 
137
 
 
138
        }
 
139
        return(result);
 
140
}
 
141
 
 
142
 
 
143
bool CompareFiles(const char *fn1,const char *fn2)
 
144
{
 
145
        bool result=true;
 
146
        int l1,l2;
 
147
        char *buf1,*buf2;
 
148
        ifstream i1,i2;
 
149
        i1.open(fn1,ios::binary);
 
150
        i2.open(fn2,ios::binary);
 
151
 
 
152
        i1.seekg(0, ios::end);
 
153
        l1= i1.tellg();
 
154
        i1.seekg (0, ios::beg);
 
155
 
 
156
        i2.seekg(0, ios::end);
 
157
        l2= i2.tellg();
 
158
        i2.seekg (0, ios::beg);
 
159
 
 
160
        if(l1==l2)
 
161
        {
 
162
                buf1 = new char [l1];
 
163
                buf2 = new char [l2];
 
164
 
 
165
                i1.read (buf1,l1);
 
166
                i2.read (buf2,l2);
 
167
 
 
168
                for(int i=0;i<l1;++i)
 
169
                {
 
170
                        if(buf1[i]!=buf2[i])
 
171
                        {
 
172
                                result=false;
 
173
                                i=l1;
 
174
                        }
 
175
                }
 
176
                delete[] buf1;
 
177
                delete[] buf2;
 
178
        }
 
179
        else
 
180
                result=false;
 
181
        i1.close();
 
182
        i2.close();
 
183
 
 
184
        return(result);
 
185
}
 
186