File: string-lib.c

package info (click to toggle)
ctn 3.2.0~dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 16,924 kB
  • sloc: ansic: 179,652; makefile: 7,006; java: 1,863; csh: 1,067; yacc: 523; sh: 424; cpp: 394; sql: 389; lex: 170
file content (88 lines) | stat: -rw-r--r-- 1,753 bytes parent folder | download | duplicates (6)
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
/* string-lib.c - generic string processing routines
   Eugene Kim, <eekim@eekim.com>
   $Id: string-lib.c,v 1.1 2001-10-23 19:08:22 smm Exp $

   Copyright (C) 1996 Eugene Eric Kim
   All Rights Reserved.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "string-lib.h"

char *newstr(char *str)
{
  char *tempstr = (char *)malloc(sizeof(char) * strlen(str) + 1);

  if (tempstr != NULL)
    strcpy(tempstr,str);
  return tempstr;
}

char *substr(char *str, int offset, int len)
{
  int slen, start, i;
  char *nstr;

  if (str == NULL)
    return NULL;
  else
    slen = strlen(str);
  nstr = (char *)malloc(sizeof(char) * slen + 1);
  if (offset >= 0)
    start = offset;
  else
    start = slen + offset - 1;
  if ( (start < 0) || (start > slen) ) /* invalid offset */
    return NULL;
  for (i = start; i < start+len; i++)
    nstr[i - start] = str[i];
  nstr[len] = '\0';
  return nstr;
}

char *replace_ltgt(char *str)
{
  unsigned int i,j = 0;
  char *newstring;


  if (str == NULL)
    return NULL;
  newstring = (char *)malloc(sizeof(char) * (strlen(str) * 4 + 1));
  for (i = 0; i < strlen(str); i++) {
    if (str[i] == '<') {
      newstring[j] = '&';
      newstring[j+1] = 'l';
      newstring[j+2] = 't';
      newstring[j+3] = ';';
      j += 3;
    }
    else if (str[i] == '>') {
      newstring[j] = '&';
      newstring[j+1] = 'g';
      newstring[j+2] = 't';
      newstring[j+3] = ';';
      j += 3;
    }
    else
      newstring[j] = str[i];
    j++;
  }
  newstring[j] = '\0';
  return newstring;
}

char *lower_case(char *buffer)
{
  char *tempstr = buffer;

  while (*buffer != '\0') {
    if (isupper(*buffer))
      *buffer = tolower(*buffer);
    buffer++;
  }
  return tempstr;
}