Annotation of parser3/src/main/pa_common.C, revision 1.113
1.15 paf 1: /** @file
1.16 paf 2: Parser: commonly functions.
3:
1.102 paf 4: Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.101 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.111 paf 6: */
1.16 paf 7:
1.113 ! paf 8: static const char* IDENT_COMMON_C="$Date: 2002/08/01 11:41:18 $";
1.1 paf 9:
10: #include "pa_common.h"
1.2 paf 11: #include "pa_types.h"
1.4 paf 12: #include "pa_exception.h"
1.14 paf 13: #include "pa_pool.h"
14: #include "pa_globals.h"
15: #include "pa_value.h"
16: #include "pa_hash.h"
17: #include "pa_string.h"
1.1 paf 18:
1.98 paf 19: #ifdef WIN32
20: # include <windows.h>
21: #endif
22:
1.93 paf 23: // some maybe-undefined constants
24:
1.82 paf 25: #ifndef _O_TEXT
26: # define _O_TEXT 0
27: #endif
28: #ifndef _O_BINARY
29: # define _O_BINARY 0
1.47 paf 30: #endif
1.80 paf 31: #ifndef O_TRUNC
32: # define O_TRUNC 0
33: #endif
34:
1.93 paf 35: // locking constants
36:
1.99 paf 37: #ifdef HAVE_FLOCK
38:
39: static int lock_shared_blocking(int fd) { return flock(fd, LOCK_SH); }
40: static int lock_exclusive_blocking(int fd) { return flock(fd, LOCK_EX); }
41: static int lock_exclusive_nonblocking(int fd) { return flock(fd, LOCK_EX || LOCK_NB); }
42: static int unlock(int fd) { return flock(fd, LOCK_UN); }
43:
1.98 paf 44: #else
1.99 paf 45: #ifdef HAVE__LOCKING
1.98 paf 46:
1.99 paf 47: #define FLOCK(operation) lseek(fd, 0, SEEK_SET); return _locking(fd, operation, 1)
48: static int lock_shared_blocking(int fd) { FLOCK(_LK_LOCK); }
49: static int lock_exclusive_blocking(int fd) { FLOCK(_LK_LOCK); }
50: static int lock_exclusive_nonblocking(int fd) { FLOCK(_LK_NBLCK); }
51: static int unlock(int fd) { FLOCK(_LK_UNLCK); }
1.93 paf 52:
1.99 paf 53: #else
54: #ifdef HAVE_FCNTL
1.93 paf 55:
1.99 paf 56: #define FLOCK(cmd, arg) struct flock ls={arg, SEEK_SET}; return fcntl(fd, cmd, &ls)
57: static int lock_shared_blocking(int fd) { FLOCK(F_SETLKW, F_RDLCK); }
58: static int lock_exclusive_blocking(int fd) { FLOCK(F_SETLKW, F_WRLCK); }
59: static int lock_exclusive_nonblocking(int fd) { FLOCK(F_SETLK, F_RDLCK); }
60: static int unlock(int fd) { FLOCK(F_SETLK, F_UNLCK); }
1.93 paf 61:
62: #else
63: #ifdef HAVE_LOCKF
1.99 paf 64:
65: #define FLOCK(fd, operation) lseek(fd, 0, SEEK_SET); return lockf(fd, operation, 1)
66: static int lock_shared_blocking(int fd) { FLOCK(F_LOCK); } // on intel solaris man doesn't have doc on shared blocking
67: static int lock_exclusive_blocking(int fd) { FLOCK(F_LOCK); }
68: static int lock_exclusive_nonblocking(int fd) { FLOCK(F_TLOCK); }
69: static int unlock(int fd) { FLOCK(F_TLOCK); }
70:
1.93 paf 71: #else
1.99 paf 72:
73: #error unable to find file locking func
74:
75: #endif
1.93 paf 76: #endif
77: #endif
78: #endif
79:
1.86 paf 80: static char *strnchr(char *buf, size_t size, char c) {
1.103 paf 81: // sanity check
82: if(!buf)
83: return 0;
84:
1.86 paf 85: for(; size-->0; buf++) {
86: if(*buf==c)
87: return buf;
88: }
89:
90: return 0;
91: }
92:
93: void fix_line_breaks(char *buf, size_t& size) {
1.87 paf 94: //_asm int 3;
1.86 paf 95: const char * const eob=buf+size;
1.87 paf 96: char *dest=buf;
1.72 parser 97: // fix DOS: \r\n -> \n
98: // fix Macintosh: \r -> \n
1.87 paf 99: char *bol=buf;
1.86 paf 100: while(char *eol=strnchr(bol, eob -bol, '\r')) {
1.72 parser 101: size_t len=eol-bol;
102: if(dest!=bol)
103: memcpy(dest, bol, len);
104: dest+=len;
105: *dest++='\n';
106:
1.87 paf 107: if(&eol[1]<eob && eol[1]=='\n') { // \r,\n = DOS
1.72 parser 108: bol=eol+2;
109: size--;
110: } else // \r,not \n = Macintosh
111: bol=eol+1;
112: }
113: // last piece without \r, including terminating 0
114: if(dest!=bol)
1.87 paf 115: memcpy(dest, bol, eob-bol);
1.72 parser 116: }
1.18 paf 117:
1.28 paf 118: char *file_read_text(Pool& pool, const String& file_spec, bool fail_on_read_problem) {
1.72 parser 119: void *result; size_t size;
120: return file_read(pool, file_spec, result, size, true, fail_on_read_problem)?(char *)result:0;
1.34 paf 121: }
122: bool file_read(Pool& pool, const String& file_spec,
1.87 paf 123: void*& data, size_t& data_size, bool as_text,
1.44 paf 124: bool fail_on_read_problem,
125: size_t offset, size_t limit) {
1.64 parser 126: const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.62 parser 127: //printf("file_read(%s)\n", fname);
1.33 paf 128: int f;
1.2 paf 129: struct stat finfo;
1.33 paf 130:
131: // first open, next stat:
1.45 paf 132: // directory update of NTFS hard links performed on open.
1.33 paf 133: // ex:
134: // a.html:^test[] and b.html hardlink to a.html
135: // user inserts ! before ^test in a.html
136: // directory entry of b.html in NTFS not updated at once,
1.35 paf 137: // they delay update till open, so we would receive "!^test[" string
138: // if would do stat, next open.
1.98 paf 139: if((f=open(fname, O_RDONLY|(as_text?_O_TEXT:_O_BINARY)))>=0) {
1.110 paf 140: if(lock_shared_blocking(f)!=0) {
141: Exception e("file.lock",
142: &file_spec,
143: "shared lock failed: %s (%d), actual filename '%s'",
144: strerror(errno), errno, fname);
145: unlock(f);
146: close(f);
147: if(fail_on_read_problem)
148: throw e;
149: return false;
150: }
1.98 paf 151: if(stat(fname, &finfo)!=0) {
1.106 paf 152: Exception e("file.missing",
1.98 paf 153: &file_spec,
154: "stat failed: %s (%d), actual filename '%s'",
155: strerror(errno), errno, fname);
1.99 paf 156: unlock(f);
1.98 paf 157: close(f);
158: if(fail_on_read_problem)
159: throw e;
160: return false;
161: }
1.105 paf 162: #ifdef NO_FOREIGN_GROUP_FILES
163: if(finfo.st_gid/*foreign?*/!=getegid()) {
1.107 paf 164: Exception e("parser.runtime",
1.105 paf 165: &file_spec,
166: "parser reading files of foreign group disabled [recompile parser without --disable-foreign-group-files configure option], actual filename '%s'",
167: fname);
168: unlock(f);
169: close(f);
170: if(fail_on_read_problem)
171: throw e;
172: return false;
173: }
174: #endif
1.98 paf 175: size_t max_size=limit?min(offset+limit, (size_t)finfo.st_size)-offset:finfo.st_size;
1.47 paf 176: if(!max_size) { // eof
1.83 paf 177: if(as_text) {
178: data=pool.malloc(1);
179: *(char*)data=0;
180: } else
181: data=0;
1.98 paf 182: data_size=0;
1.59 parser 183: } else {
1.78 paf 184: data=pool.malloc(max_size+(as_text?1:0), 3);
1.59 parser 185: if(offset)
186: lseek(f, offset, SEEK_SET);
1.98 paf 187: data_size=read(f, data, max_size);
1.47 paf 188: }
1.99 paf 189: unlock(f);
1.2 paf 190: close(f);
1.59 parser 191: if(!max_size) // eof
192: return true;
1.32 paf 193:
1.98 paf 194: if(int(data_size)<0 || data_size>max_size)
1.106 paf 195: throw Exception(0,
1.33 paf 196: &file_spec,
1.44 paf 197: "read failed: actually read %d bytes count not in [0..%lu] valid range",
1.98 paf 198: data_size, (unsigned long)max_size); //never
1.87 paf 199:
200: if(as_text) {
201: fix_line_breaks((char *)data, data_size);
202: // note: after fixing
203: ((char*&)data)[data_size]=0;
204: }
1.72 parser 205: return true;
1.2 paf 206: }
1.4 paf 207: if(fail_on_read_problem)
1.106 paf 208: throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0,
1.33 paf 209: &file_spec,
1.54 parser 210: "read failed: %s (%d), actual filename '%s'",
211: strerror(errno), errno, fname);
1.34 paf 212: return false;
1.8 paf 213: }
214:
1.63 parser 215: static void create_dir_for_file(const String& file_spec) {
216: size_t pos_after=1;
217: int pos_before;
218: while((pos_before=file_spec.pos("/", 1, pos_after))>=0) {
1.64 parser 219: mkdir(file_spec.mid(0, pos_before).cstr(String::UL_FILE_SPEC), 0775);
1.63 parser 220: pos_after=pos_before+1;
221: }
222: }
223:
1.98 paf 224: bool file_write_action_under_lock(
1.28 paf 225: const String& file_spec,
1.96 paf 226: const char *action_name, void (*action)(int, void *), void *context,
1.80 paf 227: bool as_text,
1.98 paf 228: bool do_append,
1.110 paf 229: bool do_block,
230: bool fail_on_lock_problem) {
1.64 parser 231: const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.28 paf 232: int f;
1.80 paf 233: if(access(fname, W_OK)!=0) // no
1.63 parser 234: create_dir_for_file(file_spec);
1.50 paf 235:
1.80 paf 236: if((f=open(fname,
237: O_CREAT|O_RDWR
238: |(as_text?_O_TEXT:_O_BINARY)
1.104 paf 239: |(do_append?O_APPEND:O_TRUNC), 0664))>=0) {
1.99 paf 240: if((do_block?lock_exclusive_blocking(f):lock_exclusive_nonblocking(f))!=0) {
1.110 paf 241: Exception e("file.lock",
242: &file_spec,
243: "shared lock failed: %s (%d), actual filename '%s'",
244: strerror(errno), errno, fname);
1.98 paf 245: close(f);
1.110 paf 246: if(fail_on_lock_problem)
247: throw e;
1.98 paf 248: return false;
249: }
1.96 paf 250:
251: try {
252: action(f, context);
253: } catch(...) {
1.98 paf 254: #if O_TRUNC==0
1.104 paf 255: if(!do_append)
256: ftruncate(f, tell(f));
1.98 paf 257: #endif
1.99 paf 258: unlock(f);
1.96 paf 259: close(f);
260: /*re*/throw;
261: }
1.80 paf 262:
1.98 paf 263: #if O_TRUNC==0
1.104 paf 264: if(!do_append)
265: ftruncate(f, tell(f));
1.98 paf 266: #endif
1.99 paf 267: unlock(f);
1.80 paf 268: close(f);
1.98 paf 269: return true;
1.80 paf 270: } else
1.106 paf 271: throw Exception(errno==EACCES?"file.access":0,
1.80 paf 272: &file_spec,
1.96 paf 273: "%s failed: %s (%d), actual filename '%s'",
274: action_name, strerror(errno), errno, fname);
275: // here should be nothing, see rethrow above
276: }
277:
278: #ifndef DOXYGEN
279: struct File_write_action_info {
280: const void *data; size_t size;
281: };
282: #endif
283: static void file_write_action(int f, void *context) {
284: File_write_action_info& info=*static_cast<File_write_action_info *>(context);
1.113 ! paf 285: if(info.size) {
! 286: size_t written=write(f, info.data, info.size);
! 287: if(written!=info.size)
! 288: throw Exception(0,
! 289: 0,
! 290: "write failed: %s (%d), number of bytes written not equal to requested(%u!=%u), %s",
! 291: strerror(errno), errno,
! 292: written, info.size);
! 293: }
1.96 paf 294: }
295: void file_write(
296: const String& file_spec,
297: const void *data, size_t size,
298: bool as_text,
299: bool do_append) {
300: File_write_action_info info={data, size};
1.98 paf 301: file_write_action_under_lock(
1.96 paf 302: file_spec,
303: "write", file_write_action, &info,
304: as_text,
305: do_append);
1.30 paf 306: }
307:
1.63 parser 308: // throws nothing! [this is required in file_move & file_delete]
1.50 paf 309: static void rmdir(const String& file_spec, size_t pos_after) {
310: int pos_before;
311: if((pos_before=file_spec.pos("/", 1, pos_after))>=0)
312: rmdir(file_spec, pos_before+1);
313:
1.64 parser 314: rmdir(file_spec.mid(0, pos_after-1/* / */).cstr(String::UL_FILE_SPEC));
1.50 paf 315: }
1.95 paf 316: bool file_delete(const String& file_spec, bool fail_on_read_problem) {
1.64 parser 317: const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.54 parser 318: if(unlink(fname)!=0)
1.93 paf 319: if(fail_on_read_problem)
1.106 paf 320: throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0,
1.93 paf 321: &file_spec,
322: "unlink failed: %s (%d), actual filename '%s'",
323: strerror(errno), errno, fname);
324: else
325: return false;
1.50 paf 326:
327: rmdir(file_spec, 1);
1.93 paf 328: return true;
1.60 parser 329: }
1.95 paf 330: void file_move(const String& old_spec, const String& new_spec) {
1.64 parser 331: const char *old_spec_cstr=old_spec.cstr(String::UL_FILE_SPEC);
332: const char *new_spec_cstr=new_spec.cstr(String::UL_FILE_SPEC);
1.63 parser 333:
334: create_dir_for_file(new_spec);
335:
1.60 parser 336: if(rename(old_spec_cstr, new_spec_cstr)!=0)
1.106 paf 337: throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0,
1.60 parser 338: &old_spec,
339: "rename failed: %s (%d), actual filename '%s' to '%s'",
340: strerror(errno), errno, old_spec_cstr, new_spec_cstr);
1.63 parser 341:
342: rmdir(old_spec, 1);
1.31 paf 343: }
344:
1.51 paf 345:
346: static bool entry_readable(const String& file_spec, bool need_dir) {
1.64 parser 347: const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.51 paf 348: struct stat finfo;
349: if(access(fname, R_OK)==0 && stat(fname, &finfo)==0) {
1.109 paf 350: bool is_dir=(finfo.st_mode&S_IFDIR) != 0;
1.51 paf 351: return is_dir==need_dir;
352: }
353: return false;
354: }
1.31 paf 355: bool file_readable(const String& file_spec) {
1.51 paf 356: return entry_readable(file_spec, false);
357: }
358: bool dir_readable(const String& file_spec) {
359: return entry_readable(file_spec, true);
1.65 parser 360: }
361: String *file_readable(const String& path, const String& name) {
362: String *result=new(path.pool()) String(path);
363: *result << "/";
364: *result << name;
365: return file_readable(*result)?result:0;
1.43 paf 366: }
367: bool file_executable(const String& file_spec) {
1.64 parser 368: return access(file_spec.cstr(String::UL_FILE_SPEC), X_OK)==0;
1.44 paf 369: }
370:
1.64 parser 371: bool file_stat(const String& file_spec,
1.58 parser 372: size_t& rsize,
373: time_t& ratime,
374: time_t& rmtime,
1.64 parser 375: time_t& rctime,
376: bool fail_on_read_problem) {
1.44 paf 377: Pool& pool=file_spec.pool();
1.64 parser 378: const char *fname=file_spec.cstr(String::UL_FILE_SPEC);
1.44 paf 379: struct stat finfo;
380: if(stat(fname, &finfo)!=0)
1.64 parser 381: if(fail_on_read_problem)
1.106 paf 382: throw Exception("file.missing",
1.67 parser 383: &file_spec,
384: "getting file size failed: %s (%d), real filename '%s'",
385: strerror(errno), errno, fname);
1.64 parser 386: else
387: return false;
1.58 parser 388: rsize=finfo.st_size;
389: ratime=finfo.st_atime;
390: rmtime=finfo.st_mtime;
391: rctime=finfo.st_ctime;
1.64 parser 392: return true;
1.18 paf 393: }
394:
1.8 paf 395: char *getrow(char **row_ref, char delim) {
396: char *result=*row_ref;
397: if(result) {
398: *row_ref=strchr(result, delim);
399: if(*row_ref)
400: *((*row_ref)++)=0;
401: else if(!*result)
402: return 0;
403: }
404: return result;
405: }
406:
1.23 paf 407: char *lsplit(char *string, char delim) {
408: if(string) {
409: char *v=strchr(string, delim);
1.8 paf 410: if(v) {
411: *v=0;
412: return v+1;
413: }
414: }
415: return 0;
416: }
417:
418: char *lsplit(char **string_ref, char delim) {
419: char *result=*string_ref;
420: char *next=lsplit(*string_ref, delim);
421: *string_ref=next;
422: return result;
1.9 paf 423: }
424:
425: char *rsplit(char *string, char delim) {
1.18 paf 426: if(string) {
1.9 paf 427: char *v=strrchr(string, delim);
1.18 paf 428: if(v) {
1.9 paf 429: *v=0;
430: return v+1;
431: }
432: }
433: return NULL;
1.10 paf 434: }
435:
1.37 paf 436: /// @todo less stupid type detection
1.10 paf 437: char *format(Pool& pool, double value, char *fmt) {
1.108 paf 438: char local_buf[MAX_NUMBER];
439: size_t size;
440:
1.10 paf 441: if(fmt)
442: if(strpbrk(fmt, "diouxX"))
443: if(strpbrk(fmt, "ouxX"))
1.108 paf 444: size=snprintf(local_buf, sizeof(local_buf), fmt, (uint)value);
1.10 paf 445: else
1.108 paf 446: size=snprintf(local_buf, sizeof(local_buf), fmt, (int)value);
1.10 paf 447: else
1.108 paf 448: size=snprintf(local_buf, sizeof(local_buf), fmt, value);
1.10 paf 449: else
1.108 paf 450: size=snprintf(local_buf, sizeof(local_buf), "%d", (int)value);
1.10 paf 451:
1.108 paf 452: char *pool_buf=(char *)pool.malloc(size+1, 4);
453: memcpy(pool_buf, local_buf, size+1);
454: return pool_buf;
1.12 paf 455: }
456:
1.36 paf 457: size_t stdout_write(const void *buf, size_t size) {
1.12 paf 458: #ifdef WIN32
459: do{
460: int chunk_written=fwrite(buf, 1, min(8*0x400, size), stdout);
461: if(chunk_written<=0)
462: break;
463: size-=chunk_written;
1.36 paf 464: buf=((const char*)buf)+chunk_written;
1.12 paf 465: } while(size>0);
466:
467: return size;
468: #else
1.13 paf 469: return fwrite(buf, 1, size, stdout);
1.12 paf 470: #endif
1.2 paf 471: }
1.14 paf 472:
1.77 parser 473: char *unescape_chars(Pool& pool, const char *cp, int len) {
1.78 paf 474: char *s=(char *)pool.malloc(len + 1, 5);
1.14 paf 475: enum EscapeState {
1.33 paf 476: EscapeRest,
477: EscapeFirst,
1.14 paf 478: EscapeSecond
479: } escapeState=EscapeRest;
480: int escapedValue=0;
481: int srcPos=0;
482: int dstPos=0;
483: while(srcPos < len) {
484: int ch=cp[srcPos];
485: switch(escapeState) {
486: case EscapeRest:
487: if(ch=='%') {
488: escapeState=EscapeFirst;
489: } else if(ch=='+') {
490: s[dstPos++]=' ';
491: } else {
492: s[dstPos++]=ch;
493: }
494: break;
495: case EscapeFirst:
496: escapedValue=hex_value[ch] << 4;
497: escapeState=EscapeSecond;
498: break;
499: case EscapeSecond:
500: escapedValue +=hex_value[ch];
501: s[dstPos++]=escapedValue;
502: escapeState=EscapeRest;
503: break;
504: }
505: srcPos++;
506: }
507: s[dstPos]=0;
508: return s;
509: }
510:
1.52 paf 511: /// used by attributed_meaning_to_string / append_attribute_subattribute
1.40 paf 512: struct Attributed_meaning_info {
1.52 paf 513: String *header; // header line being constructed
514: String::Untaint_lang lang; // language in which to append to that line
1.40 paf 515: };
1.17 paf 516: static void append_attribute_subattribute(const Hash::Key& akey, Hash::Val *avalue,
1.14 paf 517: void *info) {
518: if(akey==VALUE_NAME)
519: return;
520:
1.40 paf 521: Attributed_meaning_info& ami=*static_cast<Attributed_meaning_info *>(info);
522:
1.14 paf 523: // ...; charset=windows1251
1.48 paf 524: *ami.header << "; ";
1.70 parser 525: ami.header->append(akey, ami.lang);
1.48 paf 526: *ami.header << "=";
1.70 parser 527: ami.header->append(static_cast<Value *>(avalue)->as_string(), ami.lang);
1.14 paf 528: }
1.49 paf 529: const String& attributed_meaning_to_string(Value& meaning,
530: String::Untaint_lang lang) {
1.20 paf 531: String &result=*new(meaning.pool()) String(meaning.pool());
1.75 parser 532: if(Hash *hash=meaning.get_hash(0)) {
1.14 paf 533: // $value(value) $subattribute(subattribute value)
534: if(Value *value=static_cast<Value *>(hash->get(*value_name)))
1.40 paf 535: result.append(value->as_string(), lang, true);
1.14 paf 536:
1.40 paf 537: Attributed_meaning_info attributed_meaning_info={
1.52 paf 538: &result,
539: lang
1.40 paf 540: };
541: hash->for_each(append_attribute_subattribute, &attributed_meaning_info);
1.14 paf 542: } else // result value
1.40 paf 543: result.append(meaning.as_string(), lang, true);
1.14 paf 544:
545: return result;
1.24 paf 546: }
547:
548: #ifdef WIN32
549: void back_slashes_to_slashes(char *s) {
550: if(s)
551: for(; *s; s++)
552: if(*s=='\\')
553: *s='/';
554: }
1.42 paf 555: /*
556: void slashes_to_back_slashes(char *s) {
557: if(s)
558: for(; *s; s++)
559: if(*s=='/')
560: *s='\\';
561: }
562: */
1.24 paf 563: #endif
1.41 paf 564:
565: bool StrEqNc(const char *s1, const char *s2, bool strict) {
566: while(true) {
567: if(!(*s1)) {
568: if(!(*s2))
569: return true;
570: else
571: return !strict;
572: } else if(!(*s2))
573: return !strict;
574: if(isalpha(*s1)) {
575: if(tolower(*s1) !=tolower(*s2))
576: return false;
577: } else if((*s1) !=(*s2))
578: return false;
579: s1++;
580: s2++;
581: }
1.57 parser 582: }
583:
1.84 paf 584: static bool isLeap(int year) {
1.57 parser 585: return !(
586: (year % 4) || ((year % 400) && !(year % 100))
587: );
588: }
589:
590: int getMonthDays(int year, int month) {
591: int monthDays[]={
592: 31,
593: isLeap(year) ? 29 : 28,
594: 31,
595: 30,
596: 31,
597: 30,
598: 31,
599: 31,
600: 30,
601: 31,
602: 30,
603: 31
604: };
605: return monthDays[month];
1.41 paf 606: }
1.69 parser 607:
608: void remove_crlf(char *start, char *end) {
609: for(char *p=start; p<end; p++)
610: switch(*p) {
611: case '\n': *p='|'; break;
612: case '\r': *p=' '; break;
613: }
1.91 paf 614: }
615:
616:
617: /// must be last in this file
618: #undef vsnprintf
619: int __vsnprintf(char *b, size_t s, const char *f, va_list l) {
620: if(!s)
621: return 0;
622:
623: int r;
624: // note: on win32& maybe somewhere else
625: // vsnprintf do not writes terminating 0 in 'buffer full' case, reducing
626: --s;
627: #if _MSC_VER
628: /*
629: win32:
630: mk:@MSITStore:C:\Program%20Files\Microsoft%20Visual%20Studio\MSDN\2001APR\1033\vccore.chm::/html/_crt__vsnprintf.2c_._vsnwprintf.htm
631:
632: if the number of bytes to write exceeds buffer, then count bytes are written and –1 is returned
633: */
634: r=_vsnprintf(b, s, f, l);
635: if(r<0)
636: r=s;
637: #else
638: r=vsnprintf(b, s, f, l);
639: /*
640: solaris:
641: man vsnprintf
642:
643: The snprintf() function returns the number of characters
644: formatted, that is, the number of characters that would have
645: been written to the buffer if it were large enough. If the
646: value of n is 0 on a call to snprintf(), an unspecified
647: value less than 1 is returned.
648: */
649:
650: if(r<0)
651: r=0;
652: else if(r>s)
653: r=s;
654: #endif
655: b[r]=0;
656: return r;
657: }
658:
659: int __snprintf(char *b, size_t s, const char *f, ...) {
660: va_list l;
661: va_start(l, f);
662: int r=__vsnprintf(b, s, f, l);
663: va_end(l);
664: return r;
1.98 paf 665: }
666:
667: int pa_sleep(unsigned long secs, unsigned long usecs) {
668: for (; usecs >= 1000000; ++secs, usecs -= 1000000);
669:
670: #ifdef WIN32
671: Sleep(secs * 1000 + usecs / 1000);
672: return 0;
673: #else
674: struct timeval t;
675: t.tv_sec = secs;
676: t.tv_usec = usecs;
677: return (select(0, NULL, NULL, NULL, &t) == -1 ? errno : 0);
678: #endif
1.74 parser 679: }
E-mail: