format.c File Reference

Collection of string handling utilities. More...

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "hlrmisc.h"
#include "log.h"
#include "format.h"

Go to the source code of this file.

Functions

int getLine (FILE *stream, char **buffer, int *buflen)
 Read an arbitrary long line from stream, functionality analog to gets(3S).
int isBlankStr (char *s)
 Check if s is blank.
char * strCaseStr (char *s, char *t)
 Case-insensitive version of strstr(3C) from the C-libarary.
char * strCopySubstr (char *string, char begin, char end, Array substr)
 From a supplied string copy a substring delimited by two supplied characters, excluding these characters.
int strEndsWith (char *s, char *suffix)
 Checks if string 's' end with 'suffix'.
int stringAppendf (Stringa str, char *format,...)
 Same as stringPrintf(), but the result is appended to 'str'.
void stringCat (Array s1, char *s2)
 Appends null-terminated string s2 to s1.
void stringCatChar (Stringa s, char c)
 Append single character 'c' to a Stringa 's'.
void stringCatInt (Array s, int i)
 Convert i into a string and append it to string Array s.
void stringChop (Stringa s, int n)
 Remove the trailing n chars from s.
void stringClear (Array s1)
 Erases the contents of Array s1, leaving an empty string.
void stringCpy (Array s1, char *s2)
 Copies null-terminated string s2 to s1.
Array stringCreate (int initialSize)
 Create an array of char and make it null-terminated.
char * stringCut (Array s, int pos, int len)
 Removes a string segment.
void stringInsert (Stringa s, int p, char *i)
 Insert string 'i' at position p into array string 's'.
void stringNCat (Stringa s1, char *s2, int n)
 Appends first n chars from string s2 to s1.
void stringNCpy (Stringa s1, char *s2, int n)
 Analogous to strncpy() from the C library.
char * stringPrintBuf (char *format,...)
 Like sprintf() from the standard C library, but with unlimited length and own memory management.
int stringPrintf (Stringa str, char *format,...)
 Formatted printing into a Stringa (similar to sprintf()).
void stringTerminate (Array s)
 Terminate string.
void stringTerminateI (Array s, int i)
 Terminate string at the i+1 char.
void stringTerminateP (Array s, char *cp)
 Terminate string at position 'cp'.
int stringTranslate (Array s, char *fromChars, char *toChars)
 Exactly like strTranslate(), but with a Stringa.
int stringTrim (Array s, char *left, char *right)
 Exactly like strTrim(), but with a Stringa.
int stripNlGetLength (char *line)
 Strip the trailing
character off line, if present and return the length of the resulting line.
void strReplace (char **s1, char *s2)
 Replace previous contents of s1 with copy of s2.
void strScramble (char *s)
 Encrypt the input string such that it is unreadable to humans and can easily be strUnscrambled() again.
int strTranslate (char *s, char *fromChars, char *toChars)
 Translates each character from 's' which matches one of the characters in 'fromChars' with the corresponding character from 'toChars' or, if this position in 'toChars' is not filled, deletes this character from s, thus shortening 's'.
int strTrim (char *s, char *left, char *right)
 Remove leading and trailing characters from s.
void strUnscramble (char *s)
 Antidot for strScramble().
void textClear (Texta a)
 Free the strings in 'a' and make the array empty.
Texta textClone (Array a)
 Produce a clone of the Array of strings 'a'.
void textDestroyFunc (Texta a)
 Free storage allocated to text a.
Texta textFieldtok (char *s, char *sep)
 Splits string 's' in words separated by any of the characters from string 'sep'.
Texta textFieldtokP (char *s, char *sep)
 Same as textFieldtok() but does not alter its input 's'.
void textJoin (Stringa s, char *sep, Array a)
 textJoin.
Texta textStrtok (char *s, char *sep)
 Splits string 's' in words separated by one or more characters from string 'sep'.
Texta textStrtokP (char *s, char *sep)
 Same as textStrtok(), but does not alter its input 's'.
void textUniqKeepOrder (Texta t)
 Remove duplicate strings from t without changing the order.
