Subversion Repositories public

Rev

Rev 47 | Rev 60 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
43 root 1
/***************************************************************************
52 andreas 2
 *   Copyright (C) 2007 by Andreas Theofilu                                *
3
 *   andreas@TheoSys.at                                                    *
43 root 4
 *                                                                         *
5
 *   This program is free software; you can redistribute it and/or modify  *
6
 *   it under the terms of the GNU General Public License as published by  *
52 andreas 7
 *   the Free Software Foundation version 3 of the License.                *
43 root 8
 *                                                                         *
9
 *   This program is distributed in the hope that it will be useful,       *
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12
 *   GNU General Public License for more details.                          *
13
 *                                                                         *
14
 *   You should have received a copy of the GNU General Public License     *
15
 *   along with this program; if not, write to the                         *
16
 *   Free Software Foundation, Inc.,                                       *
17
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18
 ***************************************************************************/
19
 
20
#include <stdio.h>
21
#include <string.h>
22
#include <math.h>
23
#include <stdlib.h>
24
#include <ctype.h>
25
#include "helper.h"
26
 
27
int ComputeSize (int OrgLen, int NewLen, int OrgSize)
28
{
29
double OLen, NLen, OSize, zw;
30
 
31
	OLen = (double)OrgLen;
32
	NLen = (double)NewLen;
33
	OSize = (double)OrgSize;
34
 
35
	zw = (NLen / OLen) * OSize;
36
	return (int)zw;
37
}
38
 
39
void date_int (int *day, int *mon, int *year, long date)
40
{
41
	*year = (int)(date / 10000);
42
	*day = (int)(date % 100);
43
	*mon = (int)((date % 10000) - *day) / 100;
44
}
45
 
46
double integer (double zahl)
47
{
48
long zw;
49
double ret;
50
 
51
	zw = (long)zahl;
52
	ret = (double)zw;
53
	return ret;
54
}
55
 
56
double round (double zahl, int prez)
57
{
58
double divi;
59
long zw;
60
 
61
	divi = pow (zahl + 0.5,(double)prez);
62
	zw = (zahl + 0.5) * divi;
63
	return (double)zw / divi;
64
}
65
 
66
long get_date (char *sdate)
67
{
68
int tag,mon,year,i,j,pos,len,punkt;
52 andreas 69
char zw[21];
43 root 70
char *beg,*end;
71
 
47 andreas 72
	if (sdate == NULL || strlen (sdate) < 6)
43 root 73
	   return -1L;
74
 
75
	if (strchr (sdate,'.') != NULL)
76
	   punkt = 1;
77
	else
78
	   punkt = 0;
79
 
80
	if (punkt && strlen (sdate) < 8)
81
	   return -1L;
82
 
83
	tag = mon = year = 0;
84
	pos = 0;
85
	beg = sdate;
86
 
87
/* Tag */
88
	if (punkt)
89
	{
90
	   if ((end = strchr (beg,'.')) == NULL)
91
	      return -1L;
92
 
93
	   j = end - beg;
94
 
95
	   if (j > 2)
96
	      return -1L;
97
 
98
	   len = j;
99
	   beg = end;
100
	}
101
	else
102
	   len = 2;
103
 
104
	strncpy (&zw[0],sdate,len);
105
	zw[len] = 0;
106
	tag = atoi (zw);
107
	pos = len;
108
 
109
/* Monat */
110
	if (!isdigit (*(sdate+pos)))
111
	{
112
	   beg++;
113
	   pos++;
114
	}
115
 
116
	if (punkt)
117
	{
118
	   if ((end = strchr (beg,'.')) == NULL)
119
	      return -1L;
120
 
121
	   j = end - beg;
122
 
123
	   if (j > 2)
124
	      return -1L;
125
 
126
	   len = j;
127
	   beg = end;
128
	}
129
	else
130
	   len = 2;
131
 
132
	strncpy (&zw[0],sdate+pos,len);
133
	zw[len] = 0;
134
	mon = atoi (zw);
135
	pos += len;
136
 
137
/* Jahr */
138
	if (!isdigit (*(sdate+pos)))
139
	   pos++;
140
 
141
	beg = sdate+pos;
142
	i = strlen (beg);
143
 
144
	if (i != 4 && i != 2)
145
	   return -1L;
146
 
147
	year = atoi (beg);
148
 
149
/* Überprüfung */
150
 
151
	if (tag == 0 && mon == 0 && year == 0)
152
	   return 0L;
153
 
154
	if (mon < 1 || mon > 12)
155
	   return -1L;
156
 
157
	if (year < 100)
158
	{
159
	   if (year < 50)
160
	      year += 2000;
161
	   else
162
	      year += 1900;
163
	}
164
 
165
	if ((year % 4) == 0)
166
	   MonLeiste[1] = 29;
167
	else
168
	   MonLeiste[1] = 28;
169
 
170
	if (tag < 1 || tag > MonLeiste[mon-1])
171
	   return -1L;
172
 
173
	return (long)year * 10000L + ((long)mon * 100L) + (long)tag;
174
}
175
 
