ZEDA  1.6.18
Macros | Functions
zeda_string.h File Reference

string manipulations. More...

#include <zeda/zeda_misc.h>
Include dependency graph for zeda_string.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define zIsWS(c)   ( (c) == ' ' || (c) == '\t' )
 check if a charactor is the whitespace. More...
 
#define zIsQuotation(c)   ( (c) == '\'' || (c) == '\"' )
 check if a charactor is a quotation mark. More...
 
#define ZDEFAULT_COMMENT_IDENT   '%'
 
#define ZDEFAULT_TAG_BEGIN_IDENT   '['
 
#define ZDEFAULT_TAG_END_IDENT   ']'
 
#define ZDEFAULT_KEY_IDENT   ':'
 
#define zIndent(n)   zFIndent( stdout,(n) )
 
#define zEndl()   printf( "\n" )
 output a newline charactor to the standard output. More...
 
#define ZEDA_ZTK_SUFFIX   "ztk"
 default suffix of .ztk (Z-Tag-and-Key) file format. More...
 
#define zOpenZTKFile(filename, mode)   zOpenFile( filename, ZEDA_ZTK_SUFFIX, mode )
 open a .ztk file. More...
 

Functions

char * zNullStr (void)
 return the null string. More...
 
char * zStrCopyNC (char *dest, const char *src)
 copy a string without checking the buffer size. More...
 
char * zStrCopy (char *dest, const char *src, size_t size)
 copy a string. More...
 
char * zStrClone (char *str)
 clone a string. More...
 
char * zStrCat (char *dest, const char *src, size_t size)
 concatenate a string to another. More...
 
char * zStrCatPrint (char *str, size_t size, char *fmt,...)
 concatenate a string with a formatted string. More...
 
char * zStrChar (const char *s, size_t size, int c)
 find a specified charactor in a string. More...
 
char * zStrCharNul (const char *s, size_t size, int c)
 find a specified charactor in a string. More...
 
char * zStrrChar (const char *s, size_t size, int c)
 find a specified charactor from the last in a string. More...
 
char * zStrAddChar (char *str, size_t size, char c)
 concatenate a string and a charactor. More...
 
char * zStrInsChar (char *str, size_t size, int cur, char c)
 insert a charactor to a string. More...
 
char * zStrOvrChar (char *str, size_t size, int cur, char c)
 override a charactor to a string. More...
 
char * zStrDelChar (char *str, int cur)
 delete a charactor from a string. More...
 
char * zStrBSChar (char *str, int cur)
 backspace a charactor from a string. More...
 
char * zCutNL (char *str)
 cut a newline charactor. More...
 
char * zToUpper (char *src, char *dest)
 convert a string to the uppercase set. More...
 
char * zToLower (char *src, char *dest)
 convert a string to the lowercase set. More...
 
void zSetDelimiter (char s[])
 specify a delimiter set. More...
 
void zResetDelimiter (void)
 reset a delimiter set. More...
 
void zSetOperator (char s[])
 specify an operator set. More...
 
void zResetOperator (void)
 reset an operator set. More...
 
bool zIsIncludedChar (char c, char *s)
 check if a charactor is included in a specified set. More...
 
bool zIsDelimiter (char c)
 check if a charactor is a delimiter. More...
 
bool zIsOperator (char c)
 check if a charactor is an operator. More...
 
bool zStrIsHex (char *str)
 check if a string represents a hexadecimal number. More...
 
char zFSkipWS (FILE *fp)
 skip whitespaces in a file. More...
 
char * zSSkipWS (char *str)
 skip whitespaces in a string. More...
 
char zFSkipIncludedChar (FILE *fp, char *s)
 skip a certain charactors in a stream. More...
 
char * zSSkipIncludedChar (char *str, char *s)
 skip a certain charactors in a string. More...
 
char zFSkipDelimiter (FILE *fp)
 skip delimiter in a file. More...
 
char * zSSkipDelimiter (char *str)
 skip delimiter in a string. More...
 
void zSetCommentIdent (char ident)
 specify the comment identifier. More...
 
void zResetCommentIdent (void)
 reset the comment identifier. More...
 
char zFSkipComment (FILE *fp)
 skip comments. More...
 
char * zFToken (FILE *fp, char *tkn, size_t size)
 tokenize a file. More...
 
char * zSTokenSkim (char *str, char *tkn, size_t size)
 tokenize a string. More...
 