void tolowerStr (char *s)
 Converts string to lowercase.
void toupperStr (char *s)
 Converts string to uppercase.
WordIter wordIterCreate (char *s, char *seps, int manySepsAreOne)
 Create an iterator returning the words of 's', broken at any char from 'seps'.
void wordIterDestroy_func (WordIter this1)
 wordIterDestroy_func.
char * wordNextG (WordIter this1, int *lenP)
 wordNextG.


Detailed Description

Collection of string handling utilities.

Author:
Adapted by Lukas Habegger (lukas.habegger@yale.edu)

Definition in file format.c.


Function Documentation

int getLine ( FILE *  stream,
char **  buffer,
int *  buflen 
)

Read an arbitrary long line from stream, functionality analog to gets(3S).

   usage example:
     char *line = NULL ;
     int buflen ;
     while (getLine(stdin, &line, &buflen)) { 
       printf("%s", line) ;
     }
     hlr_free(line) ;
   
Parameters:
[in] stream 
[in] buffer Pointer to a string
[in] buflen Current length of buffer, for interal use of getLine()
[out] buffer Might be re-alloated
[out] buflen Adjusted
Returns:
Number of chars put into buffer, including trailing
(except for the last line), 0 if EOF encountered
Postcondition:
Next call to getLine gives next line

Definition at line 1139 of file format.c.

int isBlankStr ( char *  s  ) 

Check if s is blank.

Returns:
True, if string consists of whitespace only or is of length 0, else returns false

Definition at line 875 of file format.c.

char* strCaseStr ( char *  s,
char *  t 
)

Case-insensitive version of strstr(3C) from the C-libarary.

Parameters:
[in] s String to be searched in (subject)
[in] t String to look for in s (query)
Returns:
If t is the empty string return s, else if t does not occur in s return NULL, else pointer to first position of t in s

Definition at line 686 of file format.c.

char* strCopySubstr ( char *  string,
char  begin,
char  end,
Array  substr 
)

From a supplied string copy a substring delimited by two supplied characters, excluding these characters.

Parameters:
[in] string String to copy from
[in] begin Start copying after the leftmost occurrence of this character in string
[in] end Stop copying before the leftmost occurrence of this character from occurrence of begin on; may be null-terminated to copy to the end of string
[in] substr Stringa, must exist
[out] substr Filled with string extracted; empty string if nothing extracted;
Returns:
Position after location of end, NULL if no substring extracted

Definition at line 729 of file format.c.

int strEndsWith ( char *  s,
char *  suffix 
)

Checks if string 's' end with 'suffix'.

Parameters:
[in] s Not NULL
[in] suffix Not NULL; if empty, 1 will be returned
Returns:
1 if yes, 0 if no

Definition at line 1346 of file format.c.

int stringAppendf ( Stringa  str,
char *  format,
  ... 
)

Same as stringPrintf(), but the result is appended to 'str'.

Returns:
Number of chars appended

Definition at line 593 of file format.c.

void stringCat ( Array  s1,
char *  s2 
)

Appends null-terminated string s2 to s1.

Parameters:
[in] s1 Valid Stringa
[in] s2 Null-terminated string
[out] s1 With contents of s2 appended

Definition at line 267 of file format.c.

void stringChop ( Stringa  s,
int  n 
)

Remove the trailing n chars from s.

If n is equal or larger than the length of s, the result is the empty string.

Parameters:
[in] s A valid Stringa
[in] n How many chars to chop off, n >= 0
[out] s Modified

Definition at line 168 of file format.c.

void stringCpy ( Array  s1,
char *  s2 
)

Copies null-terminated string s2 to s1.

Same functionality as strcpy()

Parameters:
[in] s1 Array of char, null-termintated
[in] s2 String need not be null-terminated, but may not contain internal nulls
[out] s1 With contents of s2

Definition at line 348 of file format.c.

Array stringCreate ( int  initialSize  ) 

