4 min read

Program Architecture

Just a small peek at the C++ driving the cross platform layer called XPK, for xcross platform kernel.

  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
//
// Copyright 1997-2005 Intermine Pty Ltd, All Rights Reserved.
//
// $Revision: 6721 $
// $Author: sgm $
// $Date: 2006-09-14 14:26:25 +1000 (Thu, 14 Sep 2006) $
// $URL: svn://im-svn/common/trunk/src/xpk/xpk_conversions.cpp $
//

#define svn_id "$Id: xpk_conversions.cpp 6721 2006-09-14 14:26:25 +1000 (Thu, 14 Sep 2006) sgm $"

#define XPK_MT

#include <xpk_core.h>
#include <xpk_conversions.h>
#include <ipe_logger.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/////////////////////////////////////////////////////////////////////

XPK_Trace::XPK_Trace(): _tg(XPK_GetTG()) {
  _tg->incTrace();
}

XPK_Trace::~XPK_Trace() {
  _tg->_traceTop--;
}

void XPK_Trace::add(const char *key, const char *value) {
  _tg->add(key, value);
}

void XPK_Trace::add(const char *key, I64 value) {
  _tg->add(key, value);
}

void XPK_Trace::strings_begin(I32 &handle) {
  _tg->strings_begin(handle);
}

bool XPK_Trace::strings_more(I32 &handle) {
  return _tg->strings_more(handle);
}

void XPK_Trace::strings_next(I32 &handle) {
  _tg->strings_next(handle);
}

void XPK_Trace::strings_entry(const char * &key, const char * &value, I32 &handle) {
  _tg->strings_entry(key, value, handle);
}

void XPK_Trace::numbers_begin(I32 &handle) {
  _tg->numbers_begin(handle);
}

bool XPK_Trace::numbers_more(I32 &handle) {
  return _tg->numbers_more(handle);
}

void XPK_Trace::numbers_next(I32 &handle) {
  _tg->numbers_next(handle);
}

void XPK_Trace::numbers_entry(const char * &key, I64 &value, I32 &handle) {
  _tg->numbers_entry(key, value, handle);
}

void XPK_Trace::decode(char *buffer, I32 length, const char *fmt) {
  *buffer = 0;
  char *rover = buffer, *end = buffer + length;
  XPK_SafeAppend( rover, end, fmt);
}

/////////////////////////////////////////////////////////////////////

XPK_Destructor::XPK_Destructor(XPK_DestructorFunction function, void *data,
                               I32 size): _function(function), _data(data), _size(size), _callMethod(true) {
}

XPK_Destructor::~XPK_Destructor() {
  if (_callMethod) (*_function)(_data, _size);
}

/////////////////////////////////////////////////////////////////////

void XPK_SafeAppend(char * &rover, const char *end, const char *value) {
  if ((rover + strlen(value) + 1) < end) {
    strcpy(rover, value);
    rover += strlen(value);
  }
}

void XPK_SafeAppend(char * &rover, const char *end, const char value) {
  if ((rover + 2) < end) {
    char buffer2[2];
    buffer2[0] = value;
    buffer2[1] = 0;
    strcpy(rover, buffer2);
    rover += strlen(buffer2);
  }
}

/////////////////////////////////////////////////////////////////////

I32 XPK_StringToI32(const char *rover) {
  bool negative = false;
  I32 result = 0;

  if (*rover == '-') {
    negative = true;
    rover++;
  }

  while (*rover && *rover == '0')
  rover++;

  while (*rover && *rover >= '0' && *rover <= '9') {
    result *= 10;
    I32 v = *rover - '0';
    result += v;
    rover++;
  }

  if (negative) result = -result;

  return result;
}

I64 XPK_StringToI64(const char *rover) {
  bool negative = false;
  I64 result = 0;

  if (*rover == '-') {
    negative = true;
    rover++;
  }

  while (*rover && *rover == '0')
  rover++;

  while (*rover && *rover >= '0' && *rover <= '9') {
    result *= 10;
    I64 v = *rover - '0';
    result += v;
    rover++;
  }

  if (negative) result = -result;

  return result;
}

F64 XPK_StringToF64(const char *rover) {
  return atof(rover);
}

void XPK_F64ToString(char *buffer, F64 value) {
  //sprintf(rover, "%lf", value);

        I32 size = 32;

    int ret;
    char *start, *end;
    size_t length;

    ret = _snprintf(buffer, size, "%.17f", value);
    if(ret < 0)
        return;

    length = (size_t)ret;
    if(length >= size)
        return;

    /* Make sure there's a dot or 'e' in the output. Otherwise
       a real is converted to an integer when decoding */
    if(strchr(buffer, '.') == NULL &&
       strchr(buffer, 'e') == NULL)
    {
        if(length + 3 >= size) {
            /* No space to append ".0" */
            return;
        }
        buffer[length] = '.';
        buffer[length + 1] = '0';
        buffer[length + 2] = '\0';
        length += 2;
    }

    /* Remove leading '+' from positive exponent. Also remove leading
       zeros from exponents (added by some printf() implementations) */
    start = strchr(buffer, 'e');
    if(start) {
        start++;
        end = start + 1;

        if(*start == '-')
            start++;

        while(*end == '0')
            end++;

        if(end != start) {
            memmove(start, end, length - (size_t)(end - buffer));
            length -= (size_t)(end - start);
        }
    }

        buffer[length] = 0;
}

void XPK_F64ToString(char *rover, F64 value, I32 decimal) {
  if (decimal == 2) {
    sprintf(rover, "%.2f", value);
  } else if (decimal == 1) {
    sprintf(rover, "%.1f", value);
  } else {
    char conversion[32];
    sprintf(conversion, "%%.%if", decimal);
    sprintf(rover, conversion, value);
  }
}

void XPK_I64ToString(char *rover, I64 value) {
  bool negative = value < 0;

  if (negative) {
    value = -value;
    *rover++ = '-';
  }

  char stack[128];
  I32 stackTop = 0;

  do {
    stack[stackTop++] = (char)('0' + value % 10);
    value = value / 10;
  } while(value > 0);

  while (stackTop) {
    *rover++ = stack[--stackTop];
  }

  *rover = 0;
}

void XPK_I32ToString(char *rover, I32 value) {
  bool negative = value < 0;

  if (negative) {
    value = -value;
    *rover++ = '-';
  }

  char stack[128];
  I32 stackTop = 0;

  do {
    stack[stackTop++] = (char)('0' + value % 10);
    value = value / 10;
  } while(value > 0);

  while (stackTop) {
    *rover++ = stack[--stackTop];
  }

  *rover = 0;
}