/manedit-1.2.1/manedit/ca_string.c
# · C · 1208 lines · 829 code · 203 blank · 176 comment · 238 complexity · f83631a272ac719c045ea54f59a8f02f MD5 · raw file
- #include <glib.h>
- #include <string.h>
- #include "ca_string.h"
- /* New, Copy, Delete */
- CAString *CAStringNewFromString(
- const gchar *s,
- const gint len
- );
- CAString *CAStringCopy(const CAString *ca_string);
- void CAStringDelete(CAString *ca_string);
- /* Convert To Regular Single-Byte String */
- gchar *CAStringToRegularString(
- const CAString *ca_string,
- const gint len
- );
- /* Measuring */
- gint CAStringLengthPointers(
- const CAString *ca_string1_ptr,
- const CAString *ca_string2_ptr
- );
- gint CAStringLength(const CAString *ca_string);
- gboolean CAStringIsEmpty(const CAString *ca_string);
- /* Seek */
- CAString *CAStringSeekToIndex(
- CAString *ca_string,
- const gint i
- );
- CAString *CAStringSeekToCharacter(
- CAString *ca_string,
- const gchar c
- );
- CAString *CAStringSeekToNextCharacter(
- CAString *ca_string,
- const gchar c
- );
- CAString *CAStringSeekToNextCharacters(
- CAString *ca_string,
- const gchar *characters_list,
- const gint ncharacters
- );
- CAString *CAStringSeekToNextAttribute(CAString *ca_string);
- CAString *CAStringNextLine(CAString *ca_string);
- CAString *CAStringPreviousLine(
- CAString *ca_string,
- CAString *ca_string_i
- );
- CAString *CAStringCurrentLineStart(
- CAString *ca_string,
- CAString *ca_string_i
- );
- /* Add/Remove Attribute */
- void CAStringSegmentAddAttribute(
- CAString *ca_string,
- const gint start_i, const gint end_i,
- const CAStringAttributes attr
- );
- void CAStringSegmentRemoveAttribute(
- CAString *ca_string,
- const gint start_i, const gint end_i,
- const CAStringAttributes attr
- );
- /* Select/Unselect */
- void CAStringSelect(
- CAString *ca_string,
- const gint selection_start_pos,
- const gint selection_end_pos,
- const gboolean clear_existing_selection
- );
- void CAStringUnselectAll(CAString *ca_string);
- /* Insert/Remove Character */
- CAString *CAStringInsertCharacter(
- CAString *ca_string,
- const gint i,
- const gchar c,
- const CAStringAttributes attr
- );
- CAString *CAStringAppendCharacter(
- CAString *ca_string,
- const gchar c,
- const CAStringAttributes attr
- );
- CAString *CAStringInsertText(
- CAString *ca_string,
- const gint i,
- const CAString *ca_string2
- );
- CAString *CAStringRemoveCharacter(
- CAString *ca_string,
- const gint i
- );
- CAString *CAStringRemoveText(
- CAString *ca_string,
- const gint i,
- const gint len
- );
- /* Remove End Lines */
- void CAStringClearLineEnds(CAString *ca_string);
- /* Remove Spaces */
- CAString *CAStringChug(CAString *ca_string);
- CAString *CAStringChomp(CAString *ca_string);
- CAString *CAStringStrip(CAString *ca_string);
- #define ATOI(s) (((s) != NULL) ? atoi(s) : 0)
- #define ATOL(s) (((s) != NULL) ? atol(s) : 0)
- #define ATOF(s) (((s) != NULL) ? atof(s) : 0.0f)
- #define STRDUP(s) (((s) != NULL) ? g_strdup(s) : NULL)
- #define MAX(a,b) (((a) > (b)) ? (a) : (b))
- #define MIN(a,b) (((a) < (b)) ? (a) : (b))
- #define CLIP(a,l,h) (MIN(MAX((a),(l)),(h)))
- #define STRLEN(s) (((s) != NULL) ? strlen(s) : 0)
- #define STRISEMPTY(s) (((s) != NULL) ? (*(s) == '\0') : TRUE)
- /*
- * Creates a new CAString from a regular single-byte
- * null-terminated string.
- */
- CAString *CAStringNewFromString(
- const gchar *s,
- const gint len
- )
- {
- gint _len = len;
- const gchar *sp,
- *s_end;
- CAString *ca_string,
- *ca_string_ptr;
- if(s == NULL)
- return(NULL);
- if(_len < 0)
- _len = strlen(s);
- /* Create a new CAString */
- ca_string = (CAString *)g_malloc((_len + 1) * sizeof(CAString));
- if(ca_string == NULL)
- return(NULL);
- /* Copy/convert the specified single-byte string data to the
- * CAString
- */
- ca_string_ptr = ca_string;
- sp = s;
- s_end = s + _len;
- while((*sp != '\0') && (sp < s_end))
- {
- CA_STRING_SET_CHARACTER(ca_string_ptr, *sp);
- CA_STRING_SET_ATTRIBUTE(ca_string_ptr, 0);
- ca_string_ptr++;
- sp++;
- }
- /* Add the null-terminating character to the CAString */
- CA_STRING_SET_CHARACTER(ca_string_ptr, '\0');
- CA_STRING_SET_ATTRIBUTE(ca_string_ptr, 0);
- return(ca_string);
- }
- /*
- * Coppies the CAString.
- */
- CAString *CAStringCopy(const CAString *ca_string)
- {
- gint len;
- const CAString *ca_string_ptr;
- CAString *ca_string2,
- *ca_string2_ptr;
- if(ca_string == NULL)
- return(NULL);
- len = CAStringLength(ca_string);
- ca_string2 = (CAString *)g_malloc((len + 1) * sizeof(CAString));
- if(ca_string2 == NULL)
- return(NULL);
- ca_string_ptr = ca_string;
- ca_string2_ptr = ca_string2;
- while(CA_STRING_CHARACTER(ca_string_ptr) != '\0')
- {
- *ca_string2_ptr = *ca_string_ptr;
- ca_string_ptr++;
- ca_string2_ptr++;
- }
- *ca_string2_ptr = *ca_string_ptr;
- return(ca_string2);
- }
- /*
- * Deletes the CAString.
- */
- void CAStringDelete(CAString *ca_string)
- {
- g_free(ca_string);
- }
- /*
- * Copy/converts the CAString to a regular single-byte
- * null-terminated string.
- */
- gchar *CAStringToRegularString(
- const CAString *ca_string,
- const gint len
- )
- {
- gint _len = len;
- gchar *s,
- *sp,
- *s_end;
- const CAString *ca_string_ptr;
- if((ca_string == NULL) || (_len == 0))
- return(NULL);
- if(_len < 0)
- {
- _len = CAStringLength(ca_string);
- if(_len == 0)
- return(NULL);
- s = g_malloc((_len + 1) * sizeof(gchar));
- if(s == NULL)
- return(NULL);
- sp = s;
- s_end = sp + _len;
- ca_string_ptr = ca_string;
- while(sp < s_end)
- {
- *sp = CA_STRING_CHARACTER(ca_string_ptr);
- sp++;
- ca_string_ptr++;
- }
- *sp = '\0';
- }
- else
- {
- s = g_malloc((_len + 1) * sizeof(gchar));
- if(s == NULL)
- return(NULL);
- sp = s;
- s_end = sp + _len;
- ca_string_ptr = ca_string;
- while((sp < s_end) && (CA_STRING_CHARACTER(ca_string_ptr) != '\0'))
- {
- *sp = CA_STRING_CHARACTER(ca_string_ptr);
- sp++;
- ca_string_ptr++;
- }
- *sp = '\0';
- }
- return(s);
- }
- /*
- * Calculates the number of characters between two CAString
- * pointers.
- *
- * Returns the number of units between the two CAString
- * pointers. Never returns negative, but can return 0 on error.
- */
- gint CAStringLengthPointers(
- const CAString *ca_string1_ptr,
- const CAString *ca_string2_ptr
- )
- {
- if((ca_string1_ptr == NULL) || (ca_string2_ptr == NULL))
- return(0);
- if(ca_string1_ptr > ca_string2_ptr)
- return(0);
- return((gint)(ca_string2_ptr - ca_string1_ptr));
- }
- /*
- * Calculates the number of characters in the CAString.
- */
- gint CAStringLength(const CAString *ca_string)
- {
- gint len;
- const CAString *ca_string_ptr;
- if(ca_string == NULL)
- return(0);
- len = 0;
- ca_string_ptr = ca_string;
- while(CA_STRING_CHARACTER(ca_string_ptr) != '\0')
- {
- len++;
- ca_string_ptr++;
- }
- return(len);
- }
- /*
- * Checks if the string is empty or NULL.
- */
- gboolean CAStringIsEmpty(const CAString *ca_string)
- {
- if(ca_string == NULL)
- return(TRUE);
- if(CA_STRING_CHARACTER(ca_string) == '\0')
- return(TRUE);
- return(FALSE);
- }
- /*
- * Seeks to the index position or end of string.
- *
- * The i specifies the index position, if i is -1 or out of
- * bounds then it will seek to the end of the string.
- */
- CAString *CAStringSeekToIndex(
- CAString *ca_string,
- const gint i
- )
- {
- gint ci;
- if(ca_string == NULL)
- return(NULL);
- ci = 0;
- while(CA_STRING_CHARACTER(ca_string) != '\0')
- {
- if(ci == i)
- break;
- ci++;
- ca_string++;
- }
- return(ca_string);
- }
- /*
- * Seeks to the next occurance of the character, if no such
- * character is found then NULL is returned.
- */
- CAString *CAStringSeekToCharacter(
- CAString *ca_string,
- const gchar c
- )
- {
- if(ca_string == NULL)
- return(NULL);
- while(CA_STRING_CHARACTER(ca_string) != '\0')
- {
- if(CA_STRING_CHARACTER(ca_string) == c)
- return(ca_string);
- ca_string++;
- }
- return(NULL);
- }
- /*
- * Seeks to the next occurance of the character or end of string.
- */
- CAString *CAStringSeekToNextCharacter(
- CAString *ca_string,
- const gchar c
- )
- {
- if(ca_string == NULL)
- return(NULL);
- while(CA_STRING_CHARACTER(ca_string) != '\0')
- {
- if(CA_STRING_CHARACTER(ca_string) == c)
- break;
- ca_string++;
- }
- return(ca_string);
- }
- /*
- * Seeks to the next first occurance of any character in the
- * list.
- *
- * The characters_list and ncharacters specifies the list of
- * characters (it is treated as a data buffer and not a
- * null-terminated string).
- */
- CAString *CAStringSeekToNextCharacters(
- CAString *ca_string,
- const gchar *characters_list,
- const gint ncharacters
- )
- {
- gchar c;
- gint i;
- if(ca_string == NULL)
- return(NULL);
- if((characters_list == NULL) || (ncharacters <= 0))
- return(ca_string);
- while(CA_STRING_CHARACTER(ca_string) != '\0')
- {
- c = CA_STRING_CHARACTER(ca_string);
- for(i = 0; i < ncharacters; i++)
- {
- if(characters_list[i] == c)
- return(ca_string);
- }
- ca_string++;
- }
- return(ca_string);
- }
- /*
- * Seeks to the next position of the CAString that differs in
- * any attribute from the specified starting position or end of
- * string if no attributes differ.
- */
- CAString *CAStringSeekToNextAttribute(CAString *ca_string)
- {
- CAString start_attr;
- if(ca_string == NULL)
- return(NULL);
- start_attr = CA_STRING_ATTRIBUTE(ca_string);
- while(CA_STRING_CHARACTER(ca_string) != '\0')
- {
- if(CA_STRING_ATTRIBUTE(ca_string) != start_attr)
- break;
- ca_string++;
- }
- return(ca_string);
- }
- /*
- * Seeks to the start of the next line or end of string.
- */
- CAString *CAStringNextLine(CAString *ca_string)
- {
- gchar c;
- if(ca_string == NULL)
- return(NULL);
- while(CA_STRING_CHARACTER(ca_string) != '\0')
- {
- c = CA_STRING_CHARACTER(ca_string);
- if((c == CA_STRING_CHARACTER_LINE_END) ||
- (c == CA_STRING_CHARACTER_LINE_BREAK)
- )
- {
- ca_string++;
- break;
- }
- ca_string++;
- }
- return(ca_string);
- }
- /*
- * Seeks to the start of the previous line or start of string.
- */
- CAString *CAStringPreviousLine(
- CAString *ca_string,
- CAString *ca_string_i
- )
- {
- gchar c;
- if(ca_string == NULL)
- return(NULL);
- if(ca_string_i == NULL)
- return(ca_string);
- if(ca_string_i <= ca_string)
- return(ca_string);
- /* Go back one character */
- ca_string_i--;
- c = CA_STRING_CHARACTER(ca_string_i);
- if((c == CA_STRING_CHARACTER_LINE_END) ||
- (c == CA_STRING_CHARACTER_LINE_BREAK)
- )
- {
- if(ca_string_i <= ca_string)
- return(ca_string);
- c = CA_STRING_CHARACTER(ca_string_i - 1);
- if((c == CA_STRING_CHARACTER_LINE_END) ||
- (c == CA_STRING_CHARACTER_LINE_BREAK)
- )
- return(ca_string_i);
- }
- /* Seek to the end of the previous line or start of string */
- while(ca_string_i > ca_string)
- {
- c = CA_STRING_CHARACTER(ca_string_i);
- if((c == CA_STRING_CHARACTER_LINE_END) ||
- (c == CA_STRING_CHARACTER_LINE_BREAK)
- )
- break;
- ca_string_i--;
- }
- /* At the start of the string? */
- if(ca_string_i <= ca_string)
- return(ca_string);
- ca_string_i--;
- /* Seek to the start of the previous line */
- while(ca_string_i > ca_string)
- {
- c = CA_STRING_CHARACTER(ca_string_i);
- if((c == CA_STRING_CHARACTER_LINE_END) ||
- (c == CA_STRING_CHARACTER_LINE_BREAK)
- )
- {
- ca_string_i++;
- break;
- }
- ca_string_i--;
- }
- return(ca_string_i);
- }
- /*
- * Seeks to the start of the current line or start of string.
- */
- CAString *CAStringCurrentLineStart(
- CAString *ca_string,
- CAString *ca_string_i
- )
- {
- gchar c;
- if(ca_string == NULL)
- return(NULL);
- if(ca_string_i == NULL)
- return(ca_string);
- /* Already at the end of a line? */
- c = CA_STRING_CHARACTER(ca_string_i);
- if((c == CA_STRING_CHARACTER_LINE_END) ||
- (c == CA_STRING_CHARACTER_LINE_BREAK)
- )
- {
- /* Seek back before the line end character */
- if(ca_string_i > ca_string)
- ca_string_i--;
- /* Is the character before the line end character also a
- * line end character?
- */
- c = CA_STRING_CHARACTER(ca_string_i);
- if((c == CA_STRING_CHARACTER_LINE_END) ||
- (c == CA_STRING_CHARACTER_LINE_BREAK)
- )
- {
- ca_string_i++;
- return(ca_string_i);
- }
- }
- /* Seek to the start of the current line */
- while(ca_string_i > ca_string)
- {
- c = CA_STRING_CHARACTER(ca_string_i);
- if((c == CA_STRING_CHARACTER_LINE_END) ||
- (c == CA_STRING_CHARACTER_LINE_BREAK)
- )
- {
- ca_string_i++; /* Seek back from the newline */
- break;
- }
- ca_string_i--;
- }
- return(ca_string_i);
- }
- /*
- * Adds the attribute to the CAString region.
- */
- void CAStringSegmentAddAttribute(
- CAString *ca_string,
- const gint start_i, const gint end_i,
- const CAStringAttributes attr
- )
- {
- gint i;
- if((ca_string == NULL) || (attr == 0))
- return;
- i = 0;
- while(CA_STRING_CHARACTER(ca_string) != '\0')
- {
- if(i == start_i)
- break;
- i++;
- ca_string++;
- }
- while(CA_STRING_CHARACTER(ca_string) != '\0')
- {
- CA_STRING_ADD_ATTRIBUTE(ca_string, attr);
- if(i == end_i)
- break;
- i++;
- ca_string++;
- }
- }
- /*
- * Removes the attribute from the CAString region.
- */
- void CAStringSegmentRemoveAttribute(
- CAString *ca_string,
- const gint start_i, const gint end_i,
- const CAStringAttributes attr
- )
- {
- gint i;
- if((ca_string == NULL) || (attr == 0))
- return;
- i = 0;
- while(CA_STRING_CHARACTER(ca_string) != '\0')
- {
- if(i == start_i)
- break;
- i++;
- ca_string++;
- }
- while(CA_STRING_CHARACTER(ca_string) != '\0')
- {
- CA_STRING_REMOVE_ATTRIBUTE(ca_string, attr);
- if(i == end_i)
- break;
- i++;
- ca_string++;
- }
- }
- /*
- * Adds the CA_STRING_ATTRIBUTE_SELECTED attribute to the
- * CAString region and, if clear_existing_selection is TRUE,
- * removes the CA_STRING_ATTRIBUTE_SELECTED attribute from the
- * rest.
- */
- void CAStringSelect(
- CAString *ca_string,
- const gint selection_start_pos,
- const gint selection_end_pos,
- const gboolean clear_existing_selection
- )
- {
- const CAStringAttributes attr = CA_STRING_ATTRIBUTE_SELECTED;
- if(ca_string == NULL)
- return;
- if(clear_existing_selection)
- {
- gint i = 0;
- CAString *ca_string_ptr = ca_string;
- /* Unselect the first region */
- while((CA_STRING_CHARACTER(ca_string_ptr) != '\0') &&
- (i < selection_start_pos)
- )
- {
- CA_STRING_REMOVE_ATTRIBUTE(ca_string_ptr, attr);
- i++;
- ca_string_ptr++;
- }
- /* Select the specified region */
- while((CA_STRING_CHARACTER(ca_string_ptr) != '\0') &&
- (i <= selection_end_pos)
- )
- {
- CA_STRING_ADD_ATTRIBUTE(ca_string_ptr, attr);
- i++;
- ca_string_ptr++;
- }
- /* Unselect the remaining region */
- while(CA_STRING_CHARACTER(ca_string_ptr) != '\0')
- {
- CA_STRING_REMOVE_ATTRIBUTE(ca_string_ptr, attr);
- ca_string_ptr++;
- }
- }
- else
- {
- /* Select the specified region */
- CAString *ca_string_ptr = CAStringSeekToIndex(ca_string, selection_start_pos),
- *ca_string_end = CAStringSeekToIndex(ca_string, selection_end_pos);
- while((CA_STRING_CHARACTER(ca_string_ptr) != '\0') &&
- (ca_string_ptr <= ca_string_end)
- )
- {
- CA_STRING_ADD_ATTRIBUTE(ca_string_ptr, attr);
- ca_string_ptr++;
- }
- }
- }
- /*
- * Removes the CA_STRING_ATTRIBUTE_SELECTED attribute from the
- * CAString region.
- */
- void CAStringUnselectAll(CAString *ca_string)
- {
- const CAStringAttributes attr = CA_STRING_ATTRIBUTE_SELECTED;
- if(ca_string == NULL)
- return;
- while(CA_STRING_CHARACTER(ca_string) != '\0')
- {
- CA_STRING_REMOVE_ATTRIBUTE(ca_string, attr);
- ca_string++;
- }
- }
- /*
- * Inserts a character.
- *
- * The i specifies the insert position, if i is -1 or out of
- * bounds then the new character will be appended.
- */
- CAString *CAStringInsertCharacter(
- CAString *ca_string,
- const gint i,
- const gchar c,
- const CAStringAttributes attr
- )
- {
- gint len;
- CAString *ca_string_i,
- *ca_string_ptr,
- *ca_string_end;
- if(ca_string == NULL)
- {
- len = 1;
- ca_string = (CAString *)g_malloc((len + 1) * sizeof(CAString));
- if(ca_string == NULL)
- return(NULL);
- ca_string_i = ca_string;
- CA_STRING_SET_CHARACTER(ca_string_i, c);
- CA_STRING_SET_ATTRIBUTE(ca_string_i, attr);
- ca_string_i++;
- CA_STRING_SET_CHARACTER(ca_string_i, '\0');
- CA_STRING_SET_ATTRIBUTE(ca_string_i, 0);
- return(ca_string);
- }
- /* Get the current length of the string */
- len = CAStringLength(ca_string);
- /* Increase the allocation for the new character */
- len++;
- ca_string = (CAString *)g_realloc(
- ca_string,
- (len + 1) * sizeof(CAString)
- );
- if(ca_string == NULL)
- return(NULL);
- /* Seek to the insert position */
- ca_string_i = CAStringSeekToIndex(ca_string, i);
- /* Seek to the end of the string */
- ca_string_ptr = ca_string_i;
- while(CA_STRING_CHARACTER(ca_string_ptr) != '\0')
- ca_string_ptr++;
- /* Seek to the end of the string's allocation */
- ca_string_end = ca_string + len;
- /* Shift the data */
- while(ca_string_ptr >= ca_string_i)
- {
- *ca_string_end = *ca_string_ptr;
- ca_string_ptr--;
- ca_string_end--;
- }
- /* Insert the new character */
- CA_STRING_SET_CHARACTER(ca_string_i, c);
- CA_STRING_SET_ATTRIBUTE(ca_string_i, attr);
- return(ca_string);
- }
- /*
- * Appends a character.
- */
- CAString *CAStringAppendCharacter(
- CAString *ca_string,
- const gchar c,
- const CAStringAttributes attr
- )
- {
- gint i,
- len;
- CAString *ca_string_a;
- if(ca_string == NULL)
- {
- len = 1;
- ca_string = (CAString *)g_malloc((len + 1) * sizeof(CAString));
- if(ca_string == NULL)
- return(NULL);
- ca_string_a = ca_string;
- CA_STRING_SET_CHARACTER(ca_string_a, c);
- CA_STRING_SET_ATTRIBUTE(ca_string_a, attr);
- ca_string_a++;
- CA_STRING_SET_CHARACTER(ca_string_a, '\0');
- CA_STRING_SET_ATTRIBUTE(ca_string_a, 0);
- return(ca_string);
- }
- /* Get the current length of the string */
- len = CAStringLength(ca_string);
- /* Record the insert position */
- i = len;
- /* Increase the allocation for the new character */
- len++;
- ca_string = (CAString *)g_realloc(
- ca_string,
- (len + 1) * sizeof(CAString)
- );
- if(ca_string == NULL)
- return(NULL);
- /* Get the append position */
- ca_string_a = ca_string + i;
- /* Append the new character */
- CA_STRING_SET_CHARACTER(ca_string_a, c);
- CA_STRING_SET_ATTRIBUTE(ca_string_a, attr);
- ca_string_a++;
- CA_STRING_SET_CHARACTER(ca_string_a, '\0');
- CA_STRING_SET_ATTRIBUTE(ca_string_a, 0);
- return(ca_string);
- }
- /*
- * Inserts a section of text.
- *
- * The i specifies the insert position, if i is -1 or out of
- * bounds then the new text will be appended.
- */
- CAString *CAStringInsertText(
- CAString *ca_string,
- const gint i,
- const CAString *ca_string2
- )
- {
- gint len, len2;
- const CAString *ca_string2_ptr;
- CAString *ca_string_i,
- *ca_string_ptr,
- *ca_string_end;
- if(ca_string == NULL)
- return(CAStringCopy(ca_string2));
- if(ca_string2 == NULL)
- return(ca_string);
- /* Get the length of the text to insert */
- len2 = CAStringLength(ca_string2);
- if(len2 <= 0)
- return(ca_string);
- /* Get the current length of the string */
- len = CAStringLength(ca_string);
- /* Increase the allocation for the text to insert */
- len += len2;
- ca_string = (CAString *)g_realloc(
- ca_string,
- (len + 1) * sizeof(CAString)
- );
- if(ca_string == NULL)
- return(NULL);
- /* Seek to the insert position */
- ca_string_i = CAStringSeekToIndex(ca_string, i);
- /* Seek to the end of the string */
- ca_string_ptr = ca_string_i;
- while(CA_STRING_CHARACTER(ca_string_ptr) != '\0')
- ca_string_ptr++;
- /* Seek to the end of the string's allocation */
- ca_string_end = ca_string + len;
- /* Shift the data */
- while(ca_string_ptr >= ca_string_i)
- {
- *ca_string_end = *ca_string_ptr;
- ca_string_ptr--;
- ca_string_end--;
- }
- /* Insert the text */
- ca_string_ptr = ca_string_i;
- ca_string2_ptr = ca_string2;
- while(CA_STRING_CHARACTER(ca_string2_ptr) != '\0')
- {
- *ca_string_ptr = *ca_string2_ptr;
- ca_string_ptr++;
- ca_string2_ptr++;
- }
- return(ca_string);
- }
- /*
- * Removes a character.
- */
- CAString *CAStringRemoveCharacter(
- CAString *ca_string,
- const gint i
- )
- {
- gint new_len;
- CAString *ca_string_i,
- *ca_string_ptr,
- *ca_string_end;
- if(ca_string == NULL)
- return(NULL);
- /* Seek to the remove position */
- ca_string_i = CAStringSeekToIndex(ca_string, i);
- /* Shift the data at the removal position one unit down */
- ca_string_ptr = ca_string_i;
- ca_string_end = ca_string_ptr + 1;
- while(CA_STRING_CHARACTER(ca_string_ptr) != '\0')
- {
- *ca_string_ptr = *ca_string_end;
- ca_string_ptr++;
- ca_string_end++;
- }
- /* Update the allocation */
- new_len = CAStringLength(ca_string);
- ca_string = (CAString *)g_realloc(
- ca_string,
- (new_len + 1) * sizeof(CAString)
- );
- if(ca_string == NULL)
- return(NULL);
- return(ca_string);
- }
- /*
- * Removes a section of text.
- */
- CAString *CAStringRemoveText(
- CAString *ca_string,
- const gint i,
- const gint len
- )
- {
- gint new_len;
- CAString *ca_string_i,
- *ca_string_i2;
- if(ca_string == NULL)
- return(NULL);
- /* Nothing to remove? */
- if(len <= 0)
- return(ca_string);
- /* Seek to the remove position */
- ca_string_i = CAStringSeekToIndex(ca_string, i);
- ca_string_i2 = CAStringSeekToIndex(ca_string_i, len);
- if(ca_string_i == ca_string_i2)
- return(ca_string);
- /* Shift the data at the removal position one unit down */
- while(CA_STRING_CHARACTER(ca_string_i2) != '\0')
- {
- *ca_string_i = *ca_string_i2;
- ca_string_i++;
- ca_string_i2++;
- }
- *ca_string_i = *ca_string_i2;
- /* Update the allocation */
- new_len = CAStringLength(ca_string);
- ca_string = (CAString *)g_realloc(
- ca_string,
- (new_len + 1) * sizeof(CAString)
- );
- if(ca_string == NULL)
- return(NULL);
- return(ca_string);
- }
- /*
- * Removes all CA_STRING_CHARACTER_LINE_END characters and
- * replaces them with spaces.
- */
- void CAStringClearLineEnds(CAString *ca_string)
- {
- CAString *ca_string_ptr;
- if(ca_string == NULL)
- return;
- ca_string_ptr = ca_string;
- while(CA_STRING_CHARACTER(ca_string_ptr) != '\0')
- {
- if(CA_STRING_CHARACTER(ca_string_ptr) == CA_STRING_CHARACTER_LINE_END)
- CA_STRING_SET_CHARACTER(ca_string_ptr, ' ');
- ca_string_ptr++;
- }
- }
- /*
- * Removes any leading spaces.
- */
- CAString *CAStringChug(CAString *ca_string)
- {
- gint new_len;
- CAString *ca_string_i,
- *ca_string_i2;
- if(ca_string == NULL)
- return(NULL);
- /* Seek to the first non-space character */
- ca_string_i2 = ca_string;
- while(CA_STRING_CHARACTER(ca_string_i2) == ' ')
- ca_string_i2++;
- /* No leading spaces? */
- if(ca_string_i2 == ca_string)
- return(ca_string);
- /* Shift the data */
- ca_string_i = ca_string;
- while(CA_STRING_CHARACTER(ca_string_i2) != '\0')
- {
- *ca_string_i = *ca_string_i2;
- ca_string_i++;
- ca_string_i2++;
- }
- *ca_string_i = *ca_string_i2;
- /* Update the allocation */
- new_len = CAStringLength(ca_string);
- ca_string = (CAString *)g_realloc(
- ca_string,
- (new_len + 1) * sizeof(CAString)
- );
- if(ca_string == NULL)
- return(NULL);
- return(ca_string);
- }
- /*
- * Removes any tailing spaces.
- */
- CAString *CAStringChomp(CAString *ca_string)
- {
- gint new_len;
- CAString *ca_string_i;
- if(ca_string == NULL)
- return(NULL);
- /* Seek to the end of the string */
- ca_string_i = ca_string;
- while(CA_STRING_CHARACTER(ca_string_i) != '\0')
- ca_string_i++;
- /* Empty string? */
- if(ca_string_i == ca_string)
- return(ca_string);
- /* Seek back one character before the end of string */
- if(ca_string_i > ca_string)
- ca_string_i--;
- /* Seek back any spaces */
- while((ca_string_i > ca_string) &&
- (CA_STRING_CHARACTER(ca_string_i) == ' ')
- )
- ca_string_i--;
- if((CA_STRING_CHARACTER(ca_string_i) != '\0') &&
- (CA_STRING_CHARACTER(ca_string_i) != ' ')
- )
- ca_string_i++;
- CA_STRING_SET_CHARACTER(ca_string_i, '\0');
- /* Update the allocation */
- new_len = ca_string_i - ca_string;
- ca_string = (CAString *)g_realloc(
- ca_string,
- (new_len + 1) * sizeof(CAString)
- );
- if(ca_string == NULL)
- return(NULL);
- return(ca_string);
- }
- /*
- * Removes any leading and tailing spaces.
- */
- CAString *CAStringStrip(CAString *ca_string)
- {
- if(CAStringIsEmpty(ca_string))
- return(ca_string);
- ca_string = CAStringChug(ca_string);
- ca_string = CAStringChomp(ca_string);
- return(ca_string);
- }