-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcl_utils.c
153 lines (129 loc) · 2.74 KB
/
cl_utils.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
/*
* cldump - Dumps Clarion databases to text, SQL and CSV formats
*
* Copyright (C) 2004-2006 Julien BLACHE <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: cl_utils.c 53 2006-09-04 12:38:59Z julien $
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <endian.h>
#include <byteswap.h>
#include <iconv.h>
#if BYTE_ORDER == BIG_ENDIAN
size_t cl_fread (void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t ret;
size_t i;
uint16_t *p = (uint16_t *) ptr;
uint32_t *pp = (uint32_t *) ptr;
uint64_t *ppp = (uint64_t *) ptr;
ret = fread(ptr, size, nmemb, stream);
switch (size)
{
case 2:
for (i = 0; i < ret; i++)
p[i] = bswap_16(p[i]);
break;
case 4:
for (i = 0; i < ret; i++)
pp[i] = bswap_32(pp[i]);
break;
case 8:
for (i = 0; i < ret; i++)
ppp[i] = bswap_64(ppp[i]);
break;
default:
break;
}
return ret;
}
#endif
/*
* WARNING : cldump.h redefines fread() to cl_fread()
*/
#include "cldump.h"
void
clarion_trim (uint8_t *data, int length)
{
int i;
for (i = (length - 1); i >= 0; i--)
{
if (data[i] == 0x20)
continue;
else
{
data[i + 1] = '\0';
return;
}
}
data[0] = '\0';
}
void
clarion_singlespace (char *data)
{
char *p = NULL;
uint32_t offset;
while (*data != '\0')
{
if (*data == 0x20)
{
if (p == NULL)
p = data;
}
else
{
if (p != NULL)
{
p++;
offset = data - p;
memmove(p, data, strlen(data)+1);
data = data - offset;
p = NULL;
}
}
data++;
}
}
char *
clarion_iconv (const char *charset, char *data)
{
iconv_t cd;
char *buf, *tmp;
size_t len;
size_t buflen;
size_t ret;
len = strlen(data);
if (len == 0)
return NULL;
cd = iconv_open("UTF-8", charset);
if (cd == (iconv_t)(-1))
return NULL;
buflen = 2 * len;
buf = (char *) malloc(buflen + 1);
tmp = buf;
ret = iconv(cd, &data, &len, &tmp, &buflen);
if (ret == (size_t)(-1))
{
free(buf);
return NULL;
}
*tmp = '\0';
iconv_close(cd);
return buf;
}