root/libutil/char.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. isregex
  2. quote_string
  3. quote_chars
  4. quote_shell

   1 /*
   2  * Copyright (c) 2003, 2011 Tama Communications Corporation
   3  *
   4  * This file is part of GNU GLOBAL.
   5  *
   6  * This program is free software: you can redistribute it and/or modify
   7  * it under the terms of the GNU General Public License as published by
   8  * the Free Software Foundation, either version 3 of the License, or
   9  * (at your option) any later version.
  10  * 
  11  * This program is distributed in the hope that it will be useful,
  12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14  * GNU General Public License for more details.
  15  * 
  16  * You should have received a copy of the GNU General Public License
  17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18  */
  19 
  20 #ifdef HAVE_CONFIG_H
  21 #include <config.h>
  22 #endif
  23 #include <ctype.h>
  24 
  25 #include "char.h"
  26 #include "strbuf.h"
  27 
  28 #define B       BINARYCHAR
  29 #define R       REGEXCHAR
  30 #define U       URLCHAR
  31 #define RU      REGEXCHAR | URLCHAR
  32 const unsigned char chartype[256] = {
  33 #if '\n' == 0x0a && ' ' == 0x20 && '0' == 0x30 \
  34   && 'A' == 0x41 && 'a' == 0x61 && '!' == 0x21
  35         /* ASCII */
  36         B, B, B, B, B, B, B, B, 0, 0, 0, 0, 0, 0, B, B,
  37         B, B, B, B, B, B, B, B, B, B, B, 0, B, B, B, B,
  38         0, U, 0, 0, R, 0, 0, U,RU,RU,RU, R, 0, U,RU, U, /*  !"#$%&'()*+,-./ */
  39         U, U, U, U, U, U, U, U, U, U, 0, 0, 0, 0, 0, R, /* 0123456789:;<=>? */
  40         0, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, /* @ABCDEFGHIJKLMNO */
  41         U, U, U, U, U, U, U, U, U, U, U, R, R, R, R, U, /* PQRSTUVWXYZ[\]^_ */
  42         0, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, /* `abcdefghijklmno */
  43         U, U, U, U, U, U, U, U, U, U, U, R, 0, R, U,    /* pqrstuvwxyz{|}~ */
  44 #else
  45 #error "Unsupported character encoding."
  46 #endif
  47 };
  48 /**
  49  * isregex: test whether or not regular expression
  50  *
  51  *      @param[in]      s       string
  52  *      @return         1: is regex, 0: not regex
  53  */
  54 int
  55 isregex(const char *s)
  56 {
  57         int c;
  58 
  59         while ((c = *s++) != '\0')
  60                 if (isregexchar(c))
  61                         return 1;
  62         return 0;
  63 }
  64 /**
  65  * quote string.
  66  *
  67  *  @remark Non-alphanumeric characters are quoted/escaped.
  68  *
  69  *      @par Examples:
  70  *      @code
  71  *      'a:a,a' => 'a\:a\,a'
  72  *      @endcode
  73  */
  74 const char *
  75 quote_string(const char *s)
  76 {
  77         STATIC_STRBUF(sb);
  78 
  79         strbuf_clear(sb);
  80         for (; *s; s++) {
  81                 if (!isalnum((unsigned char)*s))
  82                         strbuf_putc(sb, '\\');
  83                 strbuf_putc(sb, *s);
  84         }
  85         return strbuf_value(sb);
  86 }
  87 /**
  88  * quote characters in the string.
  89  *
  90  *      @par Examples:
  91  *      @code
  92  *      quote_char('a:a,a', :) => 'a\:a,a'
  93  *      @endcode
  94  */
  95 const char *
  96 quote_chars(const char *s, unsigned int c)
  97 {
  98         STATIC_STRBUF(sb);
  99 
 100         strbuf_clear(sb);
 101         for (; *s; s++) {
 102                 if ((unsigned char)*s == c)
 103                         strbuf_putc(sb, '\\');
 104                 strbuf_putc(sb, *s);
 105         }
 106         return strbuf_value(sb);
 107 }
 108 #if defined(__DJGPP__) || (defined(_WIN32) && !defined(__CYGWIN__))
 109 #define SHELL_QUOTE '"'
 110 #else
 111 #define SHELL_QUOTE '\''
 112 #endif
 113 /**
 114  * quote for shell.
 115  *
 116  *      @par Examples:
 117  *      @code
 118  *      aaa => 'aaa'
 119  *      a'a => 'a'\''aa'
 120  *      @endcode
 121  */
 122 const char *
 123 quote_shell(const char *s)
 124 {
 125         STATIC_STRBUF(sb);
 126 
 127         strbuf_clear(sb);
 128         strbuf_putc(sb, SHELL_QUOTE);
 129 #if defined(__DJGPP__) || (defined(_WIN32) && !defined(__CYGWIN__))
 130         strbuf_puts(sb, s);
 131 #else
 132         for (; *s; s++) {
 133                 if (*s == SHELL_QUOTE) {
 134                         strbuf_putc(sb, SHELL_QUOTE);
 135                         strbuf_putc(sb, '\\');
 136                         strbuf_putc(sb, SHELL_QUOTE);
 137                         strbuf_putc(sb, SHELL_QUOTE);
 138                 } else
 139                         strbuf_putc(sb, *s);
 140         }
 141 #endif
 142         strbuf_putc(sb, SHELL_QUOTE);
 143         return strbuf_value(sb);
 144 }

/* [previous][next][first][last][top][bottom][index][help] */