Файл src/utils.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "allheaders.h"
#include <sys/time.h>
#include <sys/resource.h>

Функции

l_int32 returnErrorInt (const char *msg, const char *procname, l_int32 ival)
l_float32 returnErrorFloat (const char *msg, const char *procname, l_float32 fval)
void * returnErrorPtr (const char *msg, const char *procname, void *pval)
void returnErrorVoid (const char *msg, const char *procname)
void l_error (const char *msg, const char *procname)
void l_errorString (const char *msg, const char *procname, const char *str)
void l_errorInt (const char *msg, const char *procname, l_int32 ival)
void l_errorFloat (const char *msg, const char *procname, l_float32 fval)
void l_warning (const char *msg, const char *procname)
void l_warningString (const char *msg, const char *procname, const char *str)
void l_warningInt (const char *msg, const char *procname, l_int32 ival)
void l_warningFloat (const char *msg, const char *procname, l_float32 fval)
void l_info (const char *msg, const char *procname)
void l_infoString (const char *msg, const char *procname, const char *str)
void l_infoInt (const char *msg, const char *procname, l_int32 ival)
void l_infoInt2 (const char *msg, const char *procname, l_int32 ival1, l_int32 ival2)
void l_infoFloat (const char *msg, const char *procname, l_float32 fval)
void l_infoFloat2 (const char *msg, const char *procname, l_float32 fval1, l_float32 fval2)
char * stringNew (const char *src)
l_int32 stringReplace (char **pdest, const char *src)
char * stringJoin (const char *src1, const char *src2)
char * stringReverse (const char *src)
char * strtokSafe (char *cstr, const char *seps, char **psaveptr)
l_int32 stringSplitOnToken (char *cstr, const char *seps, char **phead, char **ptail)
char * stringRemoveChars (const char *src, const char *remchars)
l_int32 stringFindSubstr (const char *src, const char *sub, l_int32 *ploc)
char * stringReplaceSubstr (const char *src, const char *sub1, const char *sub2, l_int32 *pfound, l_int32 *ploc)
char * stringReplaceEachSubstr (const char *src, const char *sub1, const char *sub2, l_int32 *pcount)
l_int32 arrayFindSequence (const l_uint8 *data, l_int32 datalen, const l_uint8 *sequence, l_int32 seqlen, l_int32 *poffset, l_int32 *pfound)
void * reallocNew (void **pindata, l_int32 oldsize, l_int32 newsize)
l_uint8arrayRead (const char *fname, l_int32 *pnbytes)
l_uint8arrayReadStream (FILE *fp, l_int32 *pnbytes)
l_int32 nbytesInFile (const char *filename)
l_int32 fnbytesInFile (FILE *fp)
l_int32 arrayWrite (const char *filename, const char *operation, void *data, l_int32 nbytes)
l_uint16 convertOnLittleEnd16 (l_uint16 shortin)
l_uint16 convertOnBigEnd16 (l_uint16 shortin)
l_uint32 convertOnLittleEnd32 (l_uint32 wordin)
l_uint32 convertOnBigEnd32 (l_uint32 wordin)
FILE * fopenReadStream (const char *filename)
l_int32 splitPathAtDirectory (const char *pathname, char **pdir, char **ptail)
l_int32 splitPathAtExtension (const char *pathname, char **pbasename, char **pextension)
char * genPathname (const char *dir, const char *fname)
char * genTempFilename (const char *dir, const char *extension)
l_int32 extractNumberFromFilename (const char *fname, l_int32 numpre, l_int32 numpost)
void startTimer (void)
l_float32 stopTimer (void)

Переменные

static const char sepchar = '/'
static struct rusage rusage_before
static struct rusage rusage_after

Функции

l_int32 arrayFindSequence ( const l_uint8 data,
l_int32  datalen,
const l_uint8 sequence,
l_int32  seqlen,
l_int32 poffset,
l_int32 pfound 
)

arrayFindSequence()

Input: data (byte array) datalen (length of data, in bytes) sequence (subarray of bytes to find in data) seqlen (length of sequence, in bytes) &offset (return> offset from beginning of data where the sequence begins) &found (<return> 1 if sequence is found; 0 otherwise) Return: 0 if OK, 1 on error

Notes: (1) The byte arrays 'data' and 'sequence' are not C strings, as they can contain null bytes. Therefore, for each we must give the length of the array. (2) This searches for the first occurrence in 'data' of the first 'seqlen' bytes of 'sequence'. The parameter 'seqlen' must not exceed the actual length of the 'sequence' byte array. (3) If the sequence is not found, the offset will be set to -1.

