aboutsummaryrefslogtreecommitdiff
path: root/tests/glean/environ.cpp
blob: 68250d90d316e99a9467ca270a2d386661a8b5ce (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
// BEGIN_COPYRIGHT -*- glean -*-
// 
// Copyright (C) 1999  Allen Akin   All Rights Reserved.
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the
// Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
// KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL ALLEN AKIN BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// 
// END_COPYRIGHT




// environ.cpp:  implementation of test environment class

#include "environ.h"

#if defined(__UNIX__)
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <cstdlib>
#include <cstdio>
#elif defined(__MS__)

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

#endif

namespace GLEAN {

///////////////////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////////////////
Environment::Environment(Options& opt):
    options(opt),
    log(cout),
    winSys(opt)
{
#   if defined(__UNIX__)

	// If running tests, first create the results directory.
	// Refuse to overwrite one that already exists.
	if (opt.mode == Options::run) {
		if (opt.overwrite) {
			// remove existing db dir
			// XXX using system() probably isn't ideal
			char cmd[1000];
			snprintf(cmd, 999, "rm -rf %s", opt.db1Name.c_str());
			system(cmd);
		}
		if (mkdir(opt.db1Name.c_str(), 0755)) {
			if (errno == EEXIST)
				throw DBExists();
			else
				throw DBCantOpen(opt.db1Name);
		}
	// If comparing previous runs, make a token attempt to verify
	// that the two databases exist.
	} else {
		struct stat s;
		if (stat(opt.db1Name.c_str(), &s) || !S_ISDIR(s.st_mode))
			throw DBCantOpen(opt.db1Name);
		if (stat(opt.db2Name.c_str(), &s) || !S_ISDIR(s.st_mode))
			throw DBCantOpen(opt.db2Name);
	}

#   elif defined(__MS__)
	// If running tests, first create the results directory.
	// Refuse to overwrite one that already exists.
	if (opt.mode == Options::run) {
		if (opt.overwrite) {
			char cmd[1000];
#if defined(_MSC_VER)
			_snprintf(cmd, 999, "rd /s /q %s", opt.db1Name.c_str());
#else
			snprintf(cmd, 999, "rd /s /q %s", opt.db1Name.c_str());
#endif
			system(cmd);
		}
		if (!CreateDirectory(opt.db1Name.c_str(),0)) {
			if (GetLastError() == ERROR_ALREADY_EXISTS)
				throw DBExists();
			else
				throw DBCantOpen(opt.db1Name);
		}
	// If comparing previous runs, make a token attempt to verify
	// that the two databases exist.
	} else {
		struct _stat s; 

		if (_stat(opt.db1Name.c_str(), &s) || !(s.st_mode & _S_IFDIR))
			throw DBCantOpen(opt.db1Name);
		if (_stat(opt.db2Name.c_str(), &s) || !(s.st_mode & _S_IFDIR))
			throw DBCantOpen(opt.db2Name);
	}

#   endif
} // Environment::Environment()

///////////////////////////////////////////////////////////////////////////////
// Results-file access utilities
///////////////////////////////////////////////////////////////////////////////
string
Environment::resultFileName(string& dbName, string& testName) {
#   if defined(__UNIX__)
	string dirName(dbName + '/' + testName);
	if (mkdir(dirName.c_str(), 0755)) {
		if (errno != EEXIST)
			throw DBCantOpen(dirName);
	}
	string fileName(dirName + "/results");
#   elif defined(__MS__)
	string dirName(dbName + '/' + testName);
	if (!CreateDirectory(dirName.c_str(),0)) {
		if (GetLastError() != ERROR_ALREADY_EXISTS)
			throw DBCantOpen(dirName);
	}
	string fileName(dirName + "/results");
#   endif
	return fileName;
} // Environment::resultFileName

string
Environment::imageFileName(string& dbName, string& testName, int n) {
	char sn[4];
	sn[3] = 0;
	sn[2] = static_cast<char>('0' + n % 10);
	sn[1] = static_cast<char>('0' + (n / 10) % 10);
	sn[0] = static_cast<char>('0' + (n / 100) % 10);
#   if defined(__UNIX__)
	string fileName(dbName + '/' + testName + "/i" + sn + ".tif");
#   elif defined(__MS__)
	string fileName(dbName + '/' + testName + "/i" + sn + ".tif");
#   endif
	return fileName;
} // Environment::imageFileName

void
Environment::quiesce() {
	winSys.quiesce();
#   if defined(__UNIX__)
	sync();
#   elif defined(__MS__)
#   endif
} // Environment::quiesce

} // namespace GLEAN