libcgi_string - Man Page

Detailed Description

General string manipulation and utilities functions.

Functions

char * str_base64_encode (char *str)
Encodes a given tring to its base64 form.
char * str_base64_decode (char *str)
Decode a base64 encoded string.
char * addnslashes (char *s, int n)
Same to addslashes(), except that this one only do the action while 'n' is great than 0.
char * addslashes (char *s)
Add slashes to a string when necessary.
char * stripnslashes (char *s, int n)
Strip no more than 'n' slashes from a string.
char * stripslashes (char *str)
Strip slashes from a string.
void ltrim (char *str)
Strip left white spaces from a string.
void rtrim (char *str)
Strip right white spaces from a string.
void trim (char *str)
Strip both left and right white spaces from a string.
char * substr (char *src, const int start, const int count)
Copy part of a string.
char ** explode (char *src, const char *token, int *total)
Create an array from a string separated by some special char.
char * str_nreplace (char *src, const char *delim, const char *with, int n)
Replace characteres in a string, but not more than 'n'.
char * str_replace (char *str, const char *delim, const char *with)
Replace characteres in a string.
int strnpos (char *s, char *ch, unsigned int count)
Returns the position of a character in a string, but parses no more that 'n' chars.
int strpos (char *s, char *ch)
Returns the position of a character in a string.
char * strdel (char *s, int start, int count)
Delete characters from a string.
char * recvline (FILE *s)
Reads an entire line.
char * make_string (char *s,...)
Makes a string.

Function Documentation

char* addnslashes (char * s, int n)

Same to addslashes(), except that this one only do the action while 'n' is great than 0.Parameters:

s String to parse
n Number of characters to work with.

See also:

addslashes()

 char *name = 'My test string is called ´foobar´';
 puts(name); // will display My test string is called 'foobar'
 
 name = addnslashes(name, 31);
 puts(name); // will display My test string is called ´foobar'

char* addslashes (char * s)