l_uint8* arrayRead ( const char *  fname,
l_int32 pnbytes 
)

arrayRead()

Input: filename &nbytes (<return> number of bytes read) Return: array, or null on error

l_uint8* arrayReadStream ( FILE *  fp,
l_int32 pnbytes 
)

arrayReadStream()

Input: stream &nbytes (<return> number of bytes read) Return: null-terminated array, or null on error (reading 0 bytes is not an error)

Notes: (1) N.B.: as a side effect, this always re-positions the stream ptr to the beginning of the file.

l_int32 arrayWrite ( const char *  filename,
const char *  operation,
void *  data,
l_int32  nbytes 
)

arrayWrite()

Input: filename (output) operation ("w" for write; "a" for append) data (binary data to be written) nbytes (size of data array) Return: 0 if OK; 1 on error

l_uint16 convertOnBigEnd16 ( l_uint16  shortin  ) 

l_uint32 convertOnBigEnd32 ( l_uint32  wordin  ) 

l_uint16 convertOnLittleEnd16 ( l_uint16  shortin  ) 

l_uint32 convertOnLittleEnd32 ( l_uint32  wordin  ) 

l_int32 extractNumberFromFilename ( const char *  fname,
l_int32  numpre,
l_int32  numpost 
)

extractNumberFromFilename()

Input: fname numpre (number of characters before the digits to be found) numpost (number of characters after the digits to be found) Return: num (number embedded in the filename); -1 on error or if not found

l_int32 fnbytesInFile ( FILE *  fp  ) 

fnbytesInFile()

Input: file stream Return: nbytes in file; 0 on error

FILE* fopenReadStream ( const char *  filename  ) 

fopenReadStream()

Input: filename Return: stream or null on error

char* genPathname ( const char *  dir,
const char *  fname 
)

genPathname()

Input: dir (directory name, with or without trailing '/') fname (file name within the directory) Return: full pathname, or null on error

char* genTempFilename ( const char *  dir,
const char *  extension 
)

genTempFilename()

Input: dir (directory name; use '.' for local dir; no trailing '/') extension (<optional> filename extention with '.'; can be null) Return: tempname (with pid embedded in file name), or null on error

Notes: (1) This function is useful when there can be more than one process writing and reading temporary files. It will not work properly when multiple threads from a single process call this function. Furthermore, as with any function that provides easily guessed temporary filenames, it is not designed to be safe from an attack where the intruder is logged onto the server.

void l_error ( const char *  msg,
const char *  procname 
)

l_error()

Input: msg (error message) procname

void l_errorFloat ( const char *  msg,
const char *  procname,
l_float32  fval 
)

l_errorFloat()

Input: msg (error message; must include 'f') procname fval (embedded in error message via f)

void l_errorInt ( const char *  msg,
const char *  procname,
l_int32  ival 
)

l_errorInt()

Input: msg (error message; must include 'd') procname ival (embedded in error message via d)

void l_errorString ( const char *  msg,
const char *  procname,
const char *  str 
)

l_errorString()

Input: msg (error message; must include 's') procname str (embedded in error message via s)

void l_info ( const char *  msg,
const char *  procname 
)

l_info()

Input: msg (info message) procname

void l_infoFloat ( const char *  msg,
const char *  procname,
l_float32  fval 
)

l_infoFloat()

Input: msg (info message; must include 'f') procname fval (embedded in info message via f)

void l_infoFloat2 ( const char *  msg,
const char *  procname,
l_float32  fval1,
l_float32  fval2 
)

l_infoFloat2()

Input: msg (info message; must include two 'f') procname fval1, fval2 (two args, embedded in info message via f)

void l_infoInt ( const char *  msg,
const char *  procname,
l_int32  ival 
)

l_infoInt()

Input: msg (info message; must include 'd') procname ival (embedded in info message via d)

void l_infoInt2 ( const char *  msg,
const char *  procname,
l_int32  ival1,
l_int32  ival2 
)

l_infoInt2()

Input: msg (info message; must include two 'd') procname ival1, ival2 (two args, embedded in info message via d)

void l_infoString ( const char *  msg,
const char *  procname,
const char *  str 
)

l_infoString()

Input: msg (info message; must include 's') procname str (embedded in warning message via s)

void l_warning ( const char *  msg,
const char *  procname 
)

l_warning()

Input: msg (warning message) procname