176
long get_gebos_date (char *sdate, int kenn)
177
{
178
int tag,mon,year,i,j,pos,len,punkt;
52 andreas 179
char zw[21];
43 root 180
char *beg,*end;
181
 
182
	if (!kenn)
183
	{
184
	   if (strlen (sdate) < 6)
185
	      return -1L;
186
	}
187
	else if (strlen (sdate) < 4 && strlen (sdate) > 0 && kenn)
188
	      return -1L;
189
	else if (strlen (sdate) == 0 && kenn)
190
	      return 0L;		/* Kein Datum */
191
 
192
	if (strchr (sdate,'.') != NULL)
193
	   punkt = 1;
194
	else
195
	   punkt = 0;
196
 
197
	if (!kenn)
198
	{
199
	   if (punkt && strlen (sdate) < 8)
200
	      return -1L;
201
	}
202
	else
203
	{
204
	   if (punkt && strlen (sdate) < 5)
205
	      return -1L;
206
	}
207
 
208
	tag = mon = year = 0;
209
	pos = 0;
210
	beg = sdate;
211
 
212
/* Tag */
213
	if (!kenn)
214
	{
215
	   if (punkt)
216
	   {
217
	      if ((end = strchr (beg,'.')) == NULL)
218
		 return -1L;
219
 
220
	      j = end - beg;
221
 
222
	      if (j > 2)
223
		 return -1L;
224
 
225
	      len = j;
226
	      beg = end;
227
	   }
228
	   else
229
	      len = 2;
230
 
231
	   strncpy (&zw[0],sdate,len);
232
	   zw[len] = 0;
233
	}
234
	else
235
	{
236
	   strcpy (&zw[0], "01");
237
	   len = 0;
238
	}
239
 
240
	tag = atoi (zw);
241
	pos = len;
242
 
243
/* Monat */
244
	if (!isdigit (*(sdate+pos)))
245
	{
246
	   beg++;
247
	   pos++;
248
	}
249
 
250
	if (punkt)
251
	{
252
	   if ((end = strchr (beg,'.')) == NULL)
253
	      return -1L;
254
 
255
	   j = end - beg;
256
 
257
	   if (j > 2)
258
	      return -1L;
259
 
260
	   len = j;
261
	   beg = end;
262
	}
263
	else
264
	   len = 2;
265
 
266
	strncpy (&zw[0],sdate+pos,len);
267
	zw[len] = 0;
268
	mon = atoi (zw);
269
	pos += len;
270
 
271
/* Jahr */
272
	if (!isdigit (*(sdate+pos)))
273
	   pos++;
274
 
275
	beg = sdate+pos;
276
	i = strlen (beg);
277
 
278
	if (i != 4 && i != 2)
279
	   return -1L;
280
 
281
	year = atoi (beg);
282
 
283
/* Überprüfung */
284
 
285
	if (tag == 0 && mon == 0 && year == 0)
286
	   return 0L;
287
 
288
	if (mon < 1 || mon > 12)
289
	   return -1L;
290
 
291
	if (year < 100)
292
	{
293
	   if (year < 50)
294
	      year += 2000;
295
	   else
296
	      year += 1900;
297
	}
298
 
299
	if ((year % 4) == 0)
300
	   MonLeiste[1] = 29;
301
	else
302
	   MonLeiste[1] = 28;
303
 
304
	if ((tag < 1 || tag > MonLeiste[mon-1]) && tag != 32)
305
	   return -1L;
306
 
307
	return (long)year * 10000L + ((long)mon * 100L) + (long)tag;
308
}
309
 
