src/spstring.h File Reference

The Main library header. More...

#include "bool.h"
Include dependency graph for spstring.h:

Go to the source code of this file.

Typedefs

typedef char * string
 Just an alias: string is easier to use and rember than char* .
typedef unsigned int uint
 Just an alias: uint is easier to use and rember than unsigned int.

Functions

string sp_new_string (const string str)
 Creates a new string as a copy of str.
string sp_new_empty_string (int len)
 Creates a new empty string that can store at most len chars.
string sp_strcpy (string copy, const string original, int offset)
 Copies the string pointed by original into copy starting from the offset specified.
void sp_free (string str)
 Free a string previously dinamically created.
int sp_strlen (const string str)
 An strlen clone, it calculates the length of the string str, not including the terminating '\0' character.
bool sp_strcmp (const string str1, const string str2)
 Similar to strcmp, it compares two strings.
string sp_strcat (const string str1, const string str2)
 Similar to strcat, but differently from that one, it creates a NEW string.
string sp_lower_case (const string str)
 Create a NEW string, with the same content as str but lowercase.
string sp_upper_case (const string str)
 Create a NEW string, with the same content as str but uppercase.

Detailed Description

The Main library header.

Author:
Andrea Florio <andrea@opensuse.org>
Giuseppe Grassi <gra.giu@alice.it>
Version:
0.2
Date:
2010

License

Copyright (C) 2010
Andrea Florio 2010 <andrea@opensuse.org>
Giuseppe Grassi 2010 <gra.giu@alice.it>

This Project 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 3 of the License, or (at your option) any later version.

This Project 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, see <http://www.gnu.org/licenses/>.


Typedef Documentation

char * string

Just an alias: string is easier to use and rember than char* .

unsigned int uint

Just an alias: uint is easier to use and rember than unsigned int.


Function Documentation

void sp_free ( string  str  ) 

Free a string previously dinamically created.

After the free, str is NULL.

Parameters:
str Is the string to free.
string sp_lower_case ( const string  str  ) 

Create a NEW string, with the same content as str but lowercase.

Warning:
A NEW string is created. The returned string is created using calloc, you must use free() or sp_free() to free the memory.
Parameters:
str Is the string to convert to lowercase.
Returns:
a b pointer to the lowercase string or NULL if str is NULL.
string sp_new_empty_string ( int  len  ) 

Creates a new empty string that can store at most len chars.

Warning:
the returned string is created using calloc, you must use free() or sp_free() to free the memory
Parameters:
len Is the lenght of the new string
Returns:
an empty string
string sp_new_string ( const string  str  ) 

Creates a new string as a copy of str.

Warning:
the returned string is created using calloc, you must use free() or sp_free() to free the memory
Parameters:
str Is the string to copy.
Returns:
a string with the same content of str.
string sp_strcat ( const string  str1,
const string  str2 
)

Similar to strcat, but differently from that one, it creates a NEW string.

Example:

       str1 = "hello";
       str2 = "world";
       string test = sp_strcat(const string str1, const string str2);

test will be: helloworld .

Warning:
str1 and str2 are NOT touched, a NEW string is created. the returned string is created using calloc, you must use free() or sp_free() to free the memory.
Parameters:
str1 Is the first string to append.
str2 Is the second string append.
Returns:
a pointer to a string cointaining str1+str2.
bool sp_strcmp ( const string  str1,
const string  str2 
)

Similar to strcmp, it compares two strings.

Parameters:
str1 Is the first string into the comparison.
str2 Is the second string into the comparison.
Returns:
TRUE if str1 match str2, else FALSE
string sp_strcpy ( string  copy,
const string  original,
int  offset 
)

Copies the string pointed by original into copy starting from the offset specified.


The terminating null byte ('\0') is automatically added. The strings may not overlap, and
the destination string copy must be large enough to contain original + offset.

Parameters:
copy Is the destination string.
original Is the string that will be copied into copy.
offset Is a POSITIVE offset from where original will be copied.
Returns:
A pointer to destination string copy. If copy or original are NULL or offset is < 0 returns NULL.
Bug:
sp_strcpy is not safe. Be sure your string can contain original+offset.
Example:
 // The string copy can store at most 20 chars
 string copy = sp_new_empty_strig(20);
 // but the 'copy' lenght IS now only 4
 copy = sp_new_string("test");

 sp_strcpy(copy, "hello", 3);

Now the string copy will be "teshello".
This case looks ok, infact the array copy can store 20 chars; sp_strlen ("hello")+offset == 7, and 7 < 20.
But here:

 // Create a string copy
 // As before, the 'copy' lenght IS 4
 string copy = "test";

 sp_strcpy(copy, "hello", 3);

Here we'll have a buffer-overflow.
Because the array copy can store only 4 chars, and sp_strlen ("hello")+offset == 7, 7 > 4.
The new string simply doesn't fit.

int sp_strlen ( const string  str  ) 

An strlen clone, it calculates the length of the string str, not including the terminating '\0' character.

Parameters:
str Is the string witch we need to know the lenght.
Returns:
The str lenght, 0 if str is NULL.
string sp_upper_case ( const string  str  ) 

Create a NEW string, with the same content as str but uppercase.

Warning:
A NEW string is created. The returned string is created using calloc, you must use free() or sp_free() to free the memory.
Parameters:
str Is the string to convert to uppercase.
Returns:
a pointer to the uppercase string or NULL if str is NULL.
 All Data Structures Files Functions Variables Typedefs Defines
Generated on Thu Jun 10 16:35:58 2010 for libstringpool by  doxygen 1.6.3