Create an array of char and make it null-terminated.

 * This pattern occurs very often:
 * Stringa a = NULL ;
 * if (a)
 *   stringClear(a) ;
 * else
 *   a = stringCreate(n) ;
 * 
 * Therefore there is this shortcut:
 * #define stringCreateClear(s,n) {if(s) stringClear(s); else s=stringCreate(n);}
 * 

Definition at line 81 of file format.c.

char* stringCut ( Array  s,
int  pos,
int  len 
)

Removes a string segment.

Removes the segment [pos..pos+len-1] from s and returns a pointer to a copy of this segment. If pos is beyond the end the string, nothing is done and NULL returned; If pos is within the string but pos+len-1 is beyond the end of the string or len is -1, the tail of string starting at pos is removed.

Parameters:
[in] s A valid Stringa
[in] pos Start position of the segment to extract (based on 0), pos >= 0
[in] len Number of chars to cut out, -1 means as many as possible; it is ok if len asks for more than is actually present; if len is 0 s is not changed and an empty string returned.
[out] s Without specified segment
Returns:
Pointer to segment cut out this memory stays stable until the next call to this routine. This memory belongs to this routine; it may be read and modified but not realloc'd or free'd by the user of this routine.

Definition at line 197 of file format.c.

void stringInsert ( Stringa  s,
int  p,
char *  i 
)

Insert string 'i' at position p into array string 's'.

Parameters:
[in] s Destination
[in] p 0 .. arrayMax(s) (=strlen(string(s))) 0 means prepending 'i' to 's', arrayMax(s) means appending (same as stringCat())
[in] i Source
[out] s Modified

Definition at line 238 of file format.c.

void stringNCat ( Stringa  s1,
char *  s2,
int  n 
)

Appends first n chars from string s2 to s1.

Same functionality as strncat().

Parameters:
[in] s1 Array of char, null-termintated
[in] s2 String need not be null-terminated, but may not contain internal nulls
[in] n Number of chars from s2 to be copied, if n <= 0, s1 is not changed. n larger than the length of s2 is ok, too.
[out] s1 With first min(n,strlen(s2)) chars from s2 appended

Definition at line 321 of file format.c.

void stringNCpy ( Stringa  s1,
char *  s2,
int  n 
)

Analogous to strncpy() from the C library.

Analogous to stringNCat() from this module, except that s1 is cleared before appending.

Definition at line 305 of file format.c.

char* stringPrintBuf ( char *  format,
  ... 
)

Like sprintf() from the standard C library, but with unlimited length and own memory management.

Parameters:
[in] format Template to be filled
[in] ... Variable number of arguments, must match 'format'
Returns:
Formatted string; memory managed by this routine; may be written to by user, but not free'd or realloced; stable until next call to this routine.

Definition at line 625 of file format.c.

int stringPrintf ( Stringa  str,
char *  format,
  ... 
)

Formatted printing into a Stringa (similar to sprintf()).

Parameters:
[in] str Target string which receives formatted printing as in sprintf(); must not be NULL; must be created using stringCreate()
[in] format Format string as in sprintf(), only difference: argument position manipulation e.g. "%2$15.12s" or "%*3$s" is not supported
[in] ... Variable argument list, must match format string, else behaviour is undefined
[out] str Contains formatted output
Returns:
Number of characters printed into str not counting the trailing null-termination character
Precondition:
None
Postcondition:
None

Definition at line 563 of file format.c.

void stringTerminate ( Array  s  ) 

Terminate string.

Parameters:
[in] s Array of char, not yet null-terminated
[out] s Now of type Stringa, i.e. an Array of char* (null-terminated), suitable for use in all string* functions.

Definition at line 96 of file format.c.

void stringTerminateI ( Array  s,
int  i 
)

Terminate string at the i+1 char.

Parameters:
[in] i An index in s, 0 <= i <= stringLen (s), if i > stringLen (s) or i < 0 is an error
[in] s A valid Stringa of length i
[out] s *cp in s will be null-terminated and stringLen(s) is adjusted
Note:
stringTerminateI (s,0) is equivalent to stringClear (s)

Definition at line 128 of file format.c.

void stringTerminateP ( Array  s,
char *  cp 
)

Terminate string at position 'cp'.