310
void set_feb (int year)
311
{
312
	if (year == 1900 || year == 2100)
313
	{
314
	   MonLeiste[1] = 28;
315
	   return;
316
	}
317
 
318
	if (year % 4)
319
	   MonLeiste[1] = 28;
320
	else
321
	   MonLeiste[1] = 29;
322
}
323
 
324
long DateToDay (long date)
325
{
326
int day, mon, year, jahre, sj, i;
327
long tage;
328
 
329
	if (date <= 0L)
330
	   return 0L;
331
 
332
	date_int (&day, &mon, &year, date);
333
 
334
	if (day <= 0 || mon <= 0 || year <= 0)
335
	   return 0L;
336
 
337
	jahre = (year >= 1900) ? year - 1900 : year;
338
	sj = 0;
339
 
340
	if (jahre < 1)
341
	   tage = 0L;
342
	else
343
	{
344
	   tage = ((long)jahre - 1L) * 365L;
345
	   sj = (jahre - 1) / 4;
346
	}
347
 
348
	tage += (long)sj;
349
	set_feb (year);
350
 
351
	for (i = 0; i < (mon-1); i++)
352
	   tage += (long)MonLeiste[i];
353
 
354
	tage += (long)day;
355
	return tage;
356
}
357
 
358
long DayToDate (long days)
359
{
360
int jahr, mon, day, i, j;
361
 
362
	if (days <= 0L)
363
	   return 0L;
364
 
365
	if (days >= 365)
366
	{
367
	   jahr = (int)(days / 365L);
368
	   mon = (int)(days - (((long)jahr * 365L) + ((long)jahr / 4L)));
369
 
370
	   if (mon == 0)
371
	      mon = 1;
372
 
373
	   jahr++;
374
	}
375
	else
376
	{
377
	   jahr = 0;
378
	   mon = days;
379
	}
380
 
381
	set_feb (jahr + 1900);
382
	i = j = 0;
383
 
384
	while (i < mon)
385
	{
386
	   i += MonLeiste[j];
387
	   j++;
388
 
389
	   if (i < mon && j >= 12)
390
	      j = 0;
391
	}
392
 
393
	i -= MonLeiste[j-1];
394
	day = mon - i;
395
	mon = j;
396
 
397
	return (long)(jahr+1900) * 10000L + ((long)mon * 100L) + (long)day;
398
}
399
 
400
long make_date (int day, int mon, int year)
401
{
402
	if (year < 1900)
403
	   year += 1900;
404
 
405
	if (year > 9999)
406
	   return 0L;
407
 
408
	return (long)year * 10000L + ((long)mon * 100L) + (long)day;
409
}
410
 
411
char *PointNumber (double Zahl, int prec, char *ret)
412
{
413
char buf[128], hv0[128];
414
int i, j, a, count, maxlen;
415
BOOL flag;
416
char *p;
417
 
418
	// Ist Zahl < 0.0?
419
 
420
	if (Zahl < (double)0.0)
421
	{
422
	   *ret = '-';			// dann Minus setzen
423
	   j = 1;
424
	}
425
	else
426
           j = 0;
427
 
428
	sprintf (buf, "%%%d.%df", 2+prec, prec);
429
 
430
	// Wenn Zahl negativ dann Positiv machen
431
 
432
	if (Zahl < (double)0.0)
433
	   Zahl = Zahl * (-1.0);
434
 
435
	sprintf (hv0, buf, Zahl);
436
 
437
	// Interpunktion in die Zahl setzen
438
 
439
	i = strlen (hv0);
440
	maxlen = sizeof(hv0);
441
	p = hv0;
442
	a = 0;
443
	flag = FALSE;
444
	count = (i - prec - 1) % 3;
445
 
446
	if (count == 0)
447
	   count = 3;
448
 
449
	while (a < i && a < maxlen && j < maxlen)
450
	{
451
	   if (*p == '.')
452
	   {
453
              *p = ',';
454
	      flag = TRUE;
455
           }
456
 
457
	   if (count == 0 && a > 0 && !flag)
458
	   {
459
	      ret[j] = '.';
460
	      j++;
461
	      count = 3;
462
	   }
463
 
464
	   ret[j] = *p++;
465
	   j++;
466
	   a++;
467
	   count--;
468
	}
469
 
470
	ret[j] = 0;
471
	return ret;
472
}