translations.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00034 #ifdef LIBDARC_DEBUG
00035 # define TRANSLATIONS_H_INTERNAL
00036 #endif
00037
00038 #include "translations.h"
00039 #undef TRANSLATIONS_H_INTERNAL
00040 #include "user_interaction_specific.hpp"
00041 #if LIBDARC_HAVE_LIBDAR_HPP
00042 #include <dar/libdar.hpp>
00043 #endif
00044
00045 #if HAVE_LIMITS_H
00046 #include <limits.h>
00047 #endif
00048
00049 #ifdef LIBDARC_DEBUG
00050 char break_buffer[30];
00051 #endif
00052
00053 NAMESPACE_LIBDAR_START
00054
00055
00056
00057
00058 class byte_stream : public generic_file
00059 {
00060 public:
00061 const static size_t ALLOCATE_SIZE = 16;
00062
00063
00064 byte_stream(user_interaction & dialog, char* source)
00065 : generic_file( dialog, gf_read_only )
00066 {
00067 buffer = source;
00068 position = 0;
00069 buf_size = 0;
00070 }
00071
00072 byte_stream(user_interaction & dialog)
00073 : generic_file( dialog, gf_write_only )
00074 {
00075 position = length = 0;
00076 buf_size = ALLOCATE_SIZE;
00077 buffer = (char*)malloc(ALLOCATE_SIZE);
00078 if (buffer == NULL)
00079 throw Ememory("could not allocate string buffer");
00080 }
00081
00082 ~byte_stream()
00083 {
00084 if (buf_size != 0)
00085 free(buffer);
00086 }
00087
00088 char* get_string(U_I* store_size)
00089 {
00090 if (store_size != NULL)
00091 *store_size = length;
00092 char* ret = (char*)malloc(length);
00093 if (ret == NULL)
00094 return NULL;
00095 memcpy(ret, buffer, length);
00096
00097
00098
00099 return ret;
00100 }
00101
00102 bool skip(const infinint & pos)
00103 {
00104 int tmp = 0;
00105 infinint t(pos);
00106 t.unstack(tmp);
00107
00108 position += tmp;
00109 return true;
00110 }
00111
00112 bool skip_to_eof()
00113 {
00114
00115 position = length;
00116 return true;
00117 }
00118
00119 bool skip_relative(S_I x)
00120 {
00121
00122 if (x > 0)
00123 position += x;
00124 else
00125 position -= (unsigned int)(-1 * x);
00126 return true;
00127 }
00128
00129 infinint get_position()
00130 {
00131 return position;
00132 }
00133
00134 S_I inherited_read(char* a, size_t size)
00135 {
00136
00137 size_t i = 0;
00138
00139
00140
00141 for(i = 0; i < size; i++)
00142 *(a+i) = *(buffer + position + i);
00143
00144
00145
00146 position += size;
00147
00148 return (S_I)size;
00149 }
00150
00151
00152
00153 S_I inherited_write(char* a, size_t size)
00154 {
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 if (size > buf_size - position)
00165 {
00166
00167 buffer = (char*)realloc(buffer, buf_size + size + ALLOCATE_SIZE);
00168
00169 if (buffer == NULL)
00170 throw Ememory("could not reallocate string buffer");
00171
00172 buf_size += size + ALLOCATE_SIZE;
00173
00174
00175 memcpy((buffer+position), a, size);
00176 position += size;
00177 length += size;
00178
00179 } else {
00180
00181
00182 memcpy((buffer+position), a, size);
00183 position += size;
00184 length += size;
00185
00186 }
00187
00188
00189
00190
00191
00192
00193
00194 return size;
00195 }
00196
00197
00198 private:
00199 size_t buf_size;
00200 size_t length;
00201 size_t position;
00202 char* buffer;
00203 };
00204
00205
00206
00207
00208
00209
00210
00211
00212 extern "C" void dar_infinint_destroy(dar_infinint* ref)
00213 {
00214 infinint* tmp = reinterpret_cast<infinint*>( ref );
00215 delete tmp;
00216 return;
00217 }
00218
00219 extern "C" dar_infinint *dar_infinint_copy(dar_infinint* ref)
00220 {
00221 infinint* tmp = reinterpret_cast<infinint*>(ref);
00222 infinint* ret = new infinint( *tmp );
00223 return (dar_infinint*)ret;
00224 }
00225
00226
00227 extern "C" void dar_infinint_add( dar_infinint** a, dar_infinint* b )
00228 {
00229 infinint* ar = reinterpret_cast<infinint*>(*a);
00230 infinint* br = reinterpret_cast<infinint*>( b);
00231 *ar += *br;
00232
00233 }
00234
00235 extern "C" void dar_infinint_sub( dar_infinint** a, dar_infinint* b )
00236 {
00237 infinint* ar = (infinint*)*a;
00238 infinint* br = (infinint*)b;
00239 *ar -= *br;
00240
00241 }
00242
00243 extern "C" void dar_infinint_mult( dar_infinint** a, dar_infinint* b )
00244 {
00245 infinint* ar = (infinint*)*a;
00246 infinint* br = (infinint*)b;
00247 *ar *= *br;
00248
00249 }
00250
00251 extern "C" void dar_infinint_div( dar_infinint** a, dar_infinint* b )
00252 {
00253 infinint* ar = (infinint*)*a;
00254 infinint* br = (infinint*)b;
00255 *ar /= *br;
00256
00257 }
00258
00259 extern "C" void dar_infinint_pow( dar_infinint** a, dar_infinint* b )
00260 {
00261 infinint* ar = (infinint*)*a;
00262 infinint* br = (infinint*)b;
00263 *ar = ar->power(*br);
00264
00265 }
00266
00267 extern "C" void dar_infinint_powi( dar_infinint** a, int exp )
00268 {
00269 infinint* ar = (infinint*)*a;
00270 *ar = ar->power(exp);
00271
00272 }
00273
00274 extern "C" void dar_infinint_shiftl( dar_infinint** a, dar_infinint* b )
00275 {
00276 infinint* ar = (infinint*)*a;
00277
00278
00279
00280
00281 infinint* br = (infinint*)b;
00282
00283
00284
00285
00286 *ar <<= *br;
00287
00288
00289
00290
00291
00292 }
00293
00294 extern "C" void dar_infinint_shiftli( dar_infinint** a, int bit )
00295 {
00296 infinint* ar = (infinint*)*a;
00297
00298
00299
00300
00301 *ar <<= bit;
00302
00303
00304
00305
00306
00307 }
00308
00309 extern "C" void dar_infinint_shiftr( dar_infinint** a, dar_infinint* b )
00310 {
00311 infinint* ar = (infinint*)*a;
00312 infinint* br = (infinint*)b;
00313 *ar >>= *br;
00314
00315 }
00316
00317 extern "C" void dar_infinint_shiftri( dar_infinint** a, int bit )
00318 {
00319 infinint* ar = (infinint*)*a;
00320 *ar >>= bit;
00321
00322 }
00323
00324 extern "C" void dar_infinint_mod( dar_infinint** a, dar_infinint* b )
00325 {
00326 infinint* ar = reinterpret_cast<infinint*>(*a);
00327 infinint* br = reinterpret_cast<infinint*>( b);
00328 *ar %= *br;
00329
00330 }
00331
00332 extern "C" void dar_infinint_increment( dar_infinint** a )
00333 {
00334 infinint* ar = (infinint*)*a;
00335
00336
00337
00338
00339 (*ar)++;
00340
00341
00342
00343
00344
00345 }
00346
00347 extern "C" void dar_infinint_decrement( dar_infinint** a )
00348 {
00349 infinint* ar = (infinint*)*a;
00350 (*ar)--;
00351
00352 }
00353
00354 extern "C" void dar_infinint_euclide( dar_infinint* a, dar_infinint* b,
00355 dar_infinint** q, dar_infinint** r )
00356 {
00357 infinint* ar = (infinint*)a;
00358 infinint* br = (infinint*)b;
00359 infinint* qr = (infinint*)*q;
00360 infinint* rr = (infinint*)*r;
00361 *qr = *ar / *br;
00362 *rr = *ar % *br;
00363 }
00364
00365
00366 extern "C" bool dar_infinint_comp_eq( dar_infinint* a, dar_infinint* b )
00367 {
00368 infinint* ar = (infinint*)a;
00369 infinint* br = (infinint*)b;
00370 return *ar == *br;
00371 }
00372
00373 extern "C" bool dar_infinint_comp_leq( dar_infinint* a, dar_infinint* b )
00374 {
00375 infinint* ar = (infinint*)a;
00376 infinint* br = (infinint*)b;
00377 return *ar <= *br;
00378 }
00379
00380 extern "C" bool dar_infinint_comp_l( dar_infinint* a, dar_infinint* b )
00381 {
00382 infinint* ar = (infinint*)a;
00383 infinint* br = (infinint*)b;
00384 return *ar < *br;
00385 }
00386
00387 extern "C" bool dar_infinint_comp_geq( dar_infinint* a, dar_infinint* b )
00388 {
00389 infinint* ar = (infinint*)a;
00390 infinint* br = (infinint*)b;
00391 return *ar >= *br;
00392 }
00393
00394 extern "C" bool dar_infinint_comp_g( dar_infinint* a, dar_infinint* b )
00395 {
00396 infinint* ar = (infinint*)a;
00397 infinint* br = (infinint*)b;
00398 return *ar > *br;
00399 }
00400
00401 extern "C" bool dar_infinint_comp_neq( dar_infinint* a, dar_infinint* b )
00402 {
00403 infinint* ar = (infinint*)a;
00404 infinint* br = (infinint*)b;
00405 return *ar != *br;
00406 }
00407
00408
00409
00410 extern "C" dar_infinint *dar_infpi(int num)
00411 {
00412 infinint* ret = new infinint(num);
00413 return (dar_infinint*)ret;
00414 }
00415
00416 #if LIBDARC_OS_BITS == 64
00417 extern "C" dar_infinint *dar_infi(U_64 num)
00418 {
00419 infinint* ret = new infinint(num);
00420 return (dar_infinint*)ret;
00421 }
00422
00423 #else
00424 extern "C" dar_infinint *dar_infi(U_32 num)
00425 {
00426 infinint* ret = new infinint(num);
00427 return (dar_infinint*)ret;
00428 }
00429
00430 #endif
00431
00432 extern "C" dar_infinint *dar_infu32(U_32 num)
00433 {
00434 infinint* ret = new infinint(num);
00435 return (dar_infinint*)ret;
00436 }
00437
00438 extern "C" dar_infinint *dar_infu64(U_64 num)
00439 {
00440 infinint* ret = new infinint(num);
00441 return (dar_infinint*)ret;
00442 }
00443
00444
00445 extern "C" dar_infinint *dar_infds(char* num)
00446 {
00447 try {
00448 std::string str(num);
00449 deci d(str);
00450 infinint* ret = new infinint();
00451 *ret = d.computer();
00452 return (dar_infinint*)ret;
00453 } catch (...)
00454 {
00455 return NULL;
00456 }
00457 }
00458
00459 extern "C" dar_infinint *dar_infhs(char* num)
00460 {
00461
00462 infinint* ret = new infinint();
00463 return (dar_infinint*)ret;
00464 }
00465
00466 extern "C" dar_infinint *dar_infbin(U_8* num)
00467 {
00468 user_interaction_null* ut = new user_interaction_null();
00469 byte_stream stream( *ut, (char*)num );
00470 infinint* ret = new infinint();
00471 try
00472 {
00473 ret->read( stream );
00474 } catch (...) {
00475 delete ut;
00476 delete ret;
00477 return NULL;
00478 }
00479
00480
00481
00482
00483 delete ut;
00484 return (dar_infinint*)ret;
00485 }
00486
00487
00488 extern "C" int dar_piinf (bool* overflow, dar_infinint* box)
00489 {
00490 infinint* rb = (infinint*)box;
00491 infinint tmp(*rb);
00492 int ret = 0;
00493 if (overflow != NULL)
00494 {
00495 if ( tmp >= UINT_MAX )
00496 *overflow = true;
00497 else
00498 *overflow = false;
00499 }
00500 tmp.unstack(ret);
00501 return ret;
00502 }
00503
00504
00505 #if LIBDARC_OS_BITS == 64
00506 extern "C" U_64 dar_iinf (bool* overflow, dar_infinint* box)
00507 {
00508 infinint* rb = (infinint*)box;
00509 infinint tmp(*rb);
00510 U_64 ret = 0;
00511 if (overflow != NULL)
00512 {
00513 if ( tmp >= U_64_MAX )
00514 *overflow = true;
00515 else
00516 *overflow = false;
00517 }
00518 tmp.unstack(ret);
00519 return ret;
00520 }
00521
00522 #else
00523 extern "C" U_32 dar_iinf (bool* overflow, dar_infinint* box)
00524 {
00525 infinint* rb = (infinint*)box;
00526 infinint tmp(*rb);
00527 U_32 ret = 0;
00528 if (overflow != NULL)
00529 {
00530 if ( tmp >= U_32_MAX )
00531 *overflow = true;
00532 else
00533 *overflow = false;
00534 }
00535 tmp.unstack(ret);
00536 return ret;
00537 }
00538
00539 #endif
00540
00541 extern "C" U_32 dar_u32inf (bool* overflow, dar_infinint* box)
00542 {
00543 infinint* rb = (infinint*)box;
00544 infinint tmp(*rb);
00545 U_32 ret = 0;
00546 if (overflow != NULL)
00547 {
00548 if ( tmp >= U_32_MAX )
00549 *overflow = true;
00550 else
00551 *overflow = false;
00552 }
00553 tmp.unstack(ret);
00554 return ret;
00555 }
00556
00557 extern "C" U_64 dar_u64inf (bool* overflow, dar_infinint* box)
00558 {
00559 infinint* rb = (infinint*)box;
00560 infinint tmp(*rb);
00561 U_64 ret = 0;
00562 if (overflow != NULL)
00563 {
00564 if ( tmp >= U_64_MAX )
00565 *overflow = true;
00566 else
00567 *overflow = false;
00568 }
00569 tmp.unstack(ret);
00570 return ret;
00571 }
00572
00573 extern "C" char* dar_dsinf (U_I* length, dar_infinint* box)
00574 {
00575 infinint* tmp = (infinint*)box;
00576 deci d(*tmp);
00577 std::string str(d.human());
00578 char* ret = (char*)malloc(str.length() + 1);
00579 if (ret == NULL) { return NULL; }
00580 strcpy(ret, str.c_str());
00581 if (length != NULL)
00582 *length = strlen(str.c_str());
00583 return ret;
00584 }
00585
00586 extern "C" char* dar_hsinf (U_I* length, dar_infinint* box)
00587 {
00588
00589 char* ret = (char*)malloc(5);
00590 if (ret == NULL) { return NULL; }
00591 if (length != NULL)
00592 *length = 0;
00593 return ret;
00594 }
00595
00596 extern "C" U_8* dar_bininf (U_I* length, dar_infinint* box)
00597 {
00598 user_interaction_null* ut = new user_interaction_null();
00599 infinint* b = reinterpret_cast<infinint*>(box);
00600 byte_stream stream( *ut );
00601 try {
00602 b->dump( stream );
00603 } catch ( Egeneric & e ) {
00604
00605 return NULL;
00606 }
00607 delete ut;
00608 return (U_8*)stream.get_string(length);
00609 }
00610
00611
00612
00613
00614 NAMESPACE_LIBDAR_END
00615