Parameters:
[in] cp A pointer to a position within the string
[in] s A valid Stringa
[out] s *cp in s will be null-terminated and stringLen(s) is adjusted
Note:
stringTerminateP (s, string (s)) is equivalent to stringClear (s)

Definition at line 114 of file format.c.

int stringTranslate ( Array  s,
char *  fromChars,
char *  toChars 
)

Exactly like strTranslate(), but with a Stringa.

Parameters:
[in] s Must exist

Definition at line 389 of file format.c.

int stringTrim ( Array  s,
char *  left,
char *  right 
)

Exactly like strTrim(), but with a Stringa.

Parameters:
[in] s Must exist

Definition at line 372 of file format.c.

int stripNlGetLength ( char *  line  ) 

Strip the trailing
character off line, if present and return the length of the resulting line.

This function is typically used in conjunction with getLine().

Parameters:
[in] line 
[out] line 
Returns:
Length of output line

Definition at line 1185 of file format.c.

void strReplace ( char **  s1,
char *  s2 
)

Replace previous contents of s1 with copy of s2.

s2 can be NULL. This function is the same as strdup() from the C library, except that it free()s the target string before duplicating.

Parameters:
[in] s1 Place where a pointer to a string is stored
[in] s2 Contents of s2 will replace contents of s1
[out] s2 Pervious contents free()d, new memory allocated

Definition at line 708 of file format.c.

void strScramble ( char *  s  ) 

Encrypt the input string such that it is unreadable to humans and can easily be strUnscrambled() again.

Parameters:
[in] s Must not contain 0xFF
[out] s Scrambled

Definition at line 849 of file format.c.

int strTranslate ( char *  s,
char *  fromChars,
char *  toChars 
)

Translates each character from 's' which matches one of the characters in 'fromChars' with the corresponding character from 'toChars' or, if this position in 'toChars' is not filled, deletes this character from s, thus shortening 's'.

This function resembles the Unix command and the Perl function 'tr'.

   example: strTranslate("abc", "ac", "b") modifies
            "abc" into "bb" and returns 2
            strTranslate("a|b|c", "|", "|")
            just counts the number of '|' chars
  
Parameters:
[in] s 
[in] fromChars 
[in] toChars 
[out] s 
Returns:
Number of chars translated or modified
Postcondition:
strTranslate_resultLen contains the length of the string after processing; used by stringTranslate() to avoid using strlen()

Definition at line 767 of file format.c.

int strTrim ( char *  s,
char *  left,
char *  right 
)

Remove leading and trailing characters from s.

  example: strTrim("<<=text=>>", "=<", "=")
           returns 7 and leaves output "text=>>" 
 
Parameters:
[in] s Zero-terminated string of char or NULL (nothing will happen)
[in] left Set of chars to be removed from left end of s, NULL or empty string to leave beginning of s as is
[in] right Set of chars to be removed from right end of s, NULL or empty string to leave tail of s as is
[out] s Changed
Returns:
Length of s after trim

Definition at line 804 of file format.c.

void textClear ( Texta  a  ) 

Free the strings in 'a' and make the array empty.

The strings must have been allocated using hlr_malloc() or hlr_strdup() if you expect hlr_getAllocCnt() to work.

Definition at line 906 of file format.c.

Texta textClone ( Array  a  ) 

Produce a clone of the Array of strings 'a'.

Parameters:
[in] a Array of strings, NULL ok
Returns:
Array of char* with same contents as 'a', but new memory allocated
Note:
The user of this routine is responsible for freeing the memory allocated

Definition at line 938 of file format.c.

void textDestroyFunc ( Texta  a  ) 

Free storage allocated to text a.

Parameters:
[in] a Array of char*; the char* should have been allocated with hlr_malloc()
Note:
This function is private to this module. Don't call it from your program. Use textDestroy() instead

Definition at line 922 of file format.c.

Texta textFieldtok ( char *  s,
char *  sep 
)

Splits string 's' in words separated by any of the characters from string 'sep'.

