/[cvs]/dary/util.cpp
ViewVC logotype

Contents of /dary/util.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations)
Fri Nov 5 20:48:15 2004 UTC (13 years, 11 months ago) by riso
Branch: MAIN
CVS Tags: Obalky, HEAD
Changes since 1.1: +71 -0 lines
Port do mingw; pocty ludi/darov; tlac aj bez oznacenia; slovenske triedenie

1 #include "util.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <wx/datetime.h>
5
6 #define BLOCK_SIZE 1024
7
8 void IncAlloc(tAllocator *a) {
9 if (a->n == a->alloc) {
10 a->alloc += BLOCK_SIZE;
11 a->data = (void **)realloc(a->data, a->alloc*sizeof(void *));
12 }
13 a->n++;
14 }
15
16 void DecAlloc(tAllocator *a) {
17 a->n--;
18 if (a->n == 0) return;
19 if (a->n < a->alloc-2*BLOCK_SIZE) {
20 a->alloc -= BLOCK_SIZE;
21 a->data = (void **)realloc(a->data, a->alloc*sizeof(void *));
22 }
23 }
24
25 void InitAlloc(tAllocator *a, int n) {
26 a->n = n;
27 a->alloc = (n/BLOCK_SIZE + 1)*BLOCK_SIZE;
28 a->data = (void **)malloc(a->alloc*sizeof(void *));
29 }
30
31 void FreeAlloc(tAllocator *a) {
32 free(a->data);
33 free(a);
34 }
35
36 static int Leap(int y) {
37 if (y%400==0) return 1;
38 if (y%100==0) return 0;
39 if (y%4==0) return 1;
40 return 0;
41 }
42
43 static int Days(int m, int y) {
44 static int T[12]={31,28,31,30,31,30,31,31,30,31,30,31};
45 if ((m==2)&&(Leap(y))) return 29;
46 return T[m-1];
47 }
48
49 int str2cal(const char *s) {
50 int d, m, y, i, o;
51 wxDateTime D=wxDateTime::Now();
52
53 i=sscanf(s, " %d . %d . %d ", &d, &m, &y);
54 if (i<2) return DATE_ERR;
55 if (i==2) y = D.GetYear();
56 if (y<100) y+=2000;
57
58 if (m<1) return DATE_ERR;
59 if (m>12) return DATE_ERR;
60 if (d<1) return DATE_ERR;
61 if (d>Days(m,y)) return DATE_ERR;
62
63 o = (y-1)*365;
64 o += (y-1)/4;
65 o -= (y-1)/100;
66 o += (y-1)/400;
67
68 for (i=1; i<m; i++) o+=Days(i,y);
69 o += d;
70
71 return o;
72 }
73
74 char *cal2str(int o) {
75 int d,m,y;
76 static char s[100];
77
78
79 o--;
80 y = 1 + 400*(o/(400*365 + 100 -3));
81 o %= (400*365 + 100 -3);
82 y += 100*(o/(100*365 + 24));
83 o %= (100*365 + 24);
84 y += 4*(o/(4*365+1));
85 o %= (4*365 + 1);
86 y += o/365;
87 o %= 365;
88 o++;
89
90 for (m=1; m<=12; m++) {
91 if (o<=Days(m,y)) break;
92 o -= Days(m,y);
93 }
94
95 d = o;
96 sprintf(s,"%d. %d. %d", d, m, y);
97
98 return s;
99 }
100
101 // Slovenske porovnanie; kodovanie win1250 !
102 int mystrcmp(const char *a, const char *b) {
103 int A,B;
104 static int T[260] = {
105 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
106 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
107 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
108 60, 61, 62, 63, 64, 65, 71, 73, 77, 81, 83, 85, 87, 93, 97, 99, 101, 105, 107, 109,
109 115, 117, 119, 123, 127, 131, 135, 137, 139, 141, 145, 149, 150, 151, 152, 153, 154, 66, 72, 74,
110 78, 82, 84, 86, 88, 94, 98, 100, 102, 106, 108, 110, 116, 118, 120, 124, 128, 132, 136, 138,
111 140, 142, 146, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 125, 170,
112 171, 129, 147, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 126, 183, 184, 130, 148, 185,
113 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
114 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 121, 67, 218, 219, 69, 103, 220, 221,
115 75, 222, 223, 224, 225, 95, 226, 79, 227, 228, 229, 111, 113, 230, 231, 232, 233, 234, 133, 235,
116 236, 143, 237, 238, 122, 68, 239, 240, 70, 104, 241, 242, 76, 243, 244, 245, 246, 96, 247, 80,
117 248, 249, 250, 112, 114, 251, 252, 253, 254, 255, 134, 256, 257, 144, 258, 259, 89, 90, 91, 92
118 };
119
120 while (1) {
121 A = B = -1;
122
123 if (a[0] == 'C') {
124 if (a[1] == 'H') {
125 A = 256;
126 a++;
127 }
128 else if (a[1] == 'h') {
129 A = 257;
130 a++;
131 }
132 } else if (a[0] == 'c') {
133 if (a[1] == 'H') {
134 A = 258;
135 a++;
136 }
137 else if (a[1] == 'h') {
138 A = 259;
139 a++;
140 }
141 }
142 if (A == -1) A = *((unsigned char *)a);
143
144 if (b[0] == 'C') {
145 if (b[1] == 'H') {
146 B = 256;
147 b++;
148 }
149 else if (b[1] == 'h') {
150 B = 257;
151 b++;
152 }
153 } else if (b[0] == 'c') {
154 if (b[1] == 'H') {
155 B = 258;
156 b++;
157 }
158 else if (b[1] == 'h') {
159 B = 259;
160 b++;
161 }
162 }
163 if (B == -1) B = *((unsigned char *)b);
164
165 if (T[A]<T[B]) return -1;
166 if (T[A]>T[B]) return 1;
167
168 if (a[0] == 0) return 0;
169 a++; b++;
170 }
171 }

CVS Admin">CVS Admin
ViewVC Help
Powered by ViewVC 1.1.26