Add slashes to a string when necessary.Adds a '\' in every quote ( ' ), apostrophe ( ' ) or backslash ( \ ) It's useful when working with databases, for example, because someone can try insert this caracters to try hack the application...

Parameters:

*s String to parse

Returns:

The new string, with slashes

See also:

stripslashes, addnslashes

 char *name = 'My test string is called ´foobar´';
 puts(name); // will display My test string is called 'foobar'
 
 name = addslashes(name);
 puts(name); // will display My test string is called ´foobar´

char** explode (char * src, const char * token, int * total)

Create an array from a string separated by some special char.Divides the src string in pieces, each delimited by token and storing the total of pieces in total

Parameters:

src String to parse
token Character delimiter to search.
total An integer variable passed as reference, which stores the total of itens of the array

Returns:

The array, where each item is one separeted by token

  
  char **pieces;
  char *name = 'This,is,a,string,of,test';
  int total, i;
  pieces = explode(name, ',', &total);
  for (i = 0; i < total; i++)
        printf('Piece %d: %s0, i, *(pieces+i));

void ltrim (char * str)

Strip left white spaces from a string.Parameters:

str String to parse

Returns:

The new string, without left spaces

Author:

Original code was contribuition by Erik Jansson

See also:

rtrim, trim

 char *s = '     String with spaces    ';
 printf('_%s_0, s);
 s = ltrim(s);
 printf('_%s_0, s);

char* make_string (char * s, ...)

Makes a string.Works like printf(), with the difference that it returns a string that is the concatenation of the values passed as parameter.

Parameters:

*s Inicial String and optionally formatation parameters ( just s is allowed )

Returns:

The new String

 char *sql = make_string('INSERT INTO myTable VALUES ('%s', '%s', '%s')', varValue1, varValue2, varValue3);
Todo

String limits/error checking

char* recvline (FILE * s)

Reads an entire line.Reads a line from the file specified by the file pointer passed as parameter. This function is intead to replace the non-portable GNU getline() function.

Parameters:

s File pointer to the file to read from.

Returns:

String containing the line read or NULL if no more line are available

Author:

Robert Csok

void rtrim (char * str)

Strip right white spaces from a string.Parameters:

str String to parse

Returns:

The new string, without left spaces

Author:

Original code was contribuition by Erik Jansson

See also:

ltrim, trim

 char *s = '     String with spaces    ';
 printf('_%s_0, s);
 s = rtrim(s);
 printf('_%s_0, s);

char* str_base64_decode (char * str)

Decode a base64 encoded string.Parameters:

*str Encoded String to decode

Returns:

The decoded string

See also:

str_base64_encode

char* str_base64_encode (char * str)

Encodes a given tring to its base64 form.Parameters:

*str String to convert

Returns:

Base64 encoded String

See also:

str_base64_decode

char* str_nreplace (char * src, const char * delim, const char * with, int n)

Replace characteres in a string, but not more than 'n'.Replace all occourences of *delim on *src with characteres pointed by *with, stopping after 'n' char.

Parameters:

*src String to parse
*delim Character to search that will be replaced
with String to replace with
n Maximum number of chars to parse

Returns:

The new string

See also:

str_replace

  char *linux = 'Linux C';
  linux = str_nreplace(linux, 'C', 'Cool', strlen(linux));
  puts(linux);
 //  -- OR --
  char *name = 'rAfAel steil';
  name = str_nreplace(name, 'A', 'a', 3);
  puts(name);

char* str_replace (char * str, const char * delim, const char * with)

Replace characteres in a string.Replace all occourences of *delim on *src with characteres pointed by *with. The problem with the folowing code is that the function only searches for the first caracter of *delim, ingoring the rest. Other problem is speed relacioned: note that the function ever compare the length of *with to do the correct action.

Parameters:

src String to parse
delim Character to search that will be replaced
with String to replace with

Returns:

The new string

See also:

str_nreplace

  char *linux = 'Linux C';
  linux = str_replace(linux, 'C', 'Cool');
  puts(linux);
 //  -- OR --
  char *name = 'rAfAel steil';
  name = str_replace(name, 'A', 'a');
  puts(name);

char* strdel (char * s, int start, int count)

Delete characters from a string.Delete count characters of s, starting in start

Parameters:

s String to search
start Initial offset to begin search
count Number of characteres to delete

Returns:

The new string

See also:

strndel()

  *txt = 'Some text to test anything';
  puts(txt);
  txt = strdel(txt, 2, 8);
  puts(txt);

char* stripnslashes (char * s, int n)

Strip no more than 'n' slashes from a string.Strip the backslash character ( \ ) from a string, stopping after 'n' char

Parameters:

s String to parse
n Maximum number of chars to parse

Returns:

The new string, without slashes

See also:

addslashes, stripslashes

 char *name = 'My another string is called \´blablabla\´';
 puts(name); // will display My another string is called ´blablabla´
 name = stripslashes(name, 33);
 puts(name); // will display My another string is called 'blablabla´

char* stripslashes (char * str)

Strip slashes from a string.Strip the backslash character ( \ ) from a string

Parameters:

s String to parse

Returns:

The new string, without slashes

See also:

addslashes, stripnslashes

 char *name = 'My another string is called \´blablabla\´';
 puts(name); // will display My another string is called ´blablabla´
 name = stripslashes(name);
 puts(name); // will display My another string is called 'blablabla'

int strnpos (char * s, char * ch, unsigned int count)

Returns the position of a character in a string, but parses no more that 'n' chars.Parameters:

s String where the search will be done
ch Character to search
count Maximum number of chars to parse before exiting the function

See also:

strpos()

int strpos (char * s, char * ch)

Returns the position of a character in a string.Parameters:

s String where the search will be done
ch Character to search
count Maximum number of ch to search

See also:

strnpos()

char* substr (char * src, const int start, const int count)

Copy part of a string.Copy count characters from src, starting from start

Parameters:

src String to copy from
start Initial offset
count Number of chars to copy

Returns:

The new string

 char *part, *str = 'Test one, test two';
 part = substr(str, 1, 5);
 puts(part); // -> est o

void trim (char * str)

Strip both left and right white spaces from a string.Parameters:

str String to parse

Returns:

The new string, without left spaces

Author:

Original code was contribuition by Erik Jansson

See also:

ltrim, trim

 char *s = '     String with spaces    ';
 printf('_%s_0, s);
 s = trim(s);
 printf('_%s_0, s);

Info

13 Mar 2003 LibCGI