home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / libgimp / gimputils.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-01  |  4.9 KB  |  200 lines

  1. /* LIBGIMP - The GIMP Library 
  2.  * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
  3.  * Copyright (C) 2000 Sven Neumann <sven@gimp.org>
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Lesser General Public
  7.  * License as published by the Free Software Foundation; either
  8.  * version 2 of the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * Lesser General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Lesser General Public
  16.  * License along with this library; if not, write to the
  17.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18.  * Boston, MA 02111-1307, USA.
  19.  */
  20.  
  21. #include "config.h"
  22.  
  23. #include <string.h>
  24. #include <glib.h>
  25.  
  26. #include "gimputils.h"
  27.  
  28. /**
  29.  * gimp_strescape:
  30.  * @source: A string to escape special characters in.
  31.  * @exceptions: A string holding characters not to be escaped.
  32.  * 
  33.  * Escapes special characters in a string in the same way as in the
  34.  * C language, i.e. either with one of the sequences \b, \f, \n, \r,
  35.  * \t, \\, \", or as a three-digit octal escape sequence \nnn.
  36.  *
  37.  * If the list of exceptions is NULL, all ASCII control characters,
  38.  * the backslash character, the double-quote character, and all
  39.  * non-ASCII characters are escaped.
  40.  *
  41.  * If glib > 1.3 is installed this function is identical to 
  42.  * g_strescape(). For systems using glib-1.2 this function provides the 
  43.  * added functionality from glib-1.3.
  44.  * 
  45.  * Returns: A newly allocated copy of the string, with all special
  46.  * characters escaped as in the C language.
  47.  */
  48. #if !(defined (GLIB_CHECK_VERSION) && GLIB_CHECK_VERSION (1,3,1))
  49. gchar* 
  50. gimp_strescape (const gchar *source, 
  51.         const gchar *exceptions)
  52. {
  53.   const guchar *p = (guchar *) source;
  54.   /* Each source byte needs maximally four destination chars (\777) */
  55.   gchar *dest = g_malloc (strlen (source) * 4 + 1);
  56.   gchar *q = dest;
  57.   guchar excmap[256];
  58.  
  59.   memset (excmap, 0, 256);
  60.   if (exceptions)
  61.     {
  62.       guchar *e = (guchar *) exceptions;
  63.  
  64.       while (*e)
  65.     {
  66.       excmap[*e] = 1;
  67.       e++;
  68.     }
  69.     }
  70.  
  71.   while (*p)
  72.     {
  73.       if (excmap[*p])
  74.     *q++ = *p;
  75.       else
  76.     {
  77.       switch (*p)
  78.         {
  79.         case '\b':
  80.           *q++ = '\\';
  81.           *q++ = 'b';
  82.           break;
  83.         case '\f':
  84.           *q++ = '\\';
  85.           *q++ = 'f';
  86.           break;
  87.         case '\n':
  88.           *q++ = '\\';
  89.           *q++ = 'n';
  90.           break;
  91.         case '\r':
  92.           *q++ = '\\';
  93.           *q++ = 'r';
  94.           break;
  95.         case '\t':
  96.           *q++ = '\\';
  97.           *q++ = 't';
  98.           break;
  99.         case '\\':
  100.           *q++ = '\\';
  101.           *q++ = '\\';
  102.           break;
  103.         case '"':
  104.           *q++ = '\\';
  105.           *q++ = '"';
  106.           break;
  107.         default:
  108.           if ((*p < ' ') || (*p >= 0177))
  109.         {
  110.           *q++ = '\\';
  111.           *q++ = '0' + (((*p) >> 6) & 07);
  112.           *q++ = '0' + (((*p) >> 3) & 07);
  113.           *q++ = '0' + ((*p) & 07);
  114.         }
  115.           else
  116.         *q++ = *p;
  117.           break;
  118.         }
  119.     }
  120.       p++;
  121.     }
  122.   *q = 0;
  123.   return dest;
  124. }
  125. #endif  /* GLIB <= 1.3 */
  126.  
  127. /**
  128.  * gimp_strcompress:
  129.  * @source: A string to that has special characters escaped.
  130.  * 
  131.  * Does the opposite of g_strescape(), that is it converts escaped
  132.  * characters back to their unescaped form.
  133.  *
  134.  * Escaped characters are either one of the sequences \b, \f, \n, \r,
  135.  * \t, \\, \", or a three-digit octal escape sequence \nnn.
  136.  *
  137.  * If glib > 1.3 is installed this function is identical to 
  138.  * g_strcompress(). For systems using glib-1.2 this function provides 
  139.  * the functionality from glib-1.3.
  140.  * 
  141.  * Returns: A newly allocated copy of the string, with all escaped 
  142.  * special characters converted to their unescaped form.
  143.  */
  144. #if !(defined (GLIB_CHECK_VERSION) && GLIB_CHECK_VERSION (1,3,1))
  145. gchar*
  146. gimp_strcompress (const gchar *source)
  147. {
  148.   const gchar *p = source, *octal;
  149.   gchar *dest = g_malloc (strlen (source) + 1);
  150.   gchar *q = dest;
  151.   
  152.   while (*p)
  153.     {
  154.       if (*p == '\\')
  155.     {
  156.       p++;
  157.       switch (*p)
  158.         {
  159.         case '0':  case '1':  case '2':  case '3':  case '4':
  160.         case '5':  case '6':  case '7':
  161.           *q = 0;
  162.           octal = p;
  163.           while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
  164.         {
  165.           *q = (*q * 8) + (*p - '0');
  166.           p++;
  167.         }
  168.           q++;
  169.           p--;
  170.           break;
  171.         case 'b':
  172.           *q++ = '\b';
  173.           break;
  174.         case 'f':
  175.           *q++ = '\f';
  176.           break;
  177.         case 'n':
  178.           *q++ = '\n';
  179.           break;
  180.         case 'r':
  181.           *q++ = '\r';
  182.           break;
  183.         case 't':
  184.           *q++ = '\t';
  185.           break;
  186.         default:        /* Also handles \" and \\ */
  187.           *q++ = *p;
  188.           break;
  189.         }
  190.     }
  191.       else
  192.     *q++ = *p;
  193.       p++;
  194.     }
  195.   *q = 0;
  196.   
  197.   return dest;
  198. }
  199. #endif  /* GLIB <= 1.3 */
  200.