void l_warningFloat ( const char *  msg,
const char *  procname,
l_float32  fval 
)

l_warningFloat()

Input: msg (warning message; must include 'f') procname fval (embedded in warning message via f)

void l_warningInt ( const char *  msg,
const char *  procname,
l_int32  ival 
)

l_warningInt()

Input: msg (warning message; must include 'd') procname ival (embedded in warning message via d)

void l_warningString ( const char *  msg,
const char *  procname,
const char *  str 
)

l_warningString()

Input: msg (warning message; must include 's') procname str (embedded in warning message via s)

l_int32 nbytesInFile ( const char *  filename  ) 

nbytesInFile()

Input: filename Return: nbytes in file; 0 on error

void* reallocNew ( void **  pindata,
l_int32  oldsize,
l_int32  newsize 
)

reallocNew()

Input: &indata (<optional>; nulls indata) size of input data to be copied (bytes) size of data to be reallocated (bytes) Return: ptr to new data, or null on error

Action: !N.B. (3) and (4)! (1) Allocates memory, initialized to 0 (2) Copies as much of the input data as possible to the new block, truncating the copy if necessary (3) Frees the input data (4) Zeroes the input data ptr

Notes: (1) If newsize <=0, just frees input data and nulls ptr (2) If input ptr is null, just callocs new memory (3) This differs from realloc in that it always allocates new memory (if newsize > 0) and initializes it to 0, it requires the amount of old data to be copied, and it takes the address of the input ptr and nulls the handle.

l_float32 returnErrorFloat ( const char *  msg,
const char *  procname,
l_float32  fval 
)

returnErrorFloat()

Input: msg (error message) procname fval (return val) Return: fval

l_int32 returnErrorInt ( const char *  msg,
const char *  procname,
l_int32  ival 
)

returnErrorInt()

Input: msg (error message) procname ival (return val) Return: ival (typically 1)

void* returnErrorPtr ( const char *  msg,
const char *  procname,
void *  pval 
)

returnErrorPtr()

Input: msg (error message) procname pval (return val) Return: pval (typically null)

void returnErrorVoid ( const char *  msg,
const char *  procname 
)

returnErrorVoid()

Input: msg (error message) procname

l_int32 splitPathAtDirectory ( const char *  pathname,
char **  pdir,
char **  ptail 
)

splitPathAtDirectory()

Input: pathname (full path; can be a directory) &dir (<optional return>=""> root directory name of input path, including trailing '/') &tail (<optional return>=""> path tail, which is either the file name within the root directory or the last sub-directory in the path) Return: 0 if OK, 1 on error

Note: (1) if you only want the tail, input null for the root directory ptr. (2) if you only want the root directory name, input null for the tail ptr. (3) This function makes decisions based only on the lexical structure of the input. Examples: /usr/tmp/abc --> dir: /usr/tmp/ tail: abc /usr/tmp/ --> dir: /usr/tmp/ tail: [empty string] /usr/tmp --> dir: /usr/ tail: tmp

l_int32 splitPathAtExtension ( const char *  pathname,
char **  pbasename,
char **  pextension 
)

splitPathAtExtension()

Input: pathname (full path; can be a directory) &basename (<optional return>=""> pathname not including the last dot and characters after that) &extension (<optional return>=""> path extension, which is the last dot and the characters after it. If there is no extension, it returns the empty string) Return: 0 if OK, 1 on error

Notes: (1) If you only want the extension, input null for the basename ptr. (2) If you only want the basename without extension, input null for the extension ptr. (3) This function makes decisions based only on the lexical structure of the input. Examples: /usr/tmp/abc.jpg --> basename: /usr/tmp/abc ext: .jpg /usr/tmp/.jpg --> basename: /usr/tmp/ tail: .jpg /usr/tmp.jpg/ --> basename: /usr/tmp.jpg/ tail: [empty str] ./.jpg --> basename: ./ tail: .jpg

void startTimer ( void   ) 

l_float32 stopTimer ( void   ) 

l_int32 stringFindSubstr ( const char *  src,
const char *  sub,
l_int32 ploc 
)

stringFindSubstr()

Input: src (input string; can be of zero length) sub (substring to be searched for) &loc (<return optional>=""> location of substring in src) Return: 1 if found; 0 if not found or on error

Notes: (1) This is a wrapper around strstr(). (2) Both and must be defined, and must have length of at least 1. (3) If the substring is not found and loc is returned, it has the value -1.

char* stringJoin ( const char *  src1,
const char *  src2 
)

stringJoin()

