File: mystring.c

package info (click to toggle)
dancer-services 1.8.0.6.3-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,600 kB
  • ctags: 1,623
  • sloc: ansic: 34,690; sh: 2,850; makefile: 269
file content (198 lines) | stat: -rw-r--r-- 4,147 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * HybServ TS Services, Copyright (C) 1998-1999 Patrick Alken
 * This program comes with absolutely NO WARRANTY
 *
 * Should you choose to use and/or modify this source code, please
 * do so under the terms of the GNU General Public License under which
 * this program is distributed.
 *
 * $Id: mystring.c,v 1.3 2001/11/12 09:50:55 asuffield Exp $
 */

#include "defs.h"

#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#include "alloc.h"
#include "config.h"
#include "hybdefs.h"
#include "log.h"
#include "match.h"
#include "sprintf_irc.h"

/*
 * StrToupper()
 *
 * args: char *str
 * purpose: convert the string 'str' to uppercase
 * return: the uppercase string
*/
char *StrToupper(char *str)
{
  unsigned int ii;
  static char retstr[MAXLINE];

  if (!str)
    return (NULL);

  retstr[0] = '\0';
  for (ii = 0; ii < strlen(str); ++ii)
    retstr[ii] = ToUpper(str[ii]);

  retstr[ii] = '\0';

  return (retstr);
} /* StrToupper() */

/*
 * StrTolower()
 *
 * args: char *str
 * purpose: convert the string 'str' to lowercase
 * return: the lowercase string
 */
char *StrTolower(char *str)
{
  unsigned int ii;
  static char retstr[MAXLINE];

  if (!str)
    return NULL;

  retstr[0] = '\0';
  for (ii = 0; ii < strlen(str); ++ii)
    retstr[ii] = ToLower(str[ii]);

  retstr[ii] = '\0';

  return (retstr);
} /* StrTolower() */

/*
 * GetString()
 *
 * Reverse the array av back into a normal string assuming there are 'ac'
 * indices in the array. Space is allocated for the new string.
 */
char *GetString(int ac, char **av)
{
  char *final;
  char temp[MAXLINE];
  int ii;

  final = (char *) MyMalloc(sizeof(char));
  *final = '\0';

  ii = 0;
  while (ii < ac)
  {
    ircsprintf(temp, "%s%s", av[ii], ((ii + 1) >= ac) ? "" : " ");

    final = (char *) MyRealloc(final,
      strlen(final) + strlen(temp) + sizeof(char));
    strcat(final, temp);

    ++ii;
  }

  /* Routine to get rid of spaces at the end of new string. However, we
   * don't need it atm -kre */
#if 0
  ii = strlen(final) - 1;
  while (final[ii] == ' ' && ii)
    final[ii--] = 0;
#endif
  
  return (final);
} /* GetString() */

/*
 * SplitBuf()
 *
 * Take string "buff" and insert NULLs in the spaces between words. Keep
 * pointers to the beginning of each word, and store them in "array".
 * Returns the number of words in "buff"
 */
int SplitBuf(char *buff, char ***array)
{
  int argsize = 8;
  int acnt, ii;
  char *temp1, *tempbuf;

  /* Be safe. If something down fails, it will point 
   * to NULL anyway -kre */
  *array = NULL;

  /* Perform this check -kre */
  if (buff == NULL)
    return 0;

  tempbuf = buff;

  ii = strlen(tempbuf);

  /* No reason to parse this at all -kre */
  if (!ii)
    return 0;

  /* try to kill trailing \r or \n chars */
  if (IsEOL(tempbuf[ii - 1]))
    tempbuf[ii - 1] = '\0';

  /*
   * When hybrid sends channel bans during a netjoin, it leaves
   * a preceding space (not sure why) - just make sure there
   * are no preceding spaces
   *
   * Note that Hybrid7 sends also spaces after SJOIN's, like in
   * <fl_> :irc.vchan SJOIN 978405679 #ircd-coders +sptna : @Diane @dia
   * -kre
   */
  while (IsSpace(*tempbuf))
    ++tempbuf;

  /* Check if tempbuf contained only but spaces -kre */
  if (!*tempbuf)
    return 0;

  *array = (char **) MyMalloc(sizeof(char *) * argsize);
  memset(*array, 0, sizeof(char *) * argsize);
  acnt = 0;
  while (*tempbuf)
  {
    if (acnt == argsize)
    {
      argsize += 8;
      *array = (char **) MyRealloc(*array, sizeof(char *) * argsize);
    }

    if ((*tempbuf == ':') && (acnt != 0))
    {
      (*array)[acnt++] = tempbuf;
      /* (*array)[acnt++] = tempbuf + 1; */
      tempbuf = "";
    }
    else
    {
      /* Why use strpbrk() on only 1 character? We have faster strchr for
       * that. -kre */
      /* temp1 = strpbrk(tempbuf, " "); */
      temp1 = strchr(tempbuf, ' ');
      if (temp1)
      {
        *temp1++ = 0;
        while (IsSpace(*temp1))
          ++temp1;
      }
      else
        temp1 = tempbuf + strlen(tempbuf);

      (*array)[acnt++] = tempbuf;
      tempbuf = temp1;
    }
  }

  return (acnt);
} /* SplitBuf() */