char * zSToken (char *str, char *tkn, size_t size)
 
char * zFIntToken (FILE *fp, char *tkn, size_t size)
 tokenize an integer value in a file. More...
 
char * zFNumToken (FILE *fp, char *tkn, size_t size)
 tokenize a real number in a file. More...
 
char * zSIntToken (char *str, char *tkn, size_t size)
 tokenize an integer value in a string. More...
 
char * zSNumToken (char *str, char *tkn, size_t size)
 tokenize a real number in a string. More...
 
char * zFInt (FILE *fp, int *val)
 get an integer value from file. More...
 
char * zSInt (char *str, int *val)
 get an integer value from a string. More...
 
char * zFDouble (FILE *fp, double *val)
 get a d-float value in a file. More...
 
char * zSDouble (char *str, double *val)
 get a d-float value in a string. More...
 
void zSetTagIdent (char begin_ident, char end_ident)
 specify the tag identifiers. More...
 
void zResetTagIdent (void)
 reset the tag identifiers. More...
 
bool zTokenIsTag (char *tkn)
 check if a token is a tag. More...
 
char * zExtractTag (char *tag, char *notag)
 extract a tag. More...
 
int zFCountTag (FILE *fp, char *tag)
 count a number of tag tokens in a file. More...
 
bool zTagFScan (FILE *fp, bool(*tag_fscan)(FILE *, void *, char *, bool *), void *instance)
 scan tagged fields in a file. More...
 
void zSetKeyIdent (char ident)
 specify the key identifier. More...
 
void zResetKeyIdent (void)
 reset the key identifier. More...
 
bool zFPostCheckKey (FILE *fp)
 check if the last token is a key. More...
 
int zFCountKey (FILE *fp, char *key)
 count keywords in a file. More...
 
bool zFieldFScan (FILE *fp, bool(*field_fscan)(FILE *, void *, char *, bool *), void *instance)
 scan a field in a file. More...
 
void zFIndent (FILE *fp, int n)
 insert whitespaces before a string. More...
 
char * zGetBasename (char *path, char *name, size_t size)
 strip a directory from a filename. More...
 
char * zGetBasenameDRC (char *name)
 
int zGetDirFilename (char *path, char *dirname, char *filename, size_t size)
 get a directory name and a file name from a path. More...
 
char * zGetSuffix (char *name)
 get a suffix from a filename. More...
 
char * zAddSuffix (char *org, char *suffix, char *dest, size_t size)
 add a suffix to a filename. More...
 
char * zReplaceSuffix (char *org, char *suffix, char *dest, size_t size)
 replace the suffix of a filename to a specified one. More...
 
char * zCutSuffix (char *name)
 cut a suffix from a filename More...
 
FILE * zOpenFile (char filename[], char *suffix, char *mode)
 open a file with default suffix. More...
 
char * zStrSearchKMP (char *text, char *pat)
 search string by Knuth-Morris-Pratt algorithm More...
 
char * zStrSearchBM (char *text, char *pat)
 string search by Boyer-Moore algorithm More...
 

Detailed Description

string manipulations.

Author
Zhidao

Macro Definition Documentation

◆ ZDEFAULT_TAG_BEGIN_IDENT

#define ZDEFAULT_TAG_BEGIN_IDENT   '['

◆ ZDEFAULT_TAG_END_IDENT

#define ZDEFAULT_TAG_END_IDENT   ']'

◆ ZDEFAULT_KEY_IDENT

#define ZDEFAULT_KEY_IDENT   ':'

◆ zIndent

#define zIndent (   n)    zFIndent( stdout,(n) )

◆ zEndl

#define zEndl ( )    printf( "\n" )

output a newline charactor to the standard output.

Function Documentation

◆ zNullStr()

char* zNullStr ( void  )

return the null string.

zNullStr() returns a pointer to the null string "".

Returns
a pointer to the null string.

◆ zStrCopyNC()

char* zStrCopyNC ( char *  dest,
const char *  src 
)

copy a string without checking the buffer size.

zStrCopyNC() copies a string src to a buffer pointed by dest. It supposes that the destination area pointed by dest has enough size.

◆ zStrCopy()

char* zStrCopy ( char *  dest,
const char *  src,
size_t  size 
)

copy a string.

zStrCopy() copies a string src including the terminating null charactor to a buffer pointed by dest. The memory areas for dest and src may overlap. size is the size of the buffer pointed by dest. If the length of src is beyond size, it is clamped and terminated by the null charactor.

