]> code.delx.au - gnu-emacs/blob - test/manual/etags/cp-src/functions.cpp
Merge from origin/emacs-25
[gnu-emacs] / test / manual / etags / cp-src / functions.cpp
1 #include "main.hpp"
2 #pragma ident "@(#)functions.cpp 1.0 98/11/12 (c) Rupak Rathore"
3
4 // Constructor default argument initializes to today's values
5 void Date::setDate ( int d , int m , int y ){
6 time_t t;
7 struct tm * ptm;
8 t = time ( NULL ) ;
9 if ( date != NULL )
10 delete date;
11 date = NULL;
12 if ( d == 0 && m == 0 && y == 0 ) //Explicitly called or default constructor hence leave it.
13 return;
14 if ( d < 0 && m < 0 && d < 0 ) //Special instruction to initialize to today's value
15 d=m=y=0;
16 date = new tm;
17 ptm=localtime ( &t ) ;
18 *date=(*ptm);
19 if ( d )
20 date->tm_mday = d;
21 if ( m )
22 date->tm_mon = m - 1; // Months are counted from January
23 if ( y > 1900 ) // Complete year specified so take into account
24 y -= 1900;
25 if ( y )
26 date->tm_year = y;
27 date->tm_sec=date->tm_min=date->tm_hour=0;
28 t = mktime ( date ) ;
29 }
30
31 // Addition operation ::: Warning ::: A combination of addition and substraction does not give a proper result
32 void Date::plus ( int days , int month , int year ){
33 if ( ! set () )
34 return;
35 date->tm_mday += days ;
36 date->tm_mon += month ;
37 date->tm_year += year ;
38 mktime ( date );
39 }
40
41 //Substraction operation ::: Warning ::: A combination of addition and substraction does not give a proper result
42 void Date::minus ( int days , int month , int year ){
43 if ( ! set () )
44 return;
45 date->tm_mday -= days ;
46 date->tm_mon -= month ;
47 date->tm_year -= year ;
48 mktime ( date );
49 }
50
51
52 void Date::shift ( void ){//Shift this date to previous working days (useful for benchmarks)
53 if ( ! set() )
54 return ;
55 while(isHoliday(*this)||isweekend()){
56 date->tm_mday -= 1 ;
57 mktime ( date );
58 }
59 }
60
61 // Assignment
62 Date & Date::operator = ( Date d ){
63 if ( d.set() )
64 setDate ( d.date->tm_mday, d.date->tm_mon + 1, d.date->tm_year );
65 return(*this);
66 }
67
68 // Add number of days
69 Date & Date::operator += ( int days ){
70 if ( set () ){
71 date->tm_mday += days ;
72 mktime ( date );
73 }
74 return(*this);
75 }
76
77 // Substract number of days
78 Date & Date::operator -= ( int days ){
79 if ( set () ){
80 date->tm_mday -= days ;
81 mktime ( date );
82 }
83 return(*this);
84 }
85
86 // Advance one day
87 Date & Date::operator ++ ( void ){
88 if ( set () ){
89 date->tm_mday += 1 ;
90 mktime ( date );
91 }
92 return(*this);
93 }
94
95 // Backwards one day
96 Date & Date::operator -- ( void ){
97 if ( set () ){
98 date->tm_mday -= 1 ;
99 mktime ( date );
100 }
101 return(*this);
102 }
103
104 int Date::operator - ( Date d ){
105 long l;
106 if (( ! set() ) || (! d.set()))
107 return(0);
108 l=(mktime(date)-mktime(d.date))/(3600*24);
109 return((int)l);
110 }
111
112 int Date::operator < ( Date d ) {
113 return ( unidate() < d.unidate() );
114 }
115
116 int Date::operator > ( Date d ) {
117 return ( unidate() > d.unidate() );
118 }
119
120 int Date::operator == ( Date d ) {
121 return ( unidate() == d.unidate() );
122 }
123
124 ostream& operator << ( ostream &c, Date d ) {
125 if ( ! d.set() )
126 c << "Null";
127 else
128 c << d.date->tm_mday << ":" << d.date->tm_mon + 1 << ":" << d.date->tm_year + 1900 ;
129 return ( c );
130 }
131
132 // Modified to read date in yyyymmdd format.
133 istream& operator >> ( istream &i, Date & dd ){
134 int d,m,y,tmp;
135 i >> tmp;
136 d=tmp%100;
137 tmp/=100;
138 m=tmp%100;
139 tmp/=100;
140 y=tmp;
141 dd.setDate(d,m,y);
142 return(i);
143 }
144 /*
145 istream& operator >> ( istream &i, Date &dd ) {
146 char input[11];
147 int d,m,y;
148 cout << "Enter the date ( dd-mm-yyyy ) : ";
149 i >> input ;
150 d = ( input[0] - '0' ) * 10 + ( input[1] - '0' );
151 m = ( input[3] - '0' ) * 10 + ( input[4] - '0' );
152 y = ( input[6] - '0' ) * 1000 + ( input[7] - '0' ) * 100 + ( input[8] - '0' ) * 10 + ( input[9] - '0' );
153 dd.setDate ( d, m, y );
154 return ( i );
155 }
156 */
157
158 // Check whether given year is leap or not
159 bool isLeap ( int year ){
160 return ( (year%100==0) ? (year%400==0) : (year%4==0) );
161 }
162
163 bool isHoliday ( Date d ){
164 long int ld;
165 ld = ( d.year()*100 + d.month() )*100 + d.day();
166 for ( int i=0; i<no_of_vacations;i++)
167 if ( ld == vacation[i] )
168 return(true);
169 return(false);
170 }
171
172 // Sort the given array in ascending order
173 void asort(int *a, int num){
174 int i,k,mini,tmp;
175 for ( k=1; k<num; k++ ){
176 mini=k-1;
177 for ( i=k; i<num; i++ )
178 if ( a[mini] > a[i] ) {
179 tmp=a[i];
180 a[i]=a[mini];
181 a[mini]=tmp;
182 }
183 }
184 }
185
186 void ReadVacation ( char *filename ) {
187 // cerr << filename;
188 ifstream vacfile(filename);
189 if ( ! vacfile.good() )
190 d_error("ReadVacation","Unable to find the vacation and holidays file");
191 // cerr << filename ;
192 d_silent("ReadVacation","vacation file successfully opened.");
193 no_of_vacations = 0;
194 while ( !vacfile.eof() )
195 vacfile >> vacation[no_of_vacations++];
196 --no_of_vacations;
197 d_silent("ReadVacation","Finished Reading file");
198 vacfile.close();
199 }
200
201 void Debug ( int lineno, int level, char* func , char* mesg ) // error_level, function, message
202 {
203 if ( debug_level <= level )
204 cerr << PROGNAME << ": " << func << ": " << lineno << ": " << debug_string[level] << ": " << mesg << endl;
205 if ( level == ERROR ){
206 cerr << PROGNAME << ": Exiting because of fatal error." <<endl ;
207 exit(2);
208 }
209 }
210
211 int WorkingDays(Date a, Date b){
212 Date tmp;
213 int wdays=0,days=0;
214 if ( (! a.set()) || (! b.set()) )
215 return(0);
216 days=b-a+1; // Inclusive
217 tmp=a;
218 for ( int i=0;i<days;i++){
219 if((!isHoliday(tmp))&&(!tmp.isweekend()))
220 wdays++;
221 tmp++;
222 }
223 return(wdays);
224 }
225
226 Date StartDay(Date a,int days){//Function to calculate the apropriate start day to finish in days working days
227 Date tmp;
228 int wdays=0;
229 if ( ! a.set() )
230 return (a);
231 tmp=a;
232 while(wdays<days){
233 if((!isHoliday(tmp))&&(!tmp.isweekend()))
234 wdays++;
235 tmp--;
236 }
237 tmp++;
238 return(tmp);
239 }