format.h File Reference

#include <stdio.h>
#include <string.h>
#include "plabla.h"
#include "array.h"
#include "hlrmisc.h"

Go to the source code of this file.

Data Structures

struct  wordIterStruct
 WordIter. More...

Defines

#define strDiffer(s1, s2)   (strcmp((s1),(s2)) != 0)
 Check if s1 and s2 are different.
#define strEqual(s1, s2)   (strcmp((s1),(s2)) == 0)
 Check if s1 and s2 are equal.
#define string(stringa)   arrp(stringa,0,char)
 Convert Stringa to char*.
#define Stringa   Array
 Array of char that is null-terminated (Stringa).
#define stringCreateClear(s, n)   {if(s) stringClear(s); else s=stringCreate(n);}
 stringCreateClear.
#define stringCreateOnce(s, n)   {if(!s) s=stringCreate(n);}
 stringCreateOnce.
#define stringDestroy   arrayDestroy
 Destroy a Stringa.
#define stringLen(stringa)   (arrayMax(stringa)-1)
 Get the length of a Stringa (not counting the null-termination character).
#define strNDiffer(s1, s2, n)   (strncmp((s1),(s2),n) != 0)
 Check if the first n characters of s1 and s2 are different.
#define strNEqual(s1, s2, n)   (strncmp((s1),(s2),n) == 0)
 Check if the first n characters of s1 and s2 are equal.
#define strStartsWith(s1, s2)   (strncmp((s1),(s2),strlen(s2)) == 0)
 Check if s1 starts with s2.
#define strStartsWithC(s1, s2)   (strncmp((s1),(s2),sizeof(s2)-1) == 0)
 Check if s1 starts with s2.
#define Texta   Array
 A Texta is an Array of char*.
#define textAdd(t, s)   (array((t),arrayMax(t),char*)=hlr_strdup(s))
 Add a string to the end of a Texta.
#define textCreate(initialSize)   arrayCreate(initialSize,char*)
 Create a Texta.
#define textCreateClear(t, n)   {if(t) textClear(t); else t=textCreate(n);}
 textCreateClear.
#define textDestroy(x)   ((x) ? textDestroyFunc(x), x=NULL, 1 : 0)
 Destroy a Texta.
#define textItem(text, index)   arru((text),(index),char*)
 Get a pointer to the ith line in a Texta.
#define wordFldIterCreate(s, seps)   wordIterCreate(s,seps,0)
 wordFldIterCreate.
#define wordIterDestroy(this)   (wordIterDestroy_func(this), this=0)
 wordDestroy.
#define wordNext(this1)   (wordNextG((this1),NULL))
 wordNext.
#define wordTokIterCreate(s, seps)   wordIterCreate(s,seps,1)
 wordTokIterCreate.

Typedefs

typedef struct wordIterStructWordIter
 WordIter.

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 *pszString, char cBegin, char cEnd, Array acSubstr)
 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 stringCatChar (Stringa s, char c)
 Append single character 'c' to a Stringa '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.
Stringa stringCreate (int initialSize)
 Create an array of char and make it null-terminated.
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 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 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 thisOne)
 wordIterDestroy_func.
char * wordNextG (WordIter this1, int *lenP)
 wordNextG.


Detailed Description

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

Definition in file format.h.


Define Documentation

#define stringCreateClear ( s,
 )     {if(s) stringClear(s); else s=stringCreate(n);}

stringCreateClear.

See also:
stringClear()

stringCreate()

Definition at line 83 of file format.h.

#define stringCreateOnce ( s,
 )     {if(!s) s=stringCreate(n);}

stringCreateOnce.

See also:
stringCreate()

Definition at line 89 of file format.h.

#define stringDestroy   arrayDestroy

Destroy a Stringa.

See also:
arrayDestroy()

Definition at line 95 of file format.h.

#define strStartsWith ( s1,
s2   )     (strncmp((s1),(s2),strlen(s2)) == 0)

Check if s1 starts with s2.

strStartsWith() always works, but is slower than strStartsWithC()

Note:
s2 must not be an expression with side effects since it is evaluated twice

Definition at line 188 of file format.h.

#define strStartsWithC ( s1,
s2   )     (strncmp((s1),(s2),sizeof(s2)-1) == 0)

Check if s1 starts with s2.

Same as strStartsWith, but can only be used if s2 is a string constant, e.g. if (strStartsWithC(s, "CC ")) ...strStartsWith() always works, but is slower than strStartsWithC()

Definition at line 194 of file format.h.

#define Texta   Array

A Texta is an Array of char*.

The memory referenced by the char pointers belongs to the Texta

Definition at line 205 of file format.h.

#define textCreate ( initialSize   )     arrayCreate(initialSize,char*)

Create a Texta.

See also:
arrayCreate()

Definition at line 213 of file format.h.

#define textCreateClear ( t,
 )     {if(t) textClear(t); else t=textCreate(n);}

textCreateClear.

See also:
textClear()

textCreate()

Definition at line 221 of file format.h.

#define textDestroy (  )     ((x) ? textDestroyFunc(x), x=NULL, 1 : 0)

Destroy a Texta.

See also:
textDestroyFunc()

Definition at line 227 of file format.h.

#define wordFldIterCreate ( s,
seps   )     wordIterCreate(s,seps,0)

wordFldIterCreate.

See also:
wordIterCreate()

Definition at line 259 of file format.h.

#define wordIterDestroy ( this   )     (wordIterDestroy_func(this), this=0)

wordDestroy.

See also:
wordIterDestroy_func()

Definition at line 272 of file format.h.

#define wordNext ( this1   )     (wordNextG((this1),NULL))

wordNext.

See also:
wordNextG()

Definition at line 265 of file format.h.

#define wordTokIterCreate ( s,
seps   )     wordIterCreate(s,seps,1)

wordTokIterCreate.

See also:
wordIterCreate()

Definition at line 253 of file format.h.


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 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.

Stringa 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.

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 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 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