/[global]/global/libutil/strmake.c
ViewVC logotype

Diff of /global/libutil/strmake.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by shigio, Fri Jun 27 15:51:34 2003 UTC revision 1.5 by shigio, Tue Oct 12 23:18:23 2004 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (c) 1998, 1999 Shigio Yamaguchi   * Copyright (c) 1998, 1999 Shigio Yamaguchi
3   * Copyright (c) 1999, 2000 Tama Communications Corporation   * Copyright (c) 1999, 2000, 2004 Tama Communications Corporation
4   *   *
5   * This file is part of GNU GLOBAL.   * This file is part of GNU GLOBAL.
6   *   *
# Line 31  Line 31 
31    
32  static STRBUF *sb;  static STRBUF *sb;
33    
34    /*
35     * strmake: make string from original string p.
36     *
37     *      i)      p       original string.
38     *      i)      lim     limitter
39     *      r)              result string
40     *
41     * Usage:
42     *      strmake("aaa:bbb", ":/=")       => "aaaa"
43     *
44     * Note: The result string area is module local. So, following call
45     *       to the function in this module may destroy the area.
46     */
47  char *  char *
48  strmake(p, lim)  strmake(p, lim)
49          const char *p;          const char *p;
# Line 50  strmake(p, lim) Line 63  strmake(p, lim)
63  end:  end:
64          return strbuf_value(sb);          return strbuf_value(sb);
65  }  }
66    
67    /*
68     * strtrim: delete blanks from original string.
69     *
70     *      i)      p       original string.
71     *      i)      flag    TRIM_HEAD       from only head
72     *                      TRIM_TAIL       from only tail
73     *                      TRIM_BOTH       from head and tail
74     *                      TRIM_ALL        from all
75     *      o)      len     length of result string
76     *                      if len == NULL then nothing returned.
77     *      r)              result string
78     *
79     * Usage:
80     *      strtrim(" # define ", TRIM_HEAD, NULL)  => "# define "
81     *      strtrim(" # define ", TRIM_TAIL, NULL)  => " # define"
82     *      strtrim(" # define ", TRIM_BOTH, NULL)  => "# define"
83     *      strtrim(" # define ", TRIM_ALL, NULL)   => "#define"
84     *
85     * Note: The result string area is module local. So, following call
86     *       to the function in this module may destroy the area.
87     */
88    char *
89    strtrim(p, flag, len)
90            const char *p;
91            int flag;
92            int *len;
93    {
94            int cut_off = -1;
95    
96            if (sb == NULL)
97                    sb = strbuf_open(0);
98            strbuf_reset(sb);
99            /*
100             * Delete blanks of the head.
101             */
102            if (flag != TRIM_TAIL)
103                    SKIP_BLANKS(p);
104            /*
105             * Copy string.
106             */
107            for (; *p; p++) {
108                    if (isspace(*p)) {
109                            if (flag != TRIM_ALL) {
110                                    if (cut_off == -1 && flag != TRIM_HEAD)
111                                            cut_off = strbuf_getlen(sb);
112                                    strbuf_putc(sb,*p);
113                            }
114                    } else {
115                            strbuf_putc(sb,*p);
116                            cut_off = -1;
117                    }
118            }
119            /*
120             * Delete blanks of the tail.
121             */
122            if (cut_off != -1)
123                    strbuf_setlen(sb, cut_off);
124            if (len)
125                    *len = strbuf_getlen(sb);
126            return strbuf_value(sb);
127    }

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26