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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
|
/**
* Yudit Unicode Editor Source File
*
* GNU Copyright (C) 2002 Gaspar Sinai <gsinai@yudit.org>
* GNU Copyright (C) 2001 Gaspar Sinai <gsinai@yudit.org>
* GNU Copyright (C) 2000 Gaspar Sinai <gsinai@yudit.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* dated June 1991. See file COPYYING for details.
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "ProcessFile.h"
#include "stoolkit/SBMap.h"
#include "stoolkit/SString.h"
#include "stoolkit/SIOStream.h"
#include "stoolkit/SExcept.h"
#include <string.h>
#include <stdlib.h>
/* sscanf */
#include <stdio.h>
#if HAVE_STRTOL==0
#error This module needs strtol.
#endif
// We will process lines with this length
#define LINE_LENGTH 1024
#define TOK_NAME "NAME="
#define TOK_COMM "COMM="
#define TOK_TYPE "TYPE="
#define TOK_ENCODE "ENCODE="
#define TOK_KEYW "KEY_WIDTH="
#define TOK_VALUEW "VALUE_WIDTH="
#define TOK_KEYL "KEY_LENGTH="
#define TOK_VALUEL "VALUE_LENGTH="
#define TOK_SECTION "SECTION="
#define TOK_MAX_SEGMENTS 5
// The maximum length of key and value.
#define TOK_STATE_SIZE 32
/**
* Prorcess a UNI file. Columns are separated by tabs or white spaces.
* @param filename is the name of the iput file.
* @param file is the input stream. It should be open, and it will not
* be closed at the end.
* @param keysize is 0 for 8bit and 1 for 16 bit keys
* @param hilo is 1 if we need to or local codes with 0x8080
* -1 if we need to and local codes with 0x7f7f.
* @param from is the column from which coding happen.
* @parem to is the column where the coded value is
* @param reverse is nonzero to show that a reverse map should be built
* If reverse has been specified the role changes, so the same
* from and to parameters result in a reverse (encoding) map.
* @param comment will hold the comments if any.
* @param name will be the name (if parsed)
* @param type will be the type (if parsed)
*/
SBMapItemVector*
processUNI (const char* filename, SInputStream& _fl, int nocomment, int keysize, int hilo, int from, int to, int reverse, char* comment, char*name, int* type)
{
int commentSize=0;
int len;
SBMapItemVector* retVle = new SBMapItemVector();
int i;
int empty;
int section=-1;
char line [LINE_LENGTH+1];
char* chr;
int lineNo =0;
int keyw=keysize;
int valuew=1;
int keyl=0;
int valuel=0;
char sectName [LINE_LENGTH+1];
char newLine [LINE_LENGTH+24];
int sectCommSize;
int posKey=0;
int posVle=0;
int parsedKey;
int parsedVle;
int lend;
int status;
strncpy (name, filename, 32);
int count = 0;
int pos;
*type = 1;
strcpy (sectName, name);
sectCommSize =0;
SStringVector ssl("\r\n,\n,\r");
SReader r(_fl, ssl);
while (true)
{
SString ss;
if (!r.read(&ss)) break;
if (ss.size() <= 0) break;
ssl.trim (&ss);
char* cstring = ss.cString();
strncpy (line, cstring, LINE_LENGTH);
lineNo++;
line[LINE_LENGTH] = 0;
len = strlen (line);
empty = 1;
pos = 0;
posKey=-1;
posVle=-1;
for (i=0; i<len; i++)
{
if (line[i] == '\n' || line[i] == '#') break;
if (line[i] == ' ' || line[i] == '\t') continue;
empty = 0;
if (i==0 || line[i-1] == ' ' || line[i-1] == '\t')
{
if (pos == from) posKey = i;
if (pos == to) posVle = i;
pos++;
}
}
lend=i;
// Get the header comment.
if (line[0]=='#')
{
len = strlen (line);
if (section==-1)
{
if (nocomment || len + commentSize +1 >= MAX_COMMENT_SIZE)
{
continue;
}
if (commentSize==0)
{
strcpy (comment, &line[1]);
}
else
{
strcat (comment, "\n");
strcat (comment, &line[1]);
}
commentSize += len;
}
continue;
}
if (empty) continue;
if (section == -1)
{
section =0;
if (reverse)
{
retVle->maps[section] = new SBMapItem (
reverse,
(const unsigned char*) sectName,
(const unsigned char*) newLine,
sectCommSize, valuew, keyw, valuel, keyl);
}
else
{
retVle->maps[section] = new SBMapItem (
reverse,
(const unsigned char*) sectName,
(const unsigned char*) newLine,
sectCommSize, keyw, valuew, keyl, valuel);
}
retVle->count++;
}
count++;
if (posKey==-1 || posVle==-1)
{
fprintf (stderr, "mytool: file %s at line %d has some errors.\n",
filename, lineNo);
continue;
}
if (line[posKey] == '0' &&
(line[posKey+1] == 'x' || line[posKey+1] == 'X'))
{
posKey++; posKey++;
}
if (line[posVle] == '0' &&
(line[posVle+1] == 'x' || line[posVle+1] == 'X'))
{
posVle++; posVle++;
}
parsedKey = strtol (&line[posKey], &chr, 16);
if (hilo==1)
{
parsedKey |= ((keysize==0) ? 0x80 : 0x8080);
}
if (hilo==-1)
{
parsedKey &= 0x7f7f;
}
parsedVle = strtol (&line[posVle], &chr, 16);
if ((keysize==0 && parsedKey > 255) || parsedKey > 0xffff ||
parsedKey < 0 || parsedVle > 0xffff || parsedVle < 0)
{
fprintf (stderr, "mytool: file %s at line %d has some errors - check -8 option!. key=%d, vle=%d\n",
filename, lineNo, parsedKey, parsedVle);
continue;
}
// Compose a new line - parsedKey parsedVle
if (keysize==0)
{
if (reverse)
{
sprintf (newLine, "%04X -> %02X ", parsedVle, parsedKey);
}
else
{
sprintf (newLine, "%02X -> %04X ", parsedKey, parsedVle);
}
}
else
{
if (reverse)
{
sprintf (newLine, "%04X -> %04X ", parsedVle, parsedKey);
}
else
{
sprintf (newLine, "%04X -> %04X ", parsedKey, parsedVle);
}
}
if (!reverse && line[lend]=='#' && !nocomment)
{
strcat (newLine, &line[lend]);
}
//cerr << "ADD: " << newLine << "\n";
status = retVle->maps[section]->addLine (
(const unsigned char*) newLine, strlen (newLine),
0);
if (status<0)
{
fprintf (stderr, "mytool: file %s at line %d has some errors.\n",
filename, lineNo);
continue;
}
}
return retVle;
}
/**
* Prorcess a MYS file. "local code -> unicode"
* @param filename is the name of the iput file.
* @param file is the input stream. It should be open, and it will not
* be closed at the end.
* @param reverse is nonzero to show that a reverse map should be built
* If reverse has been specified the role changes, so the same
* from and to parameters result in a reverse (encoding) map.
* @param comment will hold the comments if any.
* @param name will be the name (if parsed)
* @param type will be the type (if parsed)
*/
SBMapItemVector*
processMYS (const char* filename, SInputStream& _fl, int nocomment, int reverse, char* comment, char*name, int* type)
{
int commentSize=0;
int len=0;
SBMapItemVector* retVle = new SBMapItemVector();
int i=0;
int empty;
int section=-1;
int keyw=0;
int valuew=1;
int keyl=0;
int valuel=0;
int count=0;
char* chr=0;
int lineNo=0;
int status=0;
int encode=0;
char line [LINE_LENGTH+1];
char sectName [LINE_LENGTH+1];
char sectComm [LINE_LENGTH+1];
int sectCommSize=0;
SStringVector ssl("\r\n,\n,\r");
SReader r(_fl, ssl);
while (true)
{
SString ss;
if (!r.read(&ss)) break;
if (ss.size() <= 0) break;
ssl.trim (&ss);
char* cstring = ss.cString();
strncpy (line, cstring, LINE_LENGTH);
lineNo++;
line[LINE_LENGTH] = 0;
len = strlen (line);
empty = 1;
for (i=0; i<len; i++)
{
if (line[i] == '\n' || line[i] == '#') break;
if (line[i] != ' ' && line[i] != '\t') empty = 0;
}
if (empty) continue;
//
// Process the header
//
if (section == -1 && strncmp (TOK_NAME, line, strlen(TOK_NAME)) == 0)
{
strncpy (name, &line[strlen(TOK_NAME)], 32);
continue;
}
if (section == -1 && strncmp (TOK_COMM, line, strlen(TOK_COMM)) == 0)
{
if (nocomment || len + commentSize +1 >= MAX_COMMENT_SIZE)
{
continue;
}
if (commentSize==0)
{
strcpy (comment, &line[strlen(TOK_COMM)]);
}
else
{
strcat (comment, "\n");
strcat (comment, &line[strlen(TOK_COMM)]);
}
commentSize+=len-strlen(TOK_COMM) +1;
continue;
}
if (section == -1 && strncmp (TOK_TYPE, line, strlen(TOK_TYPE)) == 0)
{
*type = strtol (&line[strlen(TOK_TYPE)], &chr, 16);
continue;
}
//
// Process the MapItems here.
//
if (strncmp (TOK_SECTION, line, strlen(TOK_SECTION)) == 0)
{
strcpy (sectName, &line[strlen(TOK_SECTION)]);
keyw=0;
valuew=1;
keyl=0;
valuel=0;
encode = 0;
section++;
count=0;
sectCommSize=0;
continue;
}
if (section == -1)
{
fprintf (stderr, "mytool: file %s at line %d has some errors.\n",
filename, lineNo);
continue;
}
if (strncmp (TOK_COMM, line, strlen(TOK_COMM)) == 0)
{
if (nocomment || len + sectCommSize + 1 >= MAX_COMMENT_SIZE)
{
continue;
}
if (sectCommSize==0)
{
strcpy (sectComm, &line[strlen(TOK_COMM)]);
}
else
{
strcat (sectComm, "\n");
strcat (sectComm, &line[strlen(TOK_COMM)]);
}
sectCommSize+=len-strlen(TOK_COMM) +1;
continue;
}
if (strncmp (TOK_KEYW, line, strlen(TOK_KEYW)) == 0)
{
keyw = strtol (&line[strlen(TOK_KEYW)], &chr, 16);
if (count != 0 || keyw<0 || keyw>3)
{
fprintf (stderr, "mytool: file %s at line %d has some errors.\n",
filename, lineNo);
continue;
}
continue;
}
if (strncmp (TOK_VALUEW, line, strlen(TOK_VALUEW)) == 0)
{
valuew = strtol (&line[strlen(TOK_VALUEW)], &chr, 16);
if (count != 0 || valuew<0 || valuew>3)
{
fprintf (stderr, "mytool: file %s at line %d has some errors.\n",
filename, lineNo);
continue;
}
continue;
}
if (strncmp (TOK_KEYL, line, strlen(TOK_KEYL)) == 0)
{
keyl = strtol (&line[strlen(TOK_KEYL)], &chr, 16);
if (count != 0 || keyl<0 || keyl>3)
{
fprintf (stderr, "mytool: file %s at line %d has some errors.\n",
filename, lineNo);
continue;
}
continue;
}
if (strncmp (TOK_VALUEL, line, strlen(TOK_VALUEL)) == 0)
{
valuel = strtol (&line[strlen(TOK_VALUEL)], &chr, 16);
if (count != 0 || valuel<0 || valuel>3)
{
fprintf (stderr, "mytool: file %s at line %d has some errors.\n",
filename, lineNo);
continue;
}
continue;
}
if (strncmp (TOK_ENCODE, line, strlen(TOK_ENCODE)) == 0)
{
encode = strtol (&line[strlen(TOK_ENCODE)], &chr, 16);
if (count != 0 || encode<0 || encode>3)
{
fprintf (stderr, "mytool: file %s at line %d has some errors.\n",
filename, lineNo);
continue;
}
continue;
}
if (retVle->count >= MAX_NMAPS)
{
fprintf (stderr, "mytool: file %s has too many sections\n", filename);
delete retVle;
return 0;
}
// Finally
if (count == 0)
{
if (reverse) encode = ! encode;
if (reverse)
{
retVle->maps[section] = new SBMapItem (
encode,
(const unsigned char*) sectName,
(const unsigned char*) sectComm,
sectCommSize, valuew, keyw, valuel, keyl);
}
else
{
retVle->maps[section] = new SBMapItem (
encode,
(const unsigned char*) sectName,
(const unsigned char*) sectComm,
sectCommSize, keyw, valuew, keyl, valuel);
}
retVle->count++;
}
count++;
status = retVle->maps[section]->addLine (
(const unsigned char*) line, strlen (line),
reverse);
if (status<0)
{
fprintf (stderr, "mytool: file %s at line %d has some errors.\n",
filename, lineNo);
continue;
}
}
return retVle;
}
/**
* Prorcess a KMAP file. "lcoal code = unicode"
* This came from UInput.cpp making that code obselete.
* @param filename is the name of the iput file.
* @param file is the input stream. It should be open, and it will not
* be closed at the end.
* @param reverse is nonzero to show that a reverse map should be built
* If reverse has been specified the role changes, so the same
* from and to parameters result in a reverse (encoding) map.
* @param comment will hold the comments if any.
* @param name will be the name (if parsed)
* @param type will be the type (if parsed)
*/
SBMapItemVector*
processKMap (const char* filename, SInputStream& _fl, int nocomment, int reverse, char* comment, char*name, int* type)
{
char line [LINE_LENGTH+1];
char quoted [LINE_LENGTH+1];
int lineNo;
int error;
char *from, *to;
int iscomment;
int empty;
int index;
int i;
int currentSegment;
int segmentCount;
char segments[TOK_MAX_SEGMENTS+1][LINE_LENGTH+1];
char segmentId[LINE_LENGTH+1];
int startingComment;
int commentSize;
int len;
char* lineComment;
int lineCommentSize;
SBMapItemVector* retVle = 0;
char* segmentArray[TOK_MAX_SEGMENTS];
int getNewLine;
lineNo = 0;
error = 0;
iscomment = 0;
currentSegment = 0;
segmentCount = 0;
startingComment = 1;
commentSize = 0;
*type = 1;
strncpy (name, filename, 32);
SStringVector ssl("\r\n,\n,\r");
SReader r(_fl, ssl);
while (true)
{
SString ss;
if (!r.read(&ss)) break;
if (ss.size() <= 0) break;
ssl.trim (&ss);
char* cstring = ss.cString();
strncpy (line, cstring, LINE_LENGTH);
// stoolkit parser, only '//' commmenst are OK at the
// beginning of the line
line[LINE_LENGTH]=0;
lineNo++;
to = line;
from= line;
index = 0;
getNewLine = 0;
// Rewrite the buffer into the quoted array.
// All // comments at the beginning are copied to comment.
while (to[0] != 0)
{
if (iscomment)
{
if (to[0]=='*' && to[1] == '/')
{
to++;
iscomment = 0;
}
to++;
continue;
}
if (to[0]=='/' && to[1] == '*')
{
to++; to++;
iscomment = 1;
continue;
}
if (to[0]=='/' && to[1] == '/')
{
getNewLine = 1;
len = strlen (to);
if (startingComment)
{
if (nocomment || len + commentSize +1 > MAX_COMMENT_SIZE)
{
break;
}
if (commentSize==0)
{
strcpy (comment, &to[2]);
}
else
{
strcat (comment, "\n");
strcat (comment, &to[2]);
}
commentSize += len-2 +1;
}
break;
}
startingComment = 0;
if (from == line && to[0] != '"')
{
to++; continue;
}
if (from == line && to[0] == '"')
{
to++;
from = to;
continue;
}
if (to[0] == '\\')
{
switch (to[1])
{
case 't':
quoted[index++] = '\t';
to++;
break;
case '\\':
quoted[index++] = '\\';
to++;
break;
case '"':
quoted[index++] = '"';
to++;
break;
case '0':
case '1':
case '2':
case '3':
if (to[2] < '0' || to[2] >'7'
|| to[3] < '0' || to[3] > '7')
{
quoted[index++] = '\\';
break;
}
quoted[index++] = ((to[1]-'0') << 6)
+ ((to[2]-'0') << 3)
+ to[3]-'0';
to++; to++; to++;
break;
}
to++;
continue;
}
if (to[0] == '"')
{
break;
}
quoted[index++] = to[0];
to++;
}
if (getNewLine==1) continue;
if (index == 0)
{
if (from != line)
{
error=1;
break;
}
continue;
}
quoted[index] = 0;
// Ending comment
lineComment = 0;
lineCommentSize = 0;
while (to[0]!=0)
{
if (to[0]=='/' && to[1] == '/')
{
lineComment = &to[2];
lineCommentSize = strlen (lineComment);
break;
}
to++;
}
// Ugly but it works.
if (TOK_MAX_SEGMENTS != 5)
{
error=1 ;
break;
}
if (retVle == 0)
{
if (strstr (quoted, "=") == 0)
{
segmentCount = sscanf (quoted,
"%[^+]+%[^+]+%[^+]+%[^+]+%[^+]+%[^+]",
segments[0], segments[1],
segments[2], segments[3],
segments[4], segments[5]);
if (segmentCount==0 && segmentCount > 5)
{
error=1;
break;
}
segmentArray[0] = segments[0];
segmentArray[1] = segments[1];
segmentArray[2] = segments[2];
segmentArray[3] = segments[3];
segmentArray[4] = segments[4];
currentSegment = -1;
retVle = new SBMapItemVector(
(int) segmentCount,
(const char**) &segmentArray[0],
reverse);
CHECK_NEW (retVle);
continue;
}
else
{
currentSegment = 0;
segmentCount = 0;
segmentArray[0] = "Section1";
retVle = new SBMapItemVector(
(int)1, (const char**) &segmentArray[0],
reverse);
CHECK_NEW (retVle);
}
}
if (currentSegment==-1)
{
for (i=0; i<segmentCount; i++)
{
strcpy (segmentId, "begin ");
strcat (segmentId, segments[i]);
if (strcmp (quoted, segmentId)==0)
{
strcpy (segmentId, "end ");
strcat (segmentId, segments[i]);
currentSegment = i;
break;
}
}
if (currentSegment==-1)
{
error=1;
break;
}
continue;
}
if (segmentCount != 0)
{
if (strcmp (quoted, segmentId) == 0)
{
currentSegment=-1;
continue;
}
}
if (retVle->addKLine (currentSegment, quoted,
(const unsigned char*) lineComment, lineCommentSize, reverse)<0)
{
error=1;
break;
}
}
if (error)
{
fprintf (stderr, "mytool: error- bad syntax in file '%s' at line %d\n",
(filename==0?"unkown":filename), lineNo);
delete retVle;
return 0;
}
empty = 0;
for (i=0; i<segmentCount || i==0; i++)
{
if (retVle->maps[i]->codeSize==0)
{
if (segmentCount==0)
{
fprintf (stderr, "mytool: error- bad syntax in file '%s' at eof\n",
(filename==0?"unkown":filename));
}
else if (currentSegment!=-1)
{
fprintf (stderr, "mytool: bad syntax (missing end?) in file '%s' at eof\n",
(filename==0?"unkown":filename));
}
else
{
fprintf (stderr, "mytool: error- bad syntax in file '%s' at multipart '%s'\n",
(filename==0?"unkown":filename), segments[i]);
}
delete retVle;
return 0;
}
}
return retVle;
}
SBMapItemVector::SBMapItemVector()
{
int i;
for (i=0; i<MAX_NMAPS; i++)
{
maps[i] = 0;
}
count = 0;
}
SBMapItemVector::SBMapItemVector(int _size, const char* _names[], int _reverse)
{
int i;
for (i=0; i<MAX_NMAPS; i++)
{
maps[i] = 0;
}
count = _size;
for (i=0; i<count; i++)
{
if (_reverse)
{
maps [i] = new SBMapItem (
_reverse,
(const unsigned char*) _names[i],
(const unsigned char*) "",
0, 1, 0, 0, 0);
}
else
{
maps [i] = new SBMapItem (
_reverse,
(const unsigned char*) _names[i],
(const unsigned char*) "",
0, 0, 1, 0, 0);
}
}
}
SBMapItemVector::~SBMapItemVector()
{
int i;
for (i=0; i<count; i++)
{
if (maps[i]) delete maps[i];
}
}
/**
* Adds a new line in kline format of InputMap
* This came from UInput.cpp making that code obselete.
* @param _index is the section index
* @param _line is the quoted line
* @param _comment is the comment.
* @param _commentSize is the size of comment.
* @param _reverse is the reverse indicator, indicating encode map.
* @return negative on fail
*/
int
SBMapItemVector::addKLine (int _index, const char* _line, const unsigned char* _comment, int _commentSize, int _reverse)
{
const char *from;
const char *to;
char * toOut;
int kcIndex;
unsigned char key[TOK_STATE_SIZE+1];
unsigned char code[2*TOK_STATE_SIZE+2];
char kcSize[2];
int value;
int base;
to = _line;
kcIndex =0;
kcSize[0] = 0;
kcSize[1] = 0;
while (to[0] != 0)
{
from = to;
if (*from==' ')
{
to++; continue;
}
if (*from=='=')
{
to++; kcIndex=1; continue;
}
if (kcSize[kcIndex] >= TOK_STATE_SIZE)
{
fprintf (stderr, "error: kmap size exceeded %d .\n", TOK_STATE_SIZE);
return 0;
}
//
// adding number
//
if (to[0] >= '0' && to[0] <= '9')
{
base = 10;
if (to[0] == '0')
{
to++;
if (to[0] == 'x' || to[0] =='X')
{
base = 16;
to++;
}
else
{
base = 8;
}
}
value = (int) strtol ((const char*) to, &toOut, base);
to = toOut;
if (kcIndex==0)
{
key[kcSize[0]] = (unsigned char) value;
}
else
{
code[kcSize[1]*2] = (((SS_WORD32)value>>8)&0xff);
code[kcSize[1]*2+1] = value&0xff;
}
kcSize[kcIndex]++;
continue;
}
// straight
while (to[0] != 0)
{
if (to[0]==' ')
{
to++; break;
}
if (to[0]=='=') break;
if (kcSize[kcIndex] >= TOK_STATE_SIZE)
{
fprintf (stderr, "error: kmap size exceeded %d .\n", TOK_STATE_SIZE);
return 0;
}
value = to[0];
if (kcIndex==0)
{
key[kcSize[0]] = (unsigned char) value;
}
else
{
code[kcSize[1]*2] = (((SS_WORD32)value>>8)&0xff);
code[kcSize[1]*2+1] = value&0xff;
}
to++;
kcSize[kcIndex]++;
}
}
if (kcSize[1]==0)
{
fprintf (stderr, "error: kmap map is empty. \n");
return -1;
}
key[kcSize[0]] = 0;
code[kcSize[1]*2] = 0;
// Now you can see the limitation of kmaps. No matchsize!
if (_reverse)
{
// No comment.
return maps[_index]->add (code, 2 * kcSize[1],2 * kcSize[1],
key, kcSize[0], _comment, 0);
}
return maps[_index]->add (key, kcSize[0], kcSize[0],
code, 2 * kcSize[1], _comment, _commentSize);
}
|