home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1999 mARCH
/
PCWK3A99.iso
/
Linux
/
DDD331
/
DDD-3_1_.000
/
DDD-3_1_
/
ddd-3.1.1
/
ddd
/
cook.C
< prev
next >
Wrap
C/C++ Source or Header
|
1998-10-23
|
5KB
|
303 lines
// $Id: cook.C,v 1.13 1998/10/23 13:08:42 zeller Exp $
// Turn a string into C representation and back again
// Copyright (C) 1995 Technische Universitaet Braunschweig, Germany.
// Written by Andreas Zeller <zeller@ips.cs.tu-bs.de>.
//
// This file is part of the ICE Library.
//
// The ICE Library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// The ICE Library 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 Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with the ICE Library -- see the file COPYING.LIB.
// If not, write to the Free Software Foundation, Inc.,
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// ICE is the incremental configuration environment.
// For details, see the ICE World-Wide-Web page,
// `http://www.cs.tu-bs.de/softech/ice/',
// or send a mail to the ICE developers <ice@ips.cs.tu-bs.de>.
char cook_rcsid[] =
"$Id: cook.C,v 1.13 1998/10/23 13:08:42 zeller Exp $";
#ifdef __GNUG__
#pragma implementation
#endif
#include <ctype.h>
#include <strstream.h>
#include <iomanip.h>
#include "bool.h"
#include "cook.h"
// Transform RAW into C string
string _cook(const string& raw, bool for_postscript)
{
ostrstream cooked;
const char *raw_s = (char *)raw;
for (unsigned i = 0; i < raw.length(); i++)
{
char cc = raw_s[i];
unsigned char c = (unsigned char)cc;
switch(c)
{
case '\a':
cooked << "\\a";
break;
case '\b':
cooked << "\\b";
break;
#if 0 // This encoding is not ISO C
case '\033':
cooked << "\\e";
break;
#endif
case '\f':
cooked << "\\f";
break;
case '\n':
cooked << "\\n";
break;
case '\r':
cooked << "\\r";
break;
case '\t':
cooked << "\\t";
break;
case '\v':
cooked << "\\v";
break;
case '\0':
cooked << "\\0";
break;
case '(':
case ')':
if (!for_postscript)
goto standard;
goto quote;
quote:
case '\"':
case '\'':
case '\\':
cooked << "\\";
cooked << c;
break;
standard:
default:
if (isascii(c) && isprint(c))
cooked << c;
else
cooked << "\\" << oct << setw(3) << setfill('0') << int(c);
break;
}
}
return string(cooked);
}
// Return the digit represented by C
static int digit(char c)
{
switch(c)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
case 'c':
case 'C':
return 12;
case 'd':
case 'D':
return 13;
case 'e':
case 'E':
return 14;
case 'f':
case 'F':
return 15;
default:
return -1;
}
}
// Transform COOKED into C string
string uncook(const string& cooked)
{
ostrstream uncooked;
int n;
int count;
char *i = cooked;
while (*i != '\0')
{
if (*i == '\\')
{
switch (*++i)
{
case '\n':
i++;
break;
case 'a':
uncooked << '\a';
i++;
break;
case 'b':
uncooked << '\b';
i++;
break;
case 'e': // GNU C extension
uncooked << '\033';
i++;
break;
case 'f':
uncooked << '\f';
i++;
break;
case 'n':
uncooked << '\n';
i++;
break;
case 'r':
uncooked << '\r';
i++;
break;
case 't':
uncooked << '\t';
i++;
break;
case 'v':
uncooked << '\v';
i++;
break;
case '0':
if (*(i + 1) == 'x')
{
i++;
goto hex;
}
// FALL THROUGH
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
n = 0;
count = 0;
while (count++ < 3)
{
int d = digit(*i);
if (d < 0 || d >= 8)
break;
n = (n << 3) + d;
i++;
}
uncooked << char(n);
break;
hex:
case 'x':
n = 0;
count = 0;
i++;
while (count++ < 2)
{
int d = digit(*i);
if (d < 0 || d >= 16)
break;
n = (n << 4) + d;
i++;
}
uncooked << char(n);
break;
case '\"':
case '\'':
case '\\':
default:
uncooked << *i++;
break;
}
}
else
uncooked << *i++;
}
return uncooked;
}