-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPKINHY36.c
311 lines (282 loc) · 7.71 KB
/
PKINHY36.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/************************** PKINHY36.c ***************************************/
/*downloaded 071002 from http://cci.lbl.gov/hybrid_36/
(which is slightly different from version a few weeks earlier)
as suggested below:
PKINHY36.h mod of hybrid_36_c.h for prototypes of hy36encode(), hy36decode()
and the include from PHENIX iotbx is commented out, 071002 dcr
*/
/*! C port of the hy36encode() and hy36decode() functions in the
hybrid_36.py Python prototype/reference implementation.
See the Python script for more information.
This file has no external dependencies, NOT even standard C headers.
Optionally, use hybrid_36_c.h, or simply copy the declarations
into your code.
This file is unrestricted Open Source (cctbx.sf.net).
Please send corrections and enhancements to [email protected] .
See also: http://cci.lbl.gov/hybrid_36/
Ralf W. Grosse-Kunstleve, Feb 2007.
*/
/* The following #include may be commented out.
It is here only to enforce consistency of the declarations
and the definitions.
*/
/* #include <iotbx/pdb/hybrid_36_c.h> */ /*071002*/
/* All static functions below are implementation details
(and not accessible from other translation units).
*/
#include "PKINHY36.h" /*071002 dcr*/
static
const char*
digits_upper() { return "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
static
const char*
digits_lower() { return "0123456789abcdefghijklmnopqrstuvwxyz"; }
static
const char*
value_out_of_range() { return "value out of range."; }
static
const char* invalid_number_literal() { return "invalid number literal."; }
static
const char* unsupported_width() { return "unsupported width."; }
static
void
fill_with_stars(unsigned width, char* result)
{
while (width) {
*result++ = '*';
width--;
}
*result = '\0';
}
static
void
encode_pure(
const char* digits,
unsigned digits_size,
unsigned width,
int value,
char* result)
{
char buf[16];
int rest;
unsigned i, j;
i = 0;
j = 0;
if (value < 0) {
j = 1;
value = -value;
}
while (1) {
rest = value / digits_size;
buf[i++] = digits[value - rest * digits_size];
if (rest == 0) break;
value = rest;
}
if (j) buf[i++] = '-';
for(j=i;j<width;j++) *result++ = ' ';
while (i != 0) *result++ = buf[--i];
*result = '\0';
}
static
const char*
decode_pure(
const int* digits_values,
unsigned digits_size,
const char* s,
unsigned s_size,
int* result)
{
int si, dv;
int have_minus = 0;
int have_non_blank = 0;
int value = 0;
unsigned i = 0;
for(;i<s_size;i++) {
si = s[i];
if (si < 0 || si > 127) {
*result = 0;
return invalid_number_literal();
}
if (si == ' ') {
if (!have_non_blank) continue;
value *= digits_size;
}
else if (si == '-') {
if (have_non_blank) {
*result = 0;
return invalid_number_literal();
}
have_non_blank = 1;
have_minus = 1;
continue;
}
else {
have_non_blank = 1;
dv = digits_values[si];
if (dv < 0 || dv >= digits_size) {
*result = 0;
return invalid_number_literal();
}
value *= digits_size;
value += dv;
}
}
if (have_minus) value = -value;
*result = value;
return 0;
}
/*! hybrid-36 encoder: converts integer value to string result
width: must be 4 (e.g. for residue sequence numbers)
or 5 (e.g. for atom serial numbers)
value: integer value to be converted
result: pointer to char array of size width+1 or greater
on return result is null-terminated
return value: pointer to error message, if any,
or 0 on success
Example usage (from C++):
char result[4+1];
const char* errmsg = hy36encode(4, 12345, result);
if (errmsg) throw std::runtime_error(errmsg);
*/
const char*
hy36encode(unsigned width, int value, char* result)
{
int i = value;
if (width == 4U) {
if (i >= -999) {
if (i < 10000) {
encode_pure(digits_upper(), 10U, 4U, i, result);
return 0;
}
i -= 10000;
if (i < 1213056 /* 26*36**3 */) {
i += 466560 /* 10*36**3 */;
encode_pure(digits_upper(), 36U, 0U, i, result);
return 0;
}
i -= 1213056;
if (i < 1213056) {
i += 466560;
encode_pure(digits_lower(), 36U, 0U, i, result);
return 0;
}
}
}
else if (width == 5U) {
if (i >= -9999) {
if (i < 100000) {
encode_pure(digits_upper(), 10U, 5U, i, result);
return 0;
}
i -= 100000;
if (i < 43670016 /* 26*36**4 */) {
i += 16796160 /* 10*36**4 */;
encode_pure(digits_upper(), 36U, 0U, i, result);
return 0;
}
i -= 43670016;
if (i < 43670016) {
i += 16796160;
encode_pure(digits_lower(), 36U, 0U, i, result);
return 0;
}
}
}
else {
fill_with_stars(width, result);
return unsupported_width();
}
fill_with_stars(width, result);
return value_out_of_range();
}
/*! hybrid-36 decoder: converts string s to integer result
width: must be 4 (e.g. for residue sequence numbers)
or 5 (e.g. for atom serial numbers)
s: string to be converted
does not have to be null-terminated
s_size: size of s
must be equal to width, or an error message is
returned otherwise
result: integer holding the conversion result
return value: pointer to error message, if any,
or 0 on success
Example usage (from C++):
int result;
const char* errmsg = hy36decode(width, "A1T5", 4, &result);
if (errmsg) throw std::runtime_error(errmsg);
*/
const char*
hy36decode(unsigned width, const char* s, unsigned s_size, int* result)
{
static int first_call = 1;
static int digits_values_upper[128U];
static int digits_values_lower[128U];
static const char*
ie_range = "internal error hy36decode: integer value out of range.";
unsigned i;
int di;
const char* errmsg;
if (first_call) {
first_call = 0;
for(i=0;i<128U;i++) digits_values_upper[i] = -1;
for(i=0;i<128U;i++) digits_values_lower[i] = -1;
for(i=0;i<36U;i++) {
di = digits_upper()[i];
if (di < 0 || di > 127) {
*result = 0;
return ie_range;
}
digits_values_upper[di] = i;
}
for(i=0;i<36U;i++) {
di = digits_lower()[i];
if (di < 0 || di > 127) {
*result = 0;
return ie_range;
}
digits_values_lower[di] = i;
}
}
if (s_size == width) {
di = s[0];
if (di >= 0 && di <= 127) {
if (digits_values_upper[di] >= 10) {
errmsg = decode_pure(digits_values_upper, 36U, s, s_size, result);
if (errmsg == 0) {
/* result - 10*36**(width-1) + 10**width */
if (width == 4U) (*result) -= 456560;
else if (width == 5U) (*result) -= 16696160;
else {
*result = 0;
return unsupported_width();
}
return 0;
}
}
else if (digits_values_lower[di] >= 10) {
errmsg = decode_pure(digits_values_lower, 36U, s, s_size, result);
if (errmsg == 0) {
/* result + 16*36**(width-1) + 10**width */
if (width == 4U) (*result) += 756496;
else if (width == 5U) (*result) += 26973856;
else {
*result = 0;
return unsupported_width();
}
return 0;
}
}
else {
errmsg = decode_pure(digits_values_upper, 10U, s, s_size, result);
if (errmsg) return errmsg;
if (!(width == 4U || width == 5U)) {
*result = 0;
return unsupported_width();
}
return 0;
}
}
}
*result = 0;
return invalid_number_literal();
}