File: string.c

package info (click to toggle)
flin 0.5.1-9.2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 276 kB
  • ctags: 164
  • sloc: ansic: 1,642; makefile: 105; sh: 71
file content (73 lines) | stat: -rw-r--r-- 1,866 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
/*
Manipulates strings for easier parsing
Copyright (C) 1995  Brian Cully

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 675 Mass
Ave, Cambridge, MA 02139, USA.

please send patches or advice to: `shmit@kublai.com'
*/
#ifdef HAVE_CONFIG_H
#   include <config.h>
#endif

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "string.h"


char escape=ESCAPE;

int getline(const char *src, char *dst) {
   int index=0;

   while ((*(src+index) != CR) && *(src+index)) {
      *(dst+index) = *(src+index);
      index++;
   }

   *(dst+index) = '\0';
   if (*(src+index))
      return index;
   return -1;
}

/* get a substring from a string, substrings are non-delimiter characters
** separated by delimiters
** On entry:
**    src == source string
**    dst == destination string
**    delim == delimiter
** On exit:
**    -1 if '\0' was found
**    otherwise, index offset in src where dst ends
*/

int substr(const char *src, char *dst, char delim) {
   int dindex=0, index=0;

   while ((*(src+index) != delim) && *(src+index)) {
      /* Escape character */
      if (*(src+index) == escape && *(src+index+1))
         index++;
      *(dst+dindex) = *(src+index);
      index++; dindex++;
   }

   *(dst+dindex) = '\0';
   if (*(src+index))
      return index;
   return -1;
}