Subversion Repositories public

Rev

Rev 52 | 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
 
60 andreas 182
	if (sdate == NULL)
183
	   return -1L;
184
 
43 root 185
	if (!kenn)
186
	{
187
	   if (strlen (sdate) < 6)
188
	      return -1L;
189
	}
190
	else if (strlen (sdate) < 4 && strlen (sdate) > 0 && kenn)
191
	      return -1L;
192
	else if (strlen (sdate) == 0 && kenn)
193
	      return 0L;		/* Kein Datum */
194
 
195
	if (strchr (sdate,'.') != NULL)
196
	   punkt = 1;
197
	else
198
	   punkt = 0;
199
 
200
	if (!kenn)
201
	{
202
	   if (punkt && strlen (sdate) < 8)
203
	      return -1L;
204
	}
205
	else
206
	{
207
	   if (punkt && strlen (sdate) < 5)
208
	      return -1L;
209
	}
210
 
211
	tag = mon = year = 0;
212
	pos = 0;
213
	beg = sdate;
214
 
215
/* Tag */
216
	if (!kenn)
217
	{
218
	   if (punkt)
219
	   {
220
	      if ((end = strchr (beg,'.')) == NULL)
221
		 return -1L;
222
 
223
	      j = end - beg;
224
 
225
	      if (j > 2)
226
		 return -1L;
227
 
228
	      len = j;
229
	      beg = end;
230
	   }
231
	   else
232
	      len = 2;
233
 
234
	   strncpy (&zw[0],sdate,len);
235
	   zw[len] = 0;
236
	}
237
	else
238
	{
239
	   strcpy (&zw[0], "01");
240
	   len = 0;
241
	}
242
 
243
	tag = atoi (zw);
244
	pos = len;
245
 
246
/* Monat */
247
	if (!isdigit (*(sdate+pos)))
248
	{
249
	   beg++;
250
	   pos++;
251
	}
252
 
253
	if (punkt)
254
	{
255
	   if ((end = strchr (beg,'.')) == NULL)
256
	      return -1L;
257
 
258
	   j = end - beg;
259
 
260
	   if (j > 2)
261
	      return -1L;
262
 
263
	   len = j;
264
	   beg = end;
265
	}
266
	else
267
	   len = 2;
268
 
269
	strncpy (&zw[0],sdate+pos,len);
270
	zw[len] = 0;
271
	mon = atoi (zw);
272
	pos += len;
273
 
274
/* Jahr */
275
	if (!isdigit (*(sdate+pos)))
276
	   pos++;
277
 
278
	beg = sdate+pos;
279
	i = strlen (beg);
280
 
281
	if (i != 4 && i != 2)
282
	   return -1L;
283
 
284
	year = atoi (beg);
285
 
286
/* Überprüfung */
287
 
288
	if (tag == 0 && mon == 0 && year == 0)
289
	   return 0L;
290
 
291
	if (mon < 1 || mon > 12)
292
	   return -1L;
293
 
294
	if (year < 100)
295
	{
296
	   if (year < 50)
297
	      year += 2000;
298
	   else
299
	      year += 1900;
300
	}
301
 
302
	if ((year % 4) == 0)
303
	   MonLeiste[1] = 29;
304
	else
305
	   MonLeiste[1] = 28;
306
 
307
	if ((tag < 1 || tag > MonLeiste[mon-1]) && tag != 32)
308
	   return -1L;
309
 
310
	return (long)year * 10000L + ((long)mon * 100L) + (long)tag;
311
}
312
 
313
void set_feb (int year)
314
{
315
	if (year == 1900 || year == 2100)
316
	{
317
	   MonLeiste[1] = 28;
318
	   return;
319
	}
320
 
321
	if (year % 4)
322
	   MonLeiste[1] = 28;
323
	else
324
	   MonLeiste[1] = 29;
325
}
326
 