Input: src1 string (<optional>) src2 string (<optional>) Return: concatenated string, or null on error

Notes: (1) This is the safe version of strcat; it makes a new string. (2) It is not an error if either or both of the strings are empty, or if either or both the pointers are null.

char* stringNew ( const char *  src  ) 

stringNew()

Input: src string Return: dest copy of src string, or null on error

char* stringRemoveChars ( const char *  src,
const char *  remchars 
)

stringRemoveChars()

Input: src (input string; can be of zero length) remchars (string of chars to be removed from src) Return: dest (string with specified chars removed), or null on error

l_int32 stringReplace ( char **  pdest,
const char *  src 
)

stringReplace()

Input: &dest string (<return> copy) src string Return: 0 if OK; 1 on error

Notes: (1) Frees any existing dest string (2) Puts a copy of src string in the dest (3) If either or both strings are null, does the reasonable thing.

char* stringReplaceEachSubstr ( const char *  src,
const char *  sub1,
const char *  sub2,
l_int32 pcount 
)

stringReplaceEachSubstr()

Input: src (input string; can be of zero length) sub1 (substring to be replaced) sub2 (substring to put in; can be "") &count (<optional return=""> the number of times that sub1 is found in src; 0 if not found) Return: dest (string with substring replaced), or null if the substring not found or on error.

Notes: (1) Replaces every instance. (2) To only remove each instance of sub1, use "" for sub2 (3) Returns NULL if sub1 and sub2 are the same.

char* stringReplaceSubstr ( const char *  src,
const char *  sub1,
const char *  sub2,
l_int32 pfound,
l_int32 ploc 
)

stringReplaceSubstr()

Input: src (input string; can be of zero length) sub1 (substring to be replaced) sub2 (substring to put in; can be "") &found (<return optional>=""> 1 if sub1 is found; 0 otherwise) &loc (<return optional>=""> location of ptr after replacement) Return: dest (string with substring replaced), or null if the substring not found or on error.

Notes: (1) Replaces the first instance. (2) To only remove sub1, use "" for sub2 (3) Returns a new string if sub1 and sub2 are the same. (4) The optional loc is input as the byte offset within the src from which the search starts, and after the search it is the char position in the string of the next character after the substituted string. (5) N.B. If ploc is not null, loc must always be initialized. To search the string from the beginning, set loc = 0.

char* stringReverse ( const char *  src  ) 

stringReverse()

Input: src (string) Return: dest (newly-allocated reversed string)

l_int32 stringSplitOnToken ( char *  cstr,
const char *  seps,
char **  phead,
char **  ptail 
)

stringSplitOnToken()

Input: cstr (input string to be split; not altered) seps (a string of character separators) &head (<return> ptr to copy of the input string, up to the first separator token encountered) &tail (<return> ptr to copy of the part of the input string starting with the first non-separator character that occurs after the first separator is found) Return: 0 if OK, 1 on error

Notes: (1) The input string is not altered; all split parts are new strings. (2) The split occurs around the first consecutive sequence of tokens encountered. (3) The head goes from the beginning of the string up to but not including the first token found. (4) The tail contains the second part of the string, starting with the first char in that part that is NOT a token. (5) If no separator token is found, 'head' contains a copy of the input string and 'tail' is null.

char* strtokSafe ( char *  cstr,
const char *  seps,
char **  psaveptr 
)

strtokSafe()

Input: cstr (input string to be sequentially parsed; use NULL after the first call) seps (a string of character separators) &saveptr (<return> ptr to the next char after the last encountered separator) Return: substr (a new string that is copied from the previous saveptr up to but not including the next separator character), or NULL if end of cstr.

Notes: (1) This is a thread-safe implementation of strtok. (2) It has the same interface as strtok_r. (3) It differs from strtok_r in usage in two respects: (a) the input string is not altered (b) each returned substring is newly allocated and must be freed after use. (4) Let me repeat that. This is "safe" because the input string is not altered and because each returned string is newly allocated on the heap. (5) It is here because, surprisingly, some C libraries don't include strtok_r. (6) Important usage points:

  • Input the string to be parsed on the first invocation.
  • Then input NULL after that; the value returned in saveptr is used in all subsequent calls. (7) This is only slightly slower than strtok_k.


Переменные

struct rusage rusage_after [static]

struct rusage rusage_before [static]

const char sepchar = '/' [static]


Документация по Leptonica. Последние изменения: Fri Aug 7 20:31:38 2009. Создано системой  doxygen 1.5.9