◆ zStrClone()

char* zStrClone ( char *  str)

clone a string.

zStrClone() duplicates a string, namely, allocates memory for a string str and copies it there.

Returns
a pointer to the memory allocated. When it fails to allocate memory or str is the null pointer, the null pointer is returned.

◆ zStrCat()

char* zStrCat ( char *  dest,
const char *  src,
size_t  size 
)

concatenate a string to another.

zStrCat() concatenates a string src to another dest. size is the size of the memory area pointed by dest. If the sum of lengths of src and dest is beyond size, the resulted string is clamped.

◆ zStrCatPrint()

char* zStrCatPrint ( char *  str,
size_t  size,
char *  fmt,
  ... 
)

concatenate a string with a formatted string.

zStrCatPrint() concatenates a formatted string fmt to another string str. size is the maximum number of charactors str can store. If the sum of lengths of src and fmt is beyond size, the resulted string is clamped.

The format of fmt and the following variable arguments conform to printf() family.

Returns
zStrCatPrint() returns a pointer str.
Note
zStrCatPrint() is not sufficiently secure since it internally calls vsprintf(), in which the size of fmt is not checked.

◆ zStrChar()

char* zStrChar ( const char *  s,
size_t  size,
int  c 
)

find a specified charactor in a string.

zStrChar() finds the first occurence of a specified charactor c in a string . size is the maximum size of the buffer where s points.

Returns
zStrChar() returns a pointer to the charactor where c appears first in s. If c is not found in s, the null pointer is returned.
See also
zStrCharNul, zStrrChar

◆ zStrCharNul()

char* zStrCharNul ( const char *  s,
size_t  size,
int  c 
)

find a specified charactor in a string.

zStrCharNul() finds the first occurence of a specified charactor c in a string . size is the maximum size of the buffer where s points. The functions is almost the same with zStrChar() except that it returns the pointer to the last null charactor if c is not found in s.

Returns
zStrCharNul() returns a pointer to the charactor where c appears first in s. If c is not found in s, the pointer to the last null charactor is returned.
See also
zStrChar, zStrrChar

◆ zStrrChar()

char* zStrrChar ( const char *  s,
size_t  size,
int  c 
)

find a specified charactor from the last in a string.

zStrrChar() finds the last occurence of a specified charactor c in a string . size is the maximum size of the buffer where s points.

Returns
zStrrChar() returns a pointer to the charactor where c appears the last in s. If c is not found in s, the null pointer is returned.
See also
zStrChar, zStrCharNul

◆ zStrAddChar()

char* zStrAddChar ( char *  str,
size_t  size,
char  c 
)

concatenate a string and a charactor.

zStrAddChar() appends a charactor c to a string str directly. size is the size of a buffer assigned to the string.

Returns
a pointer str is returned if successful. If the buffer is already exhausted, the null pointer is returned.

◆ zStrInsChar()

char* zStrInsChar ( char *  str,
size_t  size,
int  cur,
char  c 
)

insert a charactor to a string.

zStrInsChar() inserts a charactor c to a string str. size is the size of a buffer assigned to the string. cur is the location where c is to be inserted. If cur is over the length of the string, c is appended to the string.

Returns
a pointer to the inserted charactor is returned if successful. If the buffer is already exhausted, the null pointer is returned.

◆ zStrOvrChar()

char* zStrOvrChar ( char *  str,
size_t  size,
int  cur,
char  c 
)

override a charactor to a string.

zStrOvrChar() overrides a charactor c to a string str. size is the size of a buffer assigned to the string. cur is the location where c is to be overrided. If cur is over the length of the string, c is appended to the string.

Returns
a pointer to the overridden charactor is returned if successful.

◆ zStrDelChar()

char* zStrDelChar ( char *  str,
int  cur 
)

delete a charactor from a string.

zStrDelChar() deletes a charactor from a string str. cur is the location where a charactor is to be deleted.

Returns
a pointer to the deleted location is returned.

◆ zStrBSChar()

char* zStrBSChar ( char *  str,
int  cur 
)

backspace a charactor from a string.

zStrBSChar() backspaces a charactor from a string str. cur is the location where a charactor is to be withdrawn.

Returns
a pointer to the withdrawn location is returned.

◆ zCutNL()

char* zCutNL ( char *  str)

cut a newline charactor.