327
long DateToDay (long date)
328
{
329
int day, mon, year, jahre, sj, i;
330
long tage;
331
 
332
	if (date <= 0L)
333
	   return 0L;
334
 
335
	date_int (&day, &mon, &year, date);
336
 
337
	if (day <= 0 || mon <= 0 || year <= 0)
338
	   return 0L;
339
 
340
	jahre = (year >= 1900) ? year - 1900 : year;
341
	sj = 0;
342
 
343
	if (jahre < 1)
344
	   tage = 0L;
345
	else
346
	{
347
	   tage = ((long)jahre - 1L) * 365L;
348
	   sj = (jahre - 1) / 4;
349
	}
350
 
351
	tage += (long)sj;
352
	set_feb (year);
353
 
354
	for (i = 0; i < (mon-1); i++)
355
	   tage += (long)MonLeiste[i];
356
 
357
	tage += (long)day;
358
	return tage;
359
}
360
 
361
long DayToDate (long days)
362
{
363
int jahr, mon, day, i, j;
364
 
365
	if (days <= 0L)
366
	   return 0L;
367
 
368
	if (days >= 365)
369
	{
370
	   jahr = (int)(days / 365L);
371
	   mon = (int)(days - (((long)jahr * 365L) + ((long)jahr / 4L)));
372
 
373
	   if (mon == 0)
374
	      mon = 1;
375
 
376
	   jahr++;
377
	}
378
	else
379
	{
380
	   jahr = 0;
381
	   mon = days;
382
	}
383
 
384
	set_feb (jahr + 1900);
385
	i = j = 0;
386
 
387
	while (i < mon)
388
	{
389
	   i += MonLeiste[j];
390
	   j++;
391
 
392
	   if (i < mon && j >= 12)
393
	      j = 0;
394
	}
395
 
396
	i -= MonLeiste[j-1];
397
	day = mon - i;
398
	mon = j;
399
 
400
	return (long)(jahr+1900) * 10000L + ((long)mon * 100L) + (long)day;
401
}
402
 
403
long make_date (int day, int mon, int year)
404
{
405
	if (year < 1900)
406
	   year += 1900;
407
 
408
	if (year > 9999)
409
	   return 0L;
410
 
411
	return (long)year * 10000L + ((long)mon * 100L) + (long)day;
412
}
413
 
414
char *PointNumber (double Zahl, int prec, char *ret)
415
{
416
char buf[128], hv0[128];
417
int i, j, a, count, maxlen;
418
BOOL flag;
419
char *p;
420
 
60 andreas 421
	if (ret == NULL)
422
	   return NULL;
423
 
43 root 424
	// Ist Zahl < 0.0?
425
 
426
	if (Zahl < (double)0.0)
427
	{
428
	   *ret = '-';			// dann Minus setzen
429
	   j = 1;
430
	}
431
	else
432
           j = 0;
433
 
434
	sprintf (buf, "%%%d.%df", 2+prec, prec);
435
 
436
	// Wenn Zahl negativ dann Positiv machen
437
 
438
	if (Zahl < (double)0.0)
439
	   Zahl = Zahl * (-1.0);
440
 
441
	sprintf (hv0, buf, Zahl);
442
 
443
	// Interpunktion in die Zahl setzen
444
 
445
	i = strlen (hv0);
446
	maxlen = sizeof(hv0);
447
	p = hv0;
448
	a = 0;
449
	flag = FALSE;
450
	count = (i - prec - 1) % 3;
451
 
452
	if (count == 0)
453
	   count = 3;
454
 
455
	while (a < i && a < maxlen && j < maxlen)
456
	{
457
	   if (*p == '.')
458
	   {
459
              *p = ',';
460
	      flag = TRUE;
461
           }
462
 
463
	   if (count == 0 && a > 0 && !flag)
464
	   {
465
	      ret[j] = '.';
466
	      j++;
467
	      count = 3;
468
	   }
469
 
470
	   ret[j] = *p++;
471
	   j++;
472
	   a++;
473
	   count--;
474
	}
475
 
476
	ret[j] = 0;
477
	return ret;
478
}