-
Notifications
You must be signed in to change notification settings - Fork 9
/
dbf_misc.c
212 lines (172 loc) · 3.63 KB
/
dbf_misc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (c) 1991, 1992, 1993 Brad Eacker,
* (Music, Intuition, Software, and Computers)
* All Rights Reserved
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include "dbf_misc.h"
#include "dbf_sdncal.h"
#include "php_reentrancy.h"
#if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
# if defined(__LITTLE_ENDIAN__)
# undef WORDS_BIGENDIAN
# else
# if defined(__BIG_ENDIAN__)
# define WORDS_BIGENDIAN
# endif
# endif
#endif
/*
* routine to change little endian long to host long
*/
long get_long(char *cp)
{
int ret;
unsigned char *source = (unsigned char *)cp;
ret = *source++;
ret += ((*source++)<<8);
ret += ((*source++)<<16);
ret += ((*source++)<<24);
return ret;
}
void put_long(char *cp, long lval)
{
*cp++ = lval & 0xff;
*cp++ = (lval >> 8) & 0xff;
*cp++ = (lval >> 16) & 0xff;
*cp++ = (lval >> 24) & 0xff;
}
/*
* routine to change little endian short to host short
*/
int get_short(char *cp)
{
int ret;
unsigned char *source = (unsigned char *)cp;
ret = *source++;
ret += ((*source++)<<8);
return ret;
}
void put_short(char *cp, int sval)
{
*cp++ = sval & 0xff;
*cp++ = (sval >> 8) & 0xff;
}
double get_double(char *cp)
{
double ret;
unsigned char *dp = (unsigned char *)&ret;
int i;
#ifdef WORDS_BIGENDIAN
for (i = 7; i >= 0; i--) {
#else
for (i = 0; i <= 7; i++) {
#endif
dp[i] = *cp++;
}
return ret;
}
void put_double(char *cp, double fval)
{
unsigned char *dp = (unsigned char *)&fval;
int i;
#ifdef WORDS_BIGENDIAN
for (i = 7; i >= 0; i--) {
#else
for (i = 0; i <= 7; i++) {
#endif
cp[i] = *dp++;
}
}
void copy_fill(char *dp, char *sp, int len)
{
while (*sp && len > 0) {
*dp++ = *sp++;
len--;
}
while (len-- > 0)
*dp++ = ' ';
}
void copy_crimp(char *dp, char *sp, int len)
{
while (len-- > 0) {
*dp++ = *sp++;
}
*dp = 0;
for (dp-- ; *dp == ' '; dp--) {
*dp = 0;
}
}
void db_set_date(char *cp, int year, int month, int day)
{
if (month > 12)
month = 0;
if (day > 31)
day = 0;
snprintf(cp, 9, "%04d%02d%02d", year, month, day);
}
int db_date_year(char *cp)
{
int year, i;
for (year = 0, i = 0; i < 4; i++)
year = year * 10 + (cp[i] - '0');
return year;
}
int db_date_month(char *cp)
{
int month, i;
for (month = 0, i = 4; i < 6; i++)
month = month * 10 + (cp[i] - '0');
return month;
}
int db_date_day(char *cp)
{
int day, i;
for (day = 0, i = 6; i < 8; i++)
day = day * 10 + (cp[i] - '0');
return day;
}
void db_set_timestamp(char *cp, int jdn, int msecs)
{
int year, month, day, hour, minute, second, millis;
db_sdn_to_gregorian(jdn, &year, &month, &day);
millis = msecs % 1000;
msecs /= 1000;
second = msecs % 60;
msecs /= 60;
minute = msecs % 60;
msecs /= 60;
hour = msecs;
snprintf(cp, 19, "%04d%02d%02d%02d%02d%02d.%03d", year, month, day, hour, minute, second, millis);
}
void db_get_timestamp(char *cp, int *jdn, int *msecs)
{
int year, month, day, hour, minute, second, millis;
sscanf(cp, "%04d%02d%02d%02d%02d%02d.%03d", &year, &month, &day, &hour, &minute, &second, &millis);
*jdn = db_gregorian_to_sdn(year, month, day);
*msecs = (hour * 60 * 60 * 1000) + (minute * 60 * 1000) + (second * 1000) + millis;
}
#include <time.h>
char *db_cur_date(char *cp)
{
struct tm *ctm, tmbuf;
time_t c_time;
c_time = time((time_t *)NULL);
ctm = php_localtime_r(&c_time, &tmbuf);
if (cp == NULL)
cp = (char *)emalloc(9);
if (ctm == NULL)
return NULL;
db_set_date(cp, ctm->tm_year + 1900, ctm->tm_mon + 1, ctm->tm_mday);
return cp;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/