aboutsummaryrefslogtreecommitdiff
path: root/src/extString.c
blob: 6538dac8bee7d986d7e089e3b849fb2123f582b9 (plain) (blame)
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
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char *strlwr(char *s)
{
  unsigned char *p = (unsigned char *)s;

  while (*p) {
     *p = tolower((unsigned char)*p);
      p++;
  }

  return s;
}

char *trim(char *s)
{
  char *result = strdup(s);
  char *end;

  while(isspace((unsigned char)*result)) result++;

  if(*result == 0)
    return result;

  end = result + strlen(result) - 1;
  while(end > result && isspace((unsigned char)*end)) end--;

  end[1] = '\0';

  return result;
}

char *replaceStr(char *s, char *old, char *replace) {
  char* result; 
  int i, cnt = 0; 
  size_t newSize = strlen(replace); 
  size_t oldSize = strlen(old); 
 
  for (i = 0; s[i] != '\0'; i++) { 
    if (strstr(&s[i], old) == &s[i]) { 
      cnt++; 
      i += oldSize - 1; 
    } 
  } 

  result = (char*)malloc(i + cnt * (newSize - oldSize) + 1); 
 
  i = 0; 
  while (*s) { 
    if (strstr(s, old) == s) { 
      strcpy(&result[i], replace); 
      i += newSize; 
      s += oldSize; 
    } 
    else
      result[i++] = *s++; 
  } 
 
  result[i] = '\0'; 
  return result; 
};