zCutNL() directly cuts a newline charactor at the end of a string pointed by str, and puts the null charactor '\0' there instead.

Returns
a pointer str is returned.

◆ zToUpper()

char* zToUpper ( char *  src,
char *  dest 
)

convert a string to the uppercase set.

zToUpper() copies a string str to dest with all lowercase alphabets included in str converted to uppercase charactors.

Returns
a pointer dest is returned.

◆ zToLower()

char* zToLower ( char *  src,
char *  dest 
)

convert a string to the lowercase set.

zToLower() copies a string str to dest with all uppercase alphabets included in str converted to lowercase charactors.

Returns
a pointer dest is returned.

◆ zSetTagIdent()

void zSetTagIdent ( char  begin_ident,
char  end_ident 
)

specify the tag identifiers.

◆ zResetTagIdent()

void zResetTagIdent ( void  )

reset the tag identifiers.

◆ zTokenIsTag()

bool zTokenIsTag ( char *  tkn)

check if a token is a tag.

zTokenIsTag() checks if a token pointed by tkn is a tag, namely, tkn is bracketed or not.

Returns
a boolean value is returned.

◆ zExtractTag()

char* zExtractTag ( char *  tag,
char *  notag 
)

extract a tag.

zExtractTag() extracts a keyword from a bracketted token tag, and puts it where notag points. ex. suppose tag is "[xxx]", then notag is "xxx".

Returns
a pointer notag is returned.
Note
notag is allowed to be the same with tag. Namely, zExtractTag( str, str ) is a valid expression.

◆ zFCountTag()

int zFCountTag ( FILE *  fp,
char *  tag 
)

count a number of tag tokens in a file.

zFCountTag() counts bracketed tokens with a keyword tag in a file fp. After counting, the current position of fp is rewinded to the head of file.

Returns
The number of the counted tags in the file is returned.
See also
zFCountKey

◆ zTagFScan()

bool zTagFScan ( FILE *  fp,
bool(*)(FILE *, void *, char *, bool *)  tag_fscan,
void *  instance 
)

scan tagged fields in a file.

zTagFScan() scans all tagged fields in a file fp. tag_fscan defines persing rules to accept some tags. instance is a pointer to an instance of a specific class to be processed.

fp and are given to tag_fscan as the first and second arguments, respectively. The third argument of tag_fscan is for a buffer that stores the token previously scanned, which may be used to further tokenize in tag_fscan. It is up to programmers whether they use the fourth argument. It can be utilized to catch something illegal in tag_fscan and try any exceptions. It is returned by zTagFScan().

Returns
zTagFScan() returns the fourth value of tag_fscan, which may catch something illegal in the persing process. At default, the true value is returned.

◆ zSetKeyIdent()

void zSetKeyIdent ( char  ident)

specify the key identifier.

◆ zResetKeyIdent()

void zResetKeyIdent ( void  )

reset the key identifier.

◆ zFPostCheckKey()

bool zFPostCheckKey ( FILE *  fp)

check if the last token is a key.

◆ zFCountKey()

int zFCountKey ( FILE *  fp,
char *  key 
)

count keywords in a file.

zFCountKey() counts keywords which coincide with a string pointed by key in a file fp between a tag and the next tag. After counting, the current position of fp is rewinded where it pointed before counting.

Returns
The number of the counted keywords is returned.
See also
zFCountTag

◆ zFieldFScan()

bool zFieldFScan ( FILE *  fp,
bool(*)(FILE *, void *, char *, bool *)  field_fscan,
void *  instance 
)

scan a field in a file.

zFieldFScan() scans a field in a file pointed by fp from the current position. field_fscan defines persing rules to process a specific class. instance is a pointer to an instance of the specific class.

fp and are given to field_fscan as the first and second arguments, respectively. The third argument of field_fscan is for a buffer that stores the token previously scanned, which may be used to further tokenize in field_fscan. It is up to programmers whether they use the fourth argument. It can be utilized to catch something illegal in field_fscan and try any exceptions. It is returned by zFieldFScan().

Returns
zFieldFScan() returns the fourth value of field_fscan, which may catch something illegal in the persing process. At default, the true value is returned.

◆ zFIndent()

void zFIndent ( FILE *  fp,
int  n 
)

insert whitespaces before a string.

zFIndent() inserts n whitespaces at the current position of a file fp. For an indentation of the output to the standard output, use zIndent().