"Fossies" - the Fresh Open Source Software Archive

Member "global-6.6.5/libutil/encodepath.c" (3 Sep 2020, 2490 Bytes) of package /linux/misc/global-6.6.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "encodepath.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 6.6.4_vs_6.6.5.

    1 /*
    2  * Copyright (c) 2005, 2006, 2010 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 #ifdef HAVE_CONFIG_H
   20 #include <config.h>
   21 #endif
   22 #include <stdio.h>
   23 #include <ctype.h>
   24 #ifdef STDC_HEADERS
   25 #include <stdlib.h>
   26 #endif
   27 #ifdef HAVE_STRING_H
   28 #include <string.h>
   29 #else
   30 #include <strings.h>
   31 #endif
   32 
   33 /*
   34 #include "abs2rel.h"
   35 #include "format.h"
   36 #include "gparam.h"
   37 #include "gpathop.h"
   38 #include "gtagsop.h"
   39 #include "convert.h"
   40 #include "strlimcpy.h"
   41 */
   42 #include "die.h"
   43 #include "strbuf.h"
   44 #include "encodepath.h"
   45 
   46 static unsigned char encode[256];
   47 static int encoding;
   48 
   49 /**
   50  * required_encode: return encoded char.
   51  */
   52 int
   53 required_encode(int c)
   54 {
   55     return encode[(unsigned char)c];
   56 }
   57 /**
   58  * set_encode_chars: stores chars to be encoded.
   59  */
   60 void
   61 set_encode_chars(const unsigned char *chars)
   62 {
   63     unsigned int i;
   64 
   65     /* clean the table */
   66     memset(encode, 0, sizeof(encode));
   67     /* set bits */
   68     for (i = 0; chars[i]; i++) {
   69         encode[(unsigned char)chars[i]] = 1;
   70     }
   71     /* You cannot encode '.' and '/'. */
   72     encode['.'] = 0;
   73     encode['/'] = 0;
   74     /* '%' is always encoded when encode is enable. */
   75     encode['%'] = 1;
   76     encoding = 1;
   77 }
   78 /**
   79  * use_encoding: 
   80  */
   81 int
   82 use_encoding(void)
   83 {
   84     return encoding;
   85 }
   86 #define outofrange(c)   (c < '0' || c > 'f')
   87 #define h2int(c) (c >= 'a' ? c - 'a' + 10 : c - '0')
   88 /**
   89  * decode_path: decode encoded path name.
   90  *
   91  *  @param[in]  path    encoded path name
   92  *  @return     decoded path name
   93  */
   94 char *
   95 decode_path(const char *path)
   96 {
   97     STATIC_STRBUF(sb);
   98     const char *p;
   99 
  100     strbuf_clear(sb);
  101     for (p = path; *p; p++) {
  102         if (*p == '%') {
  103             unsigned char c1, c2;
  104             c1 = *++p;
  105             c2 = *++p;
  106             if (outofrange(c1) || outofrange(c2))
  107                 die("decode_path: unexpected character. (%%%c%c)", c1, c2);
  108             strbuf_putc(sb, h2int(c1) * 16 + h2int(c2));
  109         } else
  110             strbuf_putc(sb, *p);
  111     }
  112     return strbuf_value(sb);
  113 }