/* librock/text/decbe.c librock_CHISEL _summary Process strings with C-style backslash escapes Table of Contents. [License and Disclaimer of Warranty] [License Attributes] [How to Reuse this software] [Definitions and header file inclusions] [Function Implementation and inline documentation] [Revision summary and Changelog] */ /*************************************************************/ /***[License and Disclaimer of Warranty]**********************/ /* By copyright law, you must agree to these terms before you modify or copy this software or any part of it. Copyright (c) 2004, Forrest J. Cavalier III Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************/ /***[License Attributes]**************************************/ /* The librock LIDESC tool reports some license attributes. It reports the following for the MIT license used for this software: A notice must be included in documentation or other materials when a binary is distributed. (librock_DOCNOTE_1C_WITHBIN) Notices must remain in source code. (librock_SRCNOTE_1) The Free Software Foundation says this license is a Free software license. (librock_LIDESC_COMBINE_FSF_OK) The Free Software Foundation says this license is GPL compatible. (librock_LIDESC_COMBINE_GPL_OK) The Open Source Initiative approved this license as OSD compliant. (librock_LIDESC_COMBINE_OSD_OK) Note: You may be able to use LIDESC tools to check compatibility with other licenses. */ /*************************************************************/ /***[How to Reuse this software]******************************/ /* 1. Agree to the license and disclaimer of warranty. 2. Read this file for function documentation and examples. Verify the implementation is suitable for you. 3. Define preprocessor symbols for your machine and compilation environment. This file is originally from libROCK: the Reusable Opensource Code and Knowledge library. For an automated update and build system, license certificates, and more, see http://www.mibsoftware.com/librock/ This file is written for ANSI C: librock_CHISEL _portreport_permit_ansi Some comments used for CHISEL portreport suppression (!empty) (!=0). */ /*************************************************************/ /***[Definitions and header file inclusions]******************/ #ifndef librock_ISOLATED /**************************************************************/ #define librock_IMPLEMENT_decbe #define librock_PTR #define librock_CONST const #include #include /**************************************************************/ #endif #ifdef librock_IMPL_LIDESC /**************************************************************/ #include /* librock_LIDESC_HC=cc598307414a9997b32b60a2e7a8e7c6a13d6438 */ void *librock_LIDESC_decbe[] = { "\n" __FILE__ librock_LIDESC_mit "\n", 0 }; /**************************************************************/ #endif #ifndef librock_WRAP /**************************************************************/ #define librock_body_sscharbe librock_sscharbe #define librock_body_decodeCBE librock_decodeCBE /**************************************************************/ #endif #ifndef librock_NOIMPL_sscharbe #define librock_FNTYPE_sscharbe fni_s_C /**************************************************************/ int librock_body_sscharbe(librock_CONST char librock_PTR *pStart,char librock_PTR *pRet) { /* Translate C backslash escape sequences */ /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_sscharbe /* librock_sscharbe - translate C backslash escape sequences */ /**/ #include int librock_sscharbe( const char *pStart, char *pRet ); /**/ /* Translate C backslash escape sequences \f \t \n \ooo \r \b \xXX \\ \0 Other characters and those which follow a backslash character are stored at non-NULL pRet without translation. Returns number of characters processed or 0 if *pStart == '\0' Typical use: */ #ifdef librock_TYPICAL_USE_sscharbe const char *ptr = "Try\x20this\040test.\n"; while(*ptr) { char ch; ptr += librock_sscharbe(ptr,&ch); printf("%c",ch); } #endif /* // [No external calls.] */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=cc598307414a9997b32b60a2e7a8e7c6a13d6438 */ #endif /* MANUAL */ /*-------------------------------------------*/ unsigned char ch; librock_CONST char librock_PTR *sptr; if (*pStart != '\\') { if (pRet) { *pRet = *pStart; } if (*pStart) { return 1; } return 0; } /* Got a backslash */ sptr = pStart; sptr++; if (*sptr == 't') { ch = 0x09; sptr++; } else if (*sptr == 'n') { ch = 0x0a; sptr++; } else if (*sptr == 'f') { /* 10-3-95 */ ch = '\f'; sptr++; } else if (*sptr == 'r') { ch = 0x0d; sptr++; } else if (*sptr == 'b') { ch = 0x08; sptr++; } else if (*sptr == 'x') { /* Specify in hex */ sptr++; if ((*sptr >= '0')&&(*sptr <= '9')) { ch = *sptr - '0'; } else { ch = (*sptr & 0x1f) + 9; } ch <<= 4; sptr++; if ((*sptr >= '0')&&(*sptr <= '9')) { ch += *sptr - '0'; } else { ch += (*sptr & 0x1f) + 9; } sptr++; } else if ((*sptr >= '0')&&(*sptr <= '9')) { /* An octal sequence */ ch = *sptr - '0'; sptr++; if ((*sptr >= '0')&&(*sptr <= '9')) { ch *= 8; ch += *sptr - '0'; sptr++; } if ((*sptr >= '0')&&(*sptr <= '9')) { ch *= 8; ch += *sptr - '0'; sptr++; } } else { /* Unknown. Send as literal */ ch = *sptr++; } *pRet = ch; return sptr - pStart; } /* librock_sscharbe */ /****************************************************************/ #endif /* NOIMP */ #ifndef librock_NOIMPL_decodeCBE /**************************************************************/ #define librock_FNTYPE_decodeCBE fns_s_I char librock_PTR *librock_body_decodeCBE(char librock_PTR *pszBackslashEscaped,int librock_PTR *pcb) { /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_decodeCBE /* librock_decodeCBE - Translate C backslash escape sequences in a string. */ /**/ #include char * librock_decodeCBE( char *pszBackslashEscaped, /* NUL terminated string */ int *pcb ); /* Walk a string, translating backslash escapes of the forms: \f \t \n \r \b \xXX \\ \0 \0nnn and storing the result in the same buffer. Because of the properties of the encoding, the result will fit. Other characters which follow a backslash character are stored literally (but the backslash itself is not.) If (pcb), the number of characters in the translated string is stored at *pcb. (This allows \0 to be used within the string. Typical use is to allow a parameter (command line or configuration) to specify characters which include special characters. */ #ifdef librock_TYPICAL_USE_decodeCBE char *ptr = "Try\x20this\040test.\n"; librock_decodeCBE(ptr,0); printf("%s",ptr); #endif /* librock_sscharbe() */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Open-source under MIT license. NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=cc598307414a9997b32b60a2e7a8e7c6a13d6438 */ /*SEE ALSO*/ /*Reference: librock_sscharbe */ #endif /* MANUAL */ /*-------------------------------------------*/ /* We can work in place, since we know dptr <= sptr always. */ char *sptr = pszBackslashEscaped; char *dptr = sptr; char ch; while(*sptr) { sptr += librock_sscharbe(sptr,&ch); *dptr++ = ch; } *dptr = '\0'; if (pcb) { *pcb = dptr - pszBackslashEscaped; } return pszBackslashEscaped; } /* librock_decodeCBE */ /**************************************************************/ #endif /* NOIMP */ /* $Log: decbe.c,v $ Revision 1.7 2002/08/01 15:05:30 forrest@mibsoftware.com rights=#1 Updated TYPICAL_USE for decodeCBE. Revision 1.6 2002/08/01 15:00:49 forrest@mibsoftware.com rights=#1 Update TYPICAL_USE section. Added NOTLIBROCK section. Moved CVS log to end. Changed LIDESC MD5 to HC. Revision 1.5 2002/04/09 03:27:59 forrest@mibsoftware.com rights=#1 FNTYPE. Update LICENSE in manual. Revision 1.4 2002/04/06 04:00:41 forrest@mibsoftware.com rights=#1 Add FNTYPE decls for regression tests. Revision 1.3 2002/02/05 21:48:45 forrest@mibsoftware.com rights=#1 Standardized chg log Revision 1.2 2002/01/30 16:08:30 forrest@mibsoftware.com rights=#1 Renamed some .h files Revision 1.1 2002/01/29 14:25:27 forrest@mibsoftware.com rights=#1 initial rights#1 Copyright (c) Forrest J Cavalier III d-b-a Mib Software rights#1 License text in librock_LIDESC_HC=cc598307414a9997b32b60a2e7a8e7c6a13d6438 */