| 1 | // |
|---|
| 2 | // TxWin, Textmode Windowing Library |
|---|
| 3 | // |
|---|
| 4 | // Original code Copyright (c) 1995-2005 Fsys Software and Jan van Wijk |
|---|
| 5 | // |
|---|
| 6 | // ========================================================================== |
|---|
| 7 | // |
|---|
| 8 | // This file contains Original Code and/or Modifications of Original Code as |
|---|
| 9 | // defined in and that are subject to the GNU Lesser General Public License. |
|---|
| 10 | // You may not use this file except in compliance with the License. |
|---|
| 11 | // BY USING THIS FILE YOU AGREE TO ALL TERMS AND CONDITIONS OF THE LICENSE. |
|---|
| 12 | // A copy of the License is provided with the Original Code and Modifications, |
|---|
| 13 | // and is also available at http://www.dfsee.com/txwin/lgpl.htm |
|---|
| 14 | // |
|---|
| 15 | // This library is free software; you can redistribute it and/or modify |
|---|
| 16 | // it under the terms of the GNU Lesser General Public License as published |
|---|
| 17 | // by the Free Software Foundation; either version 2.1 of the License, |
|---|
| 18 | // or (at your option) any later version. |
|---|
| 19 | // |
|---|
| 20 | // This library is distributed in the hope that it will be useful, |
|---|
| 21 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 22 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|---|
| 23 | // See the GNU Lesser General Public License for more details. |
|---|
| 24 | // |
|---|
| 25 | // You should have received a copy of the GNU Lesser General Public License |
|---|
| 26 | // along with this library; (lgpl.htm) if not, write to the Free Software |
|---|
| 27 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 28 | // |
|---|
| 29 | // Questions on TxWin licensing can be directed to: txwin@fsys.nl |
|---|
| 30 | // |
|---|
| 31 | // ========================================================================== |
|---|
| 32 | // |
|---|
| 33 | // TX formatting function for mixed ASCII/HEX strings |
|---|
| 34 | // file-logging facilities |
|---|
| 35 | // |
|---|
| 36 | // Author: J. van Wijk |
|---|
| 37 | // |
|---|
| 38 | // JvW 24-07-2005 Initial version, split off from TXCON.C |
|---|
| 39 | |
|---|
| 40 | #include <txlib.h> |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | #define ST_SKIPW 0 // skip whitespace |
|---|
| 44 | #define ST_HEX_0 1 // first HEX digit |
|---|
| 45 | #define ST_HEX_1 2 // second HEX digit |
|---|
| 46 | #define ST_ASCII 3 // copy plain ASCII |
|---|
| 47 | #define ST_UNICO 4 // generate UNICODE from ASCII |
|---|
| 48 | /*****************************************************************************/ |
|---|
| 49 | // Translate Mixed Ascii / Hex string specification to length + buffer |
|---|
| 50 | /*****************************************************************************/ |
|---|
| 51 | USHORT TxFormatMixedStr // RET length of buffer |
|---|
| 52 | ( |
|---|
| 53 | char *data, // IN mixed string |
|---|
| 54 | char *buf // OUT formatted buffer |
|---|
| 55 | ) |
|---|
| 56 | { |
|---|
| 57 | char *s; |
|---|
| 58 | int c; |
|---|
| 59 | USHORT i; |
|---|
| 60 | char hex[3]; |
|---|
| 61 | int state = ST_SKIPW; // parsing state, ascii/hex |
|---|
| 62 | |
|---|
| 63 | hex[2] = '\0'; |
|---|
| 64 | for (s=data, i=0; *s; s++) |
|---|
| 65 | { |
|---|
| 66 | // TxPrint("\nParse state %u, char: '%c' for buffer pos: %u", state, *s, i); |
|---|
| 67 | switch (state) |
|---|
| 68 | { |
|---|
| 69 | case ST_SKIPW: |
|---|
| 70 | switch (*s) |
|---|
| 71 | { |
|---|
| 72 | case TXk_SPACE: |
|---|
| 73 | break; |
|---|
| 74 | |
|---|
| 75 | default: |
|---|
| 76 | s--; // evaluate again in new state |
|---|
| 77 | state = ST_ASCII; |
|---|
| 78 | break; |
|---|
| 79 | } |
|---|
| 80 | break; |
|---|
| 81 | |
|---|
| 82 | case ST_HEX_0: |
|---|
| 83 | switch (*s) |
|---|
| 84 | { |
|---|
| 85 | case '\'': |
|---|
| 86 | state = ST_ASCII; |
|---|
| 87 | break; |
|---|
| 88 | |
|---|
| 89 | case '0': case '1': case '2': case '3': case '4': |
|---|
| 90 | case '5': case '6': case '7': case '8': case '9': |
|---|
| 91 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': |
|---|
| 92 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': |
|---|
| 93 | hex[0] = *s; |
|---|
| 94 | state = ST_HEX_1; |
|---|
| 95 | break; |
|---|
| 96 | |
|---|
| 97 | case TXk_SPACE: |
|---|
| 98 | break; |
|---|
| 99 | |
|---|
| 100 | default: |
|---|
| 101 | TxPrint("\nInvalid hex char in mixed string at char: %c", *s); |
|---|
| 102 | break; |
|---|
| 103 | } |
|---|
| 104 | break; |
|---|
| 105 | |
|---|
| 106 | case ST_HEX_1: |
|---|
| 107 | switch (*s) |
|---|
| 108 | { |
|---|
| 109 | case '\'': |
|---|
| 110 | TxPrint("\nSingle hex char in mixed string at char: %c", *s); |
|---|
| 111 | state = ST_ASCII; // error, single HEX char |
|---|
| 112 | break; |
|---|
| 113 | |
|---|
| 114 | case '0': case '1': case '2': case '3': case '4': |
|---|
| 115 | case '5': case '6': case '7': case '8': case '9': |
|---|
| 116 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': |
|---|
| 117 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': |
|---|
| 118 | hex[1] = *s; |
|---|
| 119 | if (sscanf( hex, "%X", &c) != 0) |
|---|
| 120 | { |
|---|
| 121 | buf[i++] = (char) c; |
|---|
| 122 | } |
|---|
| 123 | state = ST_HEX_0; |
|---|
| 124 | break; |
|---|
| 125 | |
|---|
| 126 | case TXk_SPACE: |
|---|
| 127 | break; |
|---|
| 128 | |
|---|
| 129 | default: |
|---|
| 130 | TxPrint("\nInvalid hex char in mixed string at char: %c", *s); |
|---|
| 131 | break; |
|---|
| 132 | } |
|---|
| 133 | break; |
|---|
| 134 | |
|---|
| 135 | case ST_UNICO: |
|---|
| 136 | switch (*s) |
|---|
| 137 | { |
|---|
| 138 | case '"': |
|---|
| 139 | state = ST_ASCII; |
|---|
| 140 | break; |
|---|
| 141 | |
|---|
| 142 | default: |
|---|
| 143 | buf[i++] = *s; |
|---|
| 144 | buf[i++] = 0; |
|---|
| 145 | break; |
|---|
| 146 | } |
|---|
| 147 | break; |
|---|
| 148 | |
|---|
| 149 | default: |
|---|
| 150 | switch (*s) |
|---|
| 151 | { |
|---|
| 152 | case '\'': // to HEX |
|---|
| 153 | state = ST_HEX_0; |
|---|
| 154 | break; |
|---|
| 155 | |
|---|
| 156 | case '"': // to UNICODE |
|---|
| 157 | state = ST_UNICO; |
|---|
| 158 | break; |
|---|
| 159 | |
|---|
| 160 | default: // just copy ASCII |
|---|
| 161 | buf[i++] = *s; |
|---|
| 162 | break; |
|---|
| 163 | } |
|---|
| 164 | break; |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | return (i); |
|---|
| 168 | } // end 'TxFormatMixedStr' |
|---|
| 169 | /*---------------------------------------------------------------------------*/ |
|---|