Annotation of parser3/src/include/pa_common.h, revision 1.83
1.15 paf 1: /** @file
1.16 paf 2: Parser: commonly used functions.
3:
1.73 paf 4: Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.74 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6: */
7:
8: #ifndef PA_COMMON_H
9: #define PA_COMMON_H
1.78 paf 10:
1.83 ! paf 11: static const char* IDENT_COMMON_H="$Date: 2002/09/18 12:40:38 $";
1.1 paf 12:
1.2 paf 13: #include "pa_pool.h"
1.33 paf 14: #include "pa_string.h"
1.1 paf 15:
1.14 paf 16: class Value;
1.64 paf 17:
18: // replace system s*nprintf with our versions
1.69 paf 19: #undef vsnprintf
1.63 paf 20: int __vsnprintf(char *, size_t, const char *, va_list);
1.64 paf 21: #define vsnprintf __vsnprintf
22: #undef snprintf
1.63 paf 23: int __snprintf(char *, size_t, const char *, ...);
1.64 paf 24: #define snprintf __snprintf
1.63 paf 25:
1.21 paf 26: #if _MSC_VER
1.56 paf 27: /*
28: inline int open( const char *filename, int oflag ) { return _open(filename, oflag); }
29: inline int close( int handle ) { return _close(handle); }
30: inline int read( int handle, void *buffer, unsigned int count ) { return _read(handle,buffer,count); }
31: inline int write( int handle, const void *buffer, unsigned int count ) { return _write(handle,buffer,count); }
32: inline int stat( const char *path, struct _stat *buffer ) { return _stat(path, buffer); }
33: inline long lseek( int handle, long offset, int origin ) { return _lseek(handle, offset, origin); }
34: */
1.17 paf 35:
36: //access
37: #define F_OK 0
38: #define X_OK 1
39: #define W_OK 2
40: #define R_OK 4
41:
1.20 paf 42: #ifndef strcasecmp
43: # define strcasecmp _stricmp
44: #endif
45: #ifndef strncasecmp
46: # define strncasecmp _strnicmp
47: #endif
48: #ifndef mkdir
49: # define mkdir(path, mode) _mkdir(path)
50: #endif
1.17 paf 51:
1.20 paf 52: #ifndef putenv
53: # define putenv _putenv
1.1 paf 54: #endif
1.2 paf 55:
1.17 paf 56: #endif
1.59 paf 57:
1.61 paf 58: #ifdef HAVE_TRUNC
59: # ifndef trunc
60: extern "C" double trunc(double);
61: # endif
62: #else
1.59 paf 63: inline double trunc(double param) { return param > 0? floor(param) : ceil(param); }
64: #endif
1.61 paf 65:
66: #ifdef HAVE_ROUND
67: # ifndef round
68: extern "C" double round(double);
69: # endif
70: #else
1.59 paf 71: inline double round(double param) { return floor(param+0.5); }
72: #endif
1.61 paf 73: #ifdef HAVE_SIGN
74: # ifndef sign
75: extern "C" double sign(double);
76: # endif
77: #else
1.59 paf 78: inline double sign(double param) { return param > 0 ? 1 : ( param < 0 ? -1 : 0 ); }
79: #endif
80:
1.72 paf 81: /// yields to OS for secs secs and usecs milliseconds
82: int pa_sleep(unsigned long secs, unsigned long usecs);
1.54 parser 83:
84: /** under WIN32 "t" mode fixes DOS chars OK,
85: can't say that about other systems/ line break styles
86: */
1.57 paf 87: void fix_line_breaks(
1.62 paf 88: char *buf,
89: size_t& size ///< may change! used to speedup next actions
1.57 paf 90: );
1.17 paf 91:
1.83 ! paf 92: typedef void (*File_read_action)(Pool& pool,
! 93: int f,
! 94: const String& file_spec, const char *fname, bool as_text,
! 95: void *context);
! 96:
! 97: /**
! 98: shared-lock specified file,
! 99: do actions under lock.
! 100: if fail_on_read_problem is true[default] throws an exception
! 101:
! 102: @returns true if read OK
! 103: */
! 104: bool file_read_action_under_lock(Pool& pool, const String& file_spec,
! 105: const char *action_name, File_read_action action, void *context,
! 106: bool as_text=false,
! 107: bool fail_on_read_problem=true);
1.15 paf 108: /**
1.19 paf 109: read specified text file using pool,
1.15 paf 110: if fail_on_read_problem is true[default] throws an exception
1.83 ! paf 111:
! 112: @returns true if read OK
1.15 paf 113: */
1.19 paf 114: char *file_read_text(Pool& pool,
1.25 paf 115: const String& file_spec,
1.19 paf 116: bool fail_on_read_problem=true);
1.17 paf 117:
118: /**
1.29 paf 119: read specified file using pool,
120: if fail_on_read_problem is true[default] throws an exception
1.83 ! paf 121:
! 122: @returns true if read OK
1.29 paf 123: */
124: bool file_read(Pool& pool, const String& file_spec,
125: void*& data, size_t& size,
126: bool as_text,
1.83 ! paf 127: bool fail_on_read_problem=true);
! 128:
! 129: typedef void (*File_write_action)(int f, void *context);
1.29 paf 130:
131: /**
1.71 paf 132: lock specified file exclusively,
133: do actions under lock.
134: throws an exception in case of problems
1.72 paf 135:
136: if block=false does non-blocking lock
137: @returns true if locked OK, or false if non-blocking locking failed
1.71 paf 138: */
1.72 paf 139: bool file_write_action_under_lock(
1.71 paf 140: const String& file_spec,
1.83 ! paf 141: const char *action_name, File_write_action action, void *context,
1.71 paf 142: bool as_text=false,
1.72 paf 143: bool do_append=false,
1.77 paf 144: bool do_block=true,
145: bool fail_on_lock_problem=true);
1.71 paf 146:
147: /**
148: write data to specified file,
1.17 paf 149: throws an exception in case of problems
150: */
1.70 paf 151: void file_write(
1.25 paf 152: const String& file_spec,
1.29 paf 153: const void *data, size_t size,
1.58 paf 154: bool as_text,
1.67 paf 155: bool do_append=false);
1.26 paf 156:
157: /**
158: delete specified file
159: throws an exception in case of problems
160: */
1.70 paf 161: bool file_delete(const String& file_spec, bool fail_on_read_problem=true);
1.47 parser 162: /**
163: move specified file
164: throws an exception in case of problems
165: */
1.70 paf 166: void file_move(const String& old_spec, const String& new_spec);
1.27 paf 167:
1.81 paf 168: bool entry_exists(const char *fname, struct stat *afinfo=0);
1.82 paf 169: bool entry_exists(const String& file_spec);
1.27 paf 170: bool file_readable(const String& file_spec);
1.41 paf 171: bool dir_readable(const String& file_spec);
1.49 parser 172: String *file_readable(const String& path, const String& name);
1.36 paf 173: bool file_executable(const String& file_spec);
1.37 paf 174:
1.48 parser 175: bool file_stat(const String& file_spec,
1.46 parser 176: size_t& rsize,
177: time_t& ratime,
178: time_t& rmtime,
1.48 parser 179: time_t& rctime,
180: bool fail_on_read_problem=true);
1.15 paf 181:
182: /**
1.18 paf 183: scans for @a delim[default \n] in @a *row_ref,
184: @return piece of line before it or end of string, if no @a delim found
185: assigns @a *row_ref to point right after delimiter if there were one
186: or to zero if no @a delim were found.
1.15 paf 187: */
1.7 paf 188: char *getrow(char **row_ref,char delim='\n');
1.76 paf 189: char *lsplit(char *string, char delim);
1.7 paf 190: char *lsplit(char **string_ref,char delim);
1.8 paf 191: char *rsplit(char *string, char delim);
1.9 paf 192: char *format(Pool& pool, double value, char *fmt);
1.10 paf 193:
194: #ifndef max
1.75 paf 195: inline int max(int a, int b) { return a>b?a:b; }
196: inline int min(int a, int b){ return a<b?a:b; }
197: inline size_t max(size_t a, size_t b) { return a>b?a:b; }
198: inline size_t min(size_t a, size_t b){ return a<b?a:b; }
1.10 paf 199: #endif
200:
1.30 paf 201: size_t stdout_write(const void *buf, size_t size);
1.14 paf 202:
1.55 parser 203: char *unescape_chars(Pool& pool, const char *cp, int len);
1.23 paf 204:
205: #ifdef WIN32
206: void back_slashes_to_slashes(char *s);
1.35 paf 207: //void slashes_to_back_slashes(char *s);
1.23 paf 208: #endif
209:
1.28 paf 210: #ifndef _qsort
211: # define _qsort(names,cnt,sizeof_names,func_addr) \
212: qsort(names,cnt,sizeof_names,func_addr)
213: #endif
1.34 paf 214:
1.40 paf 215: bool StrEqNc(const char *s1, const char *s2, bool strict=true);
1.45 parser 216:
217: #define SECS_PER_DAY (60*60*24)
218: int getMonthDays(int year, int month);
1.52 parser 219:
220: void remove_crlf(char *start, char *end);
1.1 paf 221:
222: #endif
E-mail: