89 lines
1.4 KiB
C
89 lines
1.4 KiB
C
// string.c
|
|
// Author: Josh Holtrop
|
|
// Created: 02/26/04
|
|
// Implements string functions
|
|
|
|
#include "string.h"
|
|
|
|
char *strcat(char *dest, char *src)
|
|
{
|
|
strcpy(dest+strlen(dest), src);
|
|
}
|
|
|
|
|
|
//Splits a string into multiple strings by turning all characters
|
|
// equal to delim into null values (string termination character)
|
|
//Returns the number of strings after the split, 1 if no delim chars
|
|
int string_split(char *str, char delim)
|
|
{
|
|
if (strlen(str) < 1)
|
|
return 0; //empty string
|
|
int count = 1;
|
|
for (; *str; str++)
|
|
{
|
|
if (*str == delim)
|
|
{
|
|
count++;
|
|
*str = 0;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
|
|
//Advances a char pointer to the byte after the current string's
|
|
// null-terminating character
|
|
//Useful after calling string_split()
|
|
//Returns a pointer to the following string
|
|
char *string_advance(char *str)
|
|
{
|
|
for (; *str; str++);
|
|
return str+1;
|
|
}
|
|
|
|
|
|
void rtrim(char *str)
|
|
{
|
|
str += strlen(str); //now points to the null character at the end of the string
|
|
str--;
|
|
for (;;)
|
|
{
|
|
if ((*str == ' ') || (*str == '\t') || (*str == '\n'))
|
|
*str-- = 0;
|
|
else
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
char *ucase(char *str)
|
|
{
|
|
char *ret = str;
|
|
for (;;)
|
|
{
|
|
if (*str == 0)
|
|
break;
|
|
if ((*str >= 'a') && (*str <= 'z'))
|
|
*str = (*str) - 32;
|
|
str++;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
|
|
char *lcase(char *str)
|
|
{
|
|
char *ret = str;
|
|
for (;;)
|
|
{
|
|
if (*str == 0)
|
|
break;
|
|
if ((*str >= 'A') && (*str <= 'Z'))
|
|
*str = (*str) + 32;
|
|
str++;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
|