1 : |
agomez |
1 |
#include "areatime.h" |
2 : |
|
|
#include "actionattimes.h" |
3 : |
|
|
#include "errorhandler.h" |
4 : |
|
|
#include "gadget.h" |
5 : |
|
|
#include "global.h" |
6 : |
|
|
|
7 : |
|
|
int ActionAtTimes::readFromFile(CommentStream& infile, const TimeClass* const TimeInfo) {
|
8 : |
|
|
|
9 : |
|
|
/* File format is:
|
10 : |
|
|
* y1 s1
|
11 : |
|
|
* ...
|
12 : |
|
|
* yN sN
|
13 : |
|
|
* where y1, ..., yN are either a year or the text 'all'
|
14 : |
|
|
* and s1, ..., sN are either a step or the text 'all'
|
15 : |
|
|
*/
|
16 : |
|
|
|
17 : |
|
|
infile >> ws;
|
18 : |
|
|
if (infile.fail())
|
19 : |
|
|
return 0;
|
20 : |
|
|
|
21 : |
|
|
int check = 0; //check = 0 continue reading, check = 1 quit
|
22 : |
|
|
int year, step, column;
|
23 : |
|
|
year = step = column = 0;
|
24 : |
|
|
IntVector readtext(2, 0);
|
25 : |
|
|
char text[MaxStrLength];
|
26 : |
|
|
strncpy(text, "", MaxStrLength);
|
27 : |
|
|
streampos pos = infile.tellg();
|
28 : |
|
|
|
29 : |
|
|
while (!infile.eof() && (check == 0)) {
|
30 : |
|
|
//update the value of pos
|
31 : |
|
|
pos = infile.tellg();
|
32 : |
|
|
|
33 : |
|
|
if (isdigit(infile.peek())) {
|
34 : |
|
|
//OK, we are about to read a number ...
|
35 : |
|
|
readtext[column] = 0;
|
36 : |
|
|
if (column == 0)
|
37 : |
|
|
infile >> year >> ws;
|
38 : |
|
|
else
|
39 : |
|
|
infile >> step >> ws;
|
40 : |
|
|
|
41 : |
|
|
} else {
|
42 : |
|
|
//OK, we are about to read a word that should be 'all'
|
43 : |
|
|
infile >> text >> ws;
|
44 : |
|
|
readtext[column] = 1;
|
45 : |
|
|
if (strcasecmp(text, "all") != 0)
|
46 : |
|
|
check = 1; //we want to exit this while loop
|
47 : |
|
|
}
|
48 : |
|
|
|
49 : |
|
|
//now we have read from infile the second column we store the data
|
50 : |
|
|
if (column == 1) {
|
51 : |
|
|
if ((readtext[0]) && (readtext[1])) {
|
52 : |
|
|
//we have read 'all' 'all'
|
53 : |
|
|
everyStep = 1;
|
54 : |
|
|
//check = 1; //we want to exit this while loop
|
55 : |
|
|
|
56 : |
|
|
} else if ((readtext[0]) && !(readtext[1])) {
|
57 : |
|
|
//we have read 'all' step
|
58 : |
|
|
if ((TimeInfo->getLastYear() != TimeInfo->getFirstYear()) ||
|
59 : |
|
|
(TimeInfo->getFirstStep() <= step && step <= TimeInfo->getLastStep()))
|
60 : |
|
|
Steps.resize(1, step);
|
61 : |
|
|
|
62 : |
|
|
} else if (!(readtext[0]) && (readtext[1])) {
|
63 : |
|
|
//we have read year 'all''
|
64 : |
|
|
if (TimeInfo->getFirstYear() <= year && year <= TimeInfo->getLastYear())
|
65 : |
|
|
Years.resize(1, year);
|
66 : |
|
|
|
67 : |
|
|
} else {
|
68 : |
|
|
//we have read year step
|
69 : |
|
|
if (TimeInfo->isWithinPeriod(year, step))
|
70 : |
|
|
TimeSteps.resize(1, TimeInfo->calcSteps(year, step));
|
71 : |
|
|
|
72 : |
|
|
}
|
73 : |
|
|
}
|
74 : |
|
|
column = !column; //change column to be read
|
75 : |
|
|
}
|
76 : |
|
|
|
77 : |
|
|
if (!infile.eof())
|
78 : |
|
|
infile.seekg(pos);
|
79 : |
|
|
return 1;
|
80 : |
|
|
}
|
81 : |
|
|
|
82 : |
|
|
void ActionAtTimes::addActions(const IntVector& years,
|
83 : |
|
|
const IntVector& steps, const TimeClass* const TimeInfo) {
|
84 : |
|
|
|
85 : |
|
|
if (years.Size() != steps.Size())
|
86 : |
|
|
handle.logMessage(LOGFAIL, "Error in actionattimes - different number of years and steps");
|
87 : |
|
|
if (everyStep)
|
88 : |
|
|
return;
|
89 : |
|
|
int i;
|
90 : |
|
|
for (i = 0; i < years.Size(); i++)
|
91 : |
|
|
if (TimeInfo->isWithinPeriod(years[i], steps[i]))
|
92 : |
|
|
TimeSteps.resize(1, TimeInfo->calcSteps(years[i], steps[i]));
|
93 : |
|
|
}
|
94 : |
|
|
|
95 : |
|
|
void ActionAtTimes::addActionsAllYears(const IntVector& steps, const TimeClass* const TimeInfo) {
|
96 : |
|
|
|
97 : |
|
|
if (everyStep)
|
98 : |
|
|
return;
|
99 : |
|
|
int i;
|
100 : |
|
|
for (i = 0; i < steps.Size(); i++)
|
101 : |
|
|
if ((TimeInfo->getLastYear() != TimeInfo->getFirstYear()) ||
|
102 : |
|
|
(TimeInfo->getFirstStep() <= steps[i] && steps[i] <= TimeInfo->getLastStep()))
|
103 : |
|
|
Steps.resize(1, steps[i]);
|
104 : |
|
|
}
|
105 : |
|
|
|
106 : |
|
|
void ActionAtTimes::addActionsAllSteps(const IntVector& years, const TimeClass* const TimeInfo) {
|
107 : |
|
|
|
108 : |
|
|
if (everyStep)
|
109 : |
|
|
return;
|
110 : |
|
|
int i;
|
111 : |
|
|
for (i = 0; i < years.Size(); i++)
|
112 : |
|
|
if (TimeInfo->getFirstYear() <= years[i] && years[i] <= TimeInfo->getLastYear())
|
113 : |
|
|
Years.resize(1, years[i]);
|
114 : |
|
|
}
|
115 : |
|
|
|
116 : |
|
|
/* This function could be better implemented -- e.g. sort the vectors
|
117 : |
|
|
* TimeSteps, Years and Steps and use that to increase speed. That
|
118 : |
|
|
* could be done by insisting that TimeInfo's time is not decreasing
|
119 : |
|
|
* between calls, so that we can keep three indices, one for each
|
120 : |
|
|
* vector, telling us where we quit our search in the last call */
|
121 : |
|
|
int ActionAtTimes::atCurrentTime(const TimeClass* const TimeInfo) const {
|
122 : |
|
|
if (everyStep)
|
123 : |
|
|
return 1;
|
124 : |
|
|
int i;
|
125 : |
|
|
for (i = 0; i < Steps.Size(); i++)
|
126 : |
|
|
if (Steps[i] == TimeInfo->getStep())
|
127 : |
|
|
return 1;
|
128 : |
|
|
for (i = 0; i < Years.Size(); i++)
|
129 : |
|
|
if (Years[i] == TimeInfo->getYear())
|
130 : |
|
|
return 1;
|
131 : |
|
|
for (i = 0; i < TimeSteps.Size(); i++)
|
132 : |
|
|
if (TimeSteps[i] == TimeInfo->getTime())
|
133 : |
|
|
return 1;
|
134 : |
|
|
return 0;
|
135 : |
|
|
}
|