Parameters:
[in] s Input string
[in] sep Separation character(s)
[out] s The contents changed!
Returns:
An Array of pointers to C-strings. The user of this routine is responsible for freeing the Array returned after use; (e.g. using textDestroy() )

Definition at line 1003 of file format.c.

Texta textFieldtokP ( char *  s,
char *  sep 
)

Same as textFieldtok() but does not alter its input 's'.

Suffix 'P' stands for 'Preserving its input'

Definition at line 1020 of file format.c.

void textJoin ( Stringa  s,
char *  sep,
Array  a 
)

textJoin.

   example:
     Texta t = textStrtok("a b c", " ") ;
     Stringa s = stringCreate(10) ;
     textJoin(s, "-", t) ;
     puts(string(s)) ;
   prints "a-b-c"
   
Parameters:
[in] s Contents will be overridden
[in] sep Separator between elements, e.g. ","
[in] a Array of char* (can be empty, but not NULL)
[out] s Filled; empty string if empty Array

Definition at line 1045 of file format.c.

Texta textStrtok ( char *  s,
char *  sep 
)

Splits string 's' in words separated by one or more characters from string 'sep'.

Same result as repeated calls to strtok() from the standard C libary.

Parameters:
[in] s Input string
[in] sep Separation character(s)
[out] s The contents changed!
Returns:
An Array of pointers to C-strings. The user of this routine is responsible for freeing the Array returned after use; (e.g. using textDestroy() )

Definition at line 968 of file format.c.

Texta textStrtokP ( char *  s,
char *  sep 
)

Same as textStrtok(), but does not alter its input 's'.

Suffix 'P' stands for 'Preserving its input'

Definition at line 985 of file format.c.

void textUniqKeepOrder ( Texta  t  ) 

Remove duplicate strings from t without changing the order.

   Note on runtime complexity: 
     Execution time: O(n*n*log n)  (where n is arrayMax(t))

     The implementation below has a lot of room for improvments, e.g.
     - using a hashed lookup table for seen values
       would reduce runtime complexity to O(n)
     - if no hashed lookup table availabe:
       s = arrayCopy(t)        -- O(n)
       arraySort(s)            -- O(n * log n)
       remove duplicates in s  -- O(n)
       f = arrayCopy(s)        -- O(n)
       remove duplicates in t with lookup in s and flagging in f
                               -- O(n * log n) 
       would trade space for time
   
Parameters:
[in] t 
[in] t Duplicates removed, first occurences kept
Note:
It must be ok to free elements from t

Definition at line 1082 of file format.c.

WordIter wordIterCreate ( char *  s,
char *  seps,
int  manySepsAreOne 
)

Create an iterator returning the words of 's', broken at any char from 'seps'.

If 'manySepsAreOne' is 1, consequtive stretches of separators are treated as one separator (so there are no empty words).

Parameters:
[in] s String to break
[in] seps Set of word separator chars
[in] manySepsAreOne 1 or 0
[out] s Is destroyed
Returns:
WordIter object for use in wordNext() and wordIterDestroy()
Note:
sep must be kept stable during the whole scan

if 's' is the empty string, then in mode manySepsAreOne==1, wordNext() will immediately return NULL, in mode manySepsAreOne==0, wordNext() will return one empty string, then NULL

Definition at line 1252 of file format.c.

void wordIterDestroy_func ( WordIter  this1  ) 

wordIterDestroy_func.

Parameters:
[in] this1 Created by wordIterCreate*
Postcondition:
'this1' is no more accessible
Note:
Do not call in your programs, use wordIterDestroy() instead.

Definition at line 1232 of file format.c.

char* wordNextG ( WordIter  this1,
int *  lenP 
)

wordNextG.

Parameters:
[in] this1 Created by wordTokIterCreate() or wordFldIterCreate()
[in] lenP Valid place to put an int; NULL if no interest
[out] lenP If lenP not NULL: strlen() of result, computed efficiently
Returns:
NULL if no more word, else pointer to beginning of null-terminated word

Definition at line 1280 of file format.c.


Generated on Fri Aug 7 15:46:43 2009 for BIOS by  doxygen 1.5.7.1