PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/libbeye/libbeye.c

#
C | 742 lines | 578 code | 51 blank | 113 comment | 122 complexity | 8bd7ecf885df453240cffc43c6d25d51 MD5 | raw file
Possible License(s): AGPL-1.0
  1. /**
  2. * @namespace libbeye
  3. * @file libbeye/libbeye.c
  4. * @brief This file contains implementation of extension of C library.
  5. * @version -
  6. * @remark this source file is part of Binary EYE project (BEYE).
  7. * The Binary EYE (BEYE) is copyright (C) 1995 Nickols_K.
  8. * All rights reserved. This software is redistributable under the
  9. * licence given in the file "Licence.en" ("Licence.ru" in russian
  10. * translation) distributed in the BEYE archive.
  11. * @note Requires POSIX compatible development system
  12. *
  13. * @author Nickols_K
  14. * @since 1995
  15. * @note Development, fixes and improvements
  16. * @todo Increase number of functions
  17. **/
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <stdarg.h>
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <limits.h>
  24. #include "libbeye/sysdep/__config.h"
  25. #if __WORDSIZE == 16
  26. #include <mem.h>
  27. #endif
  28. #include "libbeye/pmalloc.h"
  29. bool __FASTCALL__ isseparate(int ch) { return (isspace(ch) || ispunct(ch)); }
  30. void __FASTCALL__ __nls_PrepareOEMForTVio(tvioBuff *it,unsigned size)
  31. {
  32. unsigned i;
  33. unsigned char ch;
  34. for(i = 0;i < size;i++)
  35. {
  36. ch = it->chars[i];
  37. it->oem_pg[i] = NLS_IS_OEMPG(ch) ? ch : 0;
  38. }
  39. __nls_OemToOsdep(it->chars,size);
  40. }
  41. void __FASTCALL__ memupr(void *ptr,unsigned n)
  42. {
  43. unsigned i;
  44. for(i = 0;i < n;i++)
  45. ((char *)ptr)[i] = toupper(((char *)ptr)[i]);
  46. }
  47. void __FASTCALL__ memlwr(void *ptr,unsigned n)
  48. {
  49. unsigned i;
  50. for(i = 0;i < n;i++)
  51. ((char *)ptr)[i] = tolower(((char *)ptr)[i]);
  52. }
  53. int __FASTCALL__ szTrimTrailingSpace(char *str)
  54. {
  55. unsigned len;
  56. int ret;
  57. len = strlen(str);
  58. ret = 0;
  59. while(len)
  60. {
  61. unsigned char ch;
  62. ch = str[len-1];
  63. if(isspace(ch) && ch < 0x80) { str[--len] = '\0'; ret++; }
  64. else break;
  65. }
  66. return ret;
  67. }
  68. int __FASTCALL__ szTrimLeadingSpace(char *str)
  69. {
  70. unsigned i,freq,len;
  71. len = strlen(str);
  72. for(i = freq = 0;i < len;i++)
  73. {
  74. unsigned char ch;
  75. ch = str[i];
  76. if(isspace(ch) && ch < 0x80) freq++;
  77. else break;
  78. }
  79. if(freq)
  80. {
  81. len -= freq;
  82. memmove(str,&str[freq],len+1);
  83. }
  84. return freq;
  85. }
  86. #define TEXT_TAB 8
  87. void __FASTCALL__ szSpace2Tab(char *dest,const char * src)
  88. {
  89. unsigned int i,len,limit,dest_idx;
  90. int j;
  91. unsigned char buff[8],nspc;
  92. len = strlen(src);
  93. i = 0;
  94. dest_idx = 0;
  95. while(1)
  96. {
  97. if(i + TEXT_TAB < len)
  98. {
  99. memcpy(buff,&src[i],8);
  100. i+=8;
  101. /* scan */
  102. nspc = 0;
  103. for(j = TEXT_TAB-1;j >= 0;j--)
  104. {
  105. if(buff[j] != ' ') break;
  106. else nspc++;
  107. }
  108. limit = TEXT_TAB - nspc;
  109. memcpy(&dest[dest_idx],buff,limit);
  110. dest_idx += limit;
  111. if(nspc) dest[dest_idx++] = '\t';
  112. }
  113. else
  114. {
  115. limit = len - i;
  116. memcpy(&dest[dest_idx],&src[i],limit);
  117. dest_idx += limit;
  118. i += limit;
  119. break;
  120. }
  121. }
  122. dest[dest_idx] = '\0';
  123. }
  124. int __FASTCALL__ szTab2Space(char * dest,const char * src)
  125. {
  126. int i,k,len;
  127. size_t size;
  128. unsigned int freq;
  129. unsigned char ch;
  130. len = strlen(src);
  131. for(freq = 0,i = k = 0;i < len;i++,freq++)
  132. {
  133. ch = src[i];
  134. if(ch == '\t')
  135. {
  136. size = TEXT_TAB - (freq%TEXT_TAB);
  137. memset(&dest[k],' ',size);
  138. k += size;
  139. freq += size-1;
  140. }
  141. else
  142. {
  143. dest[k] = ch;
  144. k++;
  145. }
  146. }
  147. return k;
  148. }
  149. char * __FASTCALL__ szKillSpaceAround(char *str,char *place)
  150. {
  151. char *sptr;
  152. unsigned nmoves,len,idx,freq;
  153. unsigned char prev;
  154. unsigned char ch;
  155. prev = *place;
  156. len = strlen(str);
  157. *place = 0;
  158. idx = place - str;
  159. nmoves = szTrimTrailingSpace(str);
  160. sptr = place;
  161. freq = 0;
  162. sptr++;
  163. while((ch = *sptr) != 0)
  164. {
  165. if(isspace(ch)) freq++;
  166. else break;
  167. sptr++;
  168. }
  169. memmove(&str[idx-nmoves],&str[idx+freq],len-idx+1-freq);
  170. str[idx-nmoves] = prev;
  171. return &str[idx-nmoves];
  172. }
  173. #if __WORDSIZE == 16
  174. void huge * __FASTCALL__ HMemCpy(void huge *_dest, const void huge *_source, unsigned long n)
  175. {
  176. long i;
  177. for(i = 0;i < n;i++)
  178. {
  179. ((char huge *)_dest)[i] = ((const char huge *)_source)[i];
  180. }
  181. return _dest;
  182. }
  183. #endif
  184. #ifdef __GNUC__
  185. /* (emx+gcc) -- Copyright (c) 1990-1995 by Eberhard Mattes */
  186. char *ltoa (long value, char *string, int radix)
  187. {
  188. char *dst;
  189. dst = string;
  190. if (radix < 2 || radix > 36) *dst = 0;
  191. else
  192. {
  193. unsigned long x;
  194. int i, n;
  195. char digits[32];
  196. if (radix == 10 && value < 0)
  197. {
  198. *dst++ = '-';
  199. x = -value;
  200. }
  201. else x = value;
  202. i = 0;
  203. do
  204. {
  205. n = x % radix;
  206. digits[i++] = n+(n < 10 ? '0' : 'A'-10);
  207. x /= radix;
  208. } while (x != 0);
  209. while (i > 0) *dst++ = digits[--i];
  210. *dst = 0;
  211. }
  212. return string;
  213. }
  214. char *ultoa (unsigned long value, char *string, int radix)
  215. {
  216. char *dst;
  217. dst = string;
  218. if (radix < 2 || radix > 36) *dst = 0;
  219. else
  220. {
  221. int i;
  222. unsigned n;
  223. char digits[32];
  224. i = 0;
  225. do
  226. {
  227. n = value % radix;
  228. digits[i++] = n+(n < 10 ? '0' : 'A'-10);
  229. value /= radix;
  230. } while (value != 0);
  231. while (i > 0) *dst++ = digits[--i];
  232. *dst = 0;
  233. }
  234. return string;
  235. }
  236. #endif
  237. #if __WORDSIZE >= 32
  238. char *lltoa (long long int value, char *string, int radix)
  239. {
  240. char *dst;
  241. dst = string;
  242. if (radix < 2 || radix > 36) *dst = 0;
  243. else
  244. {
  245. unsigned long long int x;
  246. int i, n;
  247. char digits[64];
  248. if (radix == 10 && value < 0)
  249. {
  250. *dst++ = '-';
  251. x = -value;
  252. }
  253. else x = value;
  254. i = 0;
  255. do
  256. {
  257. n = x % radix;
  258. digits[i++] = n+(n < 10 ? '0' : 'A'-10);
  259. x /= radix;
  260. } while (x != 0);
  261. while (i > 0) *dst++ = digits[--i];
  262. *dst = 0;
  263. }
  264. return string;
  265. }
  266. char *ulltoa (unsigned long long int value, char *string, int radix)
  267. {
  268. char *dst;
  269. dst = string;
  270. if (radix < 2 || radix > 36) *dst = 0;
  271. else
  272. {
  273. int i;
  274. unsigned n;
  275. char digits[64];
  276. i = 0;
  277. do
  278. {
  279. n = value % radix;
  280. digits[i++] = n+(n < 10 ? '0' : 'A'-10);
  281. value /= radix;
  282. } while (value != 0);
  283. while (i > 0) *dst++ = digits[--i];
  284. *dst = 0;
  285. }
  286. return string;
  287. }
  288. #endif
  289. /*
  290. Using own code for qsort and bsearch functions is guarantee of stable work */
  291. /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
  292. /* Modified for use with 16-bits huge arrays by Nickols_K */
  293. /*-
  294. * Copyright (c) 1980, 1983 The Regents of the University of California.
  295. * All rights reserved.
  296. *
  297. * Redistribution and use in source and binary forms are permitted
  298. * provided that: (1) source distributions retain this entire copyright
  299. * notice and comment, and (2) distributions including binaries display
  300. * the following acknowledgement: ``This product includes software
  301. * developed by the University of California, Berkeley and its contributors''
  302. * in the documentation or other materials provided with the distribution
  303. * and in all advertising materials mentioning features or use of this
  304. * software. Neither the name of the University nor the names of its
  305. * contributors may be used to endorse or promote products derived
  306. * from this software without specific prior written permission.
  307. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  308. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  309. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  310. */
  311. /*
  312. * qsort.c:
  313. * Our own version of the system qsort routine which is faster by an average
  314. * of 25%, with lows and highs of 10% and 50%.
  315. * The THRESHold below is the insertion sort threshold, and has been adjusted
  316. * for records of size 48 bytes.
  317. * The MTHREShold is where we stop finding a better median.
  318. */
  319. #define THRESH 4 /**< threshold for insertion */
  320. #define MTHRESH 6 /**< threshold for median */
  321. static func_compare qcmp; /**< the comparison routine */
  322. static int qsz; /**< size of each record */
  323. static long thresh; /**< THRESHold in chars */
  324. static long mthresh; /**< MTHRESHold in chars */
  325. /**
  326. * qst:
  327. * Do a quicksort
  328. * First, find the median element, and put that one in the first place as the
  329. * discriminator. (This "median" is just the median of the first, last and
  330. * middle elements). (Using this median instead of the first element is a big
  331. * win). Then, the usual partitioning/swapping, followed by moving the
  332. * discriminator into the right place. Then, figure out the sizes of the two
  333. * partions, do the smaller one recursively and the larger one via a repeat of
  334. * this code. Stopping when there are less than THRESH elements in a partition
  335. * and cleaning up with an insertion sort (in our caller) is a huge win.
  336. * All data swaps are done in-line, which is space-losing but time-saving.
  337. * (And there are only three places where this is done).
  338. */
  339. static void __NEAR__ qst(char __HUGE__ *base, char __HUGE__ *max)
  340. {
  341. long ii,lo,hi;
  342. char __HUGE__ *i, __HUGE__ *j,__HUGE__ *jj;
  343. char __HUGE__ *mid, __HUGE__ *tmp;
  344. /*
  345. * At the top here, lo is the number of characters of elements in the
  346. * current partition. (Which should be max - base).
  347. * Find the median of the first, last, and middle element and make
  348. * that the middle element. Set j to largest of first and middle.
  349. * If max is larger than that guy, then it's that guy, else compare
  350. * max with loser of first and take larger. Things are set up to
  351. * prefer the middle, then the first in case of ties.
  352. */
  353. lo = max - base; /* number of elements as chars */
  354. do {
  355. mid = i = base + qsz * ((lo / qsz) >> 1);
  356. if (lo >= mthresh)
  357. {
  358. j = (qcmp((jj = base), i) > 0 ? jj : i);
  359. if (qcmp(j, (tmp = max - qsz)) > 0)
  360. {
  361. /* switch to first loser */
  362. j = (j == jj ? i : jj);
  363. if (qcmp(j, tmp) < 0)
  364. j = tmp;
  365. }
  366. if (j != i)
  367. {
  368. ii = qsz;
  369. do{
  370. __XchgB__(i,j);
  371. i++; j++;
  372. } while (--ii);
  373. }
  374. }
  375. /*
  376. * Semi-standard quicksort partitioning/swapping
  377. */
  378. for (i = base, j = max - qsz; ; )
  379. {
  380. while (i < mid && qcmp(i, mid) <= 0)
  381. i += qsz;
  382. while (j > mid)
  383. {
  384. if (qcmp(mid, j) <= 0)
  385. {
  386. j -= qsz;
  387. continue;
  388. }
  389. tmp = i + qsz; /* value of i after swap */
  390. if (i == mid)
  391. {
  392. /* j <-> mid, new mid is j */
  393. mid = jj = j;
  394. }
  395. else
  396. {
  397. /* i <-> j */
  398. jj = j;
  399. j -= qsz;
  400. }
  401. goto swap;
  402. }
  403. if (i == mid)
  404. {
  405. break;
  406. }
  407. else
  408. {
  409. /* i <-> mid, new mid is i */
  410. jj = mid;
  411. tmp = mid = i; /* value of i after swap */
  412. j -= qsz;
  413. }
  414. swap:
  415. ii = qsz;
  416. do{
  417. __XchgB__(i,jj);
  418. i++; jj++;
  419. } while (--ii);
  420. i = tmp;
  421. }
  422. /*
  423. * Look at sizes of the two partitions, do the smaller
  424. * one first by recursion, then do the larger one by
  425. * making sure lo is its size, base and max are update
  426. * correctly, and branching back. But only repeat
  427. * (recursively or by branching) if the partition is
  428. * of at least size THRESH.
  429. */
  430. i = (j = mid) + qsz;
  431. if ((lo = j - base) <= (hi = max - i))
  432. {
  433. if (lo >= thresh)
  434. qst(base, j);
  435. base = i;
  436. lo = hi;
  437. }
  438. else
  439. {
  440. if (hi >= thresh)
  441. qst(i, max);
  442. max = j;
  443. }
  444. } while (lo >= thresh);
  445. }
  446. /*
  447. * qsort:
  448. * First, set up some global parameters for qst to share. Then, quicksort
  449. * with qst(), and then a cleanup insertion sort ourselves. Sound simple?
  450. * It's not...
  451. */
  452. void __FASTCALL__ HQSort(void __HUGE__ *base0,unsigned long num, unsigned width,
  453. func_compare compare)
  454. {
  455. char __HUGE__ *base = (char __HUGE__ *)base0;
  456. char __HUGE__ *i, __HUGE__ *j, __HUGE__ *lo, __HUGE__ *hi;
  457. char __HUGE__ *min, __HUGE__ *max;
  458. register char c;
  459. if (num <= 1)
  460. return;
  461. qsz = width;
  462. qcmp = compare;
  463. thresh = qsz * THRESH;
  464. mthresh = qsz * MTHRESH;
  465. max = base + num * qsz;
  466. if (num >= THRESH)
  467. {
  468. qst(base, max);
  469. hi = base + thresh;
  470. }
  471. else
  472. {
  473. hi = max;
  474. }
  475. /*
  476. * First put smallest element, which must be in the first THRESH, in
  477. * the first position as a sentinel. This is done just by searching
  478. * the first THRESH elements (or the first n if n < THRESH), finding
  479. * the min, and swapping it into the first position.
  480. */
  481. for (j = lo = base; (lo += qsz) < hi; )
  482. if (qcmp(j, lo) > 0)
  483. j = lo;
  484. if (j != base)
  485. {
  486. /* swap j into place */
  487. for (i = base, hi = base + qsz; i < hi; )
  488. {
  489. __XchgB__(i,j);
  490. i++; j++;
  491. }
  492. }
  493. /*
  494. * With our sentinel in place, we now run the following hyper-fast
  495. * insertion sort. For each remaining element, min, from [1] to [n-1],
  496. * set hi to the index of the element AFTER which this one goes.
  497. * Then, do the standard insertion sort shift on a character at a time
  498. * basis for each element in the frob.
  499. */
  500. for (min = base; (hi = min += qsz) < max; )
  501. {
  502. while (qcmp(hi -= qsz, min) > 0)
  503. /* void */;
  504. if ((hi += qsz) != min) {
  505. for (lo = min + qsz; --lo >= min; )
  506. {
  507. c = *lo;
  508. for (i = j = lo; (j -= qsz) >= hi; i = j)
  509. *i = *j;
  510. *i = c;
  511. }
  512. }
  513. }
  514. }
  515. void __HUGE__ * __FASTCALL__ HLFind(const void *key,void __HUGE__ *base,unsigned long nelem,unsigned width,
  516. func_compare compare)
  517. {
  518. unsigned long iter,start,end;
  519. void __HUGE__ *it;
  520. tCompare comp_result;
  521. start = 0;
  522. end = nelem;
  523. iter = nelem >> 1;
  524. while(1)
  525. {
  526. it = (char __HUGE__ *)base + iter*width;
  527. comp_result = (*compare)(key,it);
  528. if(!comp_result) return it;
  529. if(end - start < 5) break;
  530. if(comp_result > 0) start = iter;
  531. else end = iter;
  532. iter = start + ((end - start) >> 1L);
  533. }
  534. for(iter = start;iter < end;iter++)
  535. {
  536. it = (char __HUGE__ *)base + iter*width;
  537. if(!(*compare)(key,it)) return it;
  538. }
  539. return NULL;
  540. }
  541. unsigned long __FASTCALL__ HLFindNearest(const void *key,void __HUGE__ *base,unsigned long nelem,
  542. unsigned width,
  543. func_compare compare)
  544. {
  545. unsigned long iter,start,end;
  546. tCompare comp_result,comp_result2;
  547. start = 0;
  548. end = nelem;
  549. iter = nelem >> 1;
  550. while(1)
  551. {
  552. comp_result = (*compare)(key,(char __HUGE__ *)base + iter*width);
  553. if(!comp_result) return iter;
  554. if(end - start < 5) break;
  555. if(comp_result > 0) start = iter;
  556. else end = iter;
  557. iter = start + ((end - start)>>1L);
  558. }
  559. for(iter = start;iter < end;iter++)
  560. {
  561. comp_result = (*compare)(key,(char __HUGE__ *)base + iter*width);
  562. comp_result2 = iter < (nelem-1) ? (*compare)(key,(char __HUGE__ *)base + (iter+1)*width):
  563. -1;
  564. if(comp_result >= 0 && comp_result2 < 0) return iter;
  565. }
  566. return comp_result < 0 ? (start ? start - 1 : 0L)
  567. : end == nelem ? nelem-1 : end;
  568. }
  569. /*
  570. print message when window system is not initialized
  571. only this function must be used for error reporting
  572. (do not use printf, fprintf, etc. !)
  573. */
  574. int printm(const char *str,...)
  575. {
  576. #define _out_ stderr
  577. int i;
  578. va_list args;
  579. va_start(args,str);
  580. i = vfprintf(_out_,str,args);
  581. va_end(args);
  582. fflush(_out_);
  583. return i;
  584. #undef _out_
  585. }
  586. linearArray * __FASTCALL__ la_Build( unsigned long nitems, unsigned size_of_item,
  587. void (__FASTCALL__ *mem_out)(const char *) )
  588. {
  589. linearArray * ret;
  590. ret = PMalloc(sizeof(linearArray));
  591. if(ret)
  592. {
  593. memset(ret,0,sizeof(linearArray));
  594. ret->itemSize = size_of_item;
  595. if(nitems)
  596. {
  597. ret->data = PHMalloc(nitems*size_of_item);
  598. if(ret->data)
  599. {
  600. ret->nSize = nitems;
  601. }
  602. }
  603. }
  604. else
  605. {
  606. if(mem_out) (*mem_out)("Creating array");
  607. }
  608. return ret;
  609. }
  610. void __FASTCALL__ la_ForEach(linearArray *obj,void (__FASTCALL__ *iter_func)(void __HUGE__ *))
  611. {
  612. unsigned long i;
  613. for(i = 0;i < obj->nItems;i++)
  614. {
  615. (*iter_func)(((char *)obj->data)+i*obj->itemSize);
  616. }
  617. }
  618. void __FASTCALL__ la_IterDestroy(linearArray *obj,void (__FASTCALL__ *del_it)(void __HUGE__ *))
  619. {
  620. la_ForEach(obj,del_it);
  621. PHFREE(obj->data);
  622. PFREE(obj);
  623. }
  624. void __FASTCALL__ la_Destroy(linearArray *obj)
  625. {
  626. if(obj)
  627. {
  628. PHFREE(obj->data);
  629. PFREE(obj);
  630. }
  631. }
  632. #define LST_STEP 16
  633. void __HUGE__* __FASTCALL__ la_AddData(linearArray *obj,const void *udata,void (__FASTCALL__ *mem_out)(const char *))
  634. {
  635. void __HUGE__*to;
  636. if(obj->nSize > ULONG_MAX - (LST_STEP+1)) return 0;
  637. if(obj->nItems + 1 > obj->nSize)
  638. {
  639. void *ptr;
  640. if(!obj->data) ptr = PHMalloc((obj->nSize+LST_STEP)*obj->itemSize);
  641. else ptr = PHRealloc(obj->data,obj->itemSize*(obj->nSize+LST_STEP));
  642. if(ptr)
  643. {
  644. obj->nSize = obj->nSize+LST_STEP;
  645. obj->data = ptr;
  646. }
  647. else
  648. {
  649. if(mem_out) (*mem_out)("Building List");
  650. return NULL;
  651. }
  652. }
  653. to = ((char __HUGE__ *)obj->data) + obj->nItems*obj->itemSize;
  654. HMemCpy(to,udata,obj->itemSize);
  655. obj->nItems++;
  656. return to;
  657. }
  658. void __FASTCALL__ la_DeleteData(linearArray *obj,unsigned long idx) {
  659. char __HUGE__ *from;
  660. char __HUGE__ *to;
  661. if(idx >= obj->nItems) return;
  662. to = ((char __HUGE__ *)obj->data) + idx*obj->itemSize;
  663. from = ((char __HUGE__ *)obj->data) + (idx+1)*obj->itemSize;
  664. memmove(to,from,(obj->nItems-(idx+1))*obj->itemSize);
  665. obj->nItems--;
  666. }
  667. void __FASTCALL__ la_Sort(linearArray *obj,func_compare compare)
  668. {
  669. if(obj)
  670. if(obj->nItems)
  671. HQSort(obj->data,obj->nItems,obj->itemSize,compare);
  672. }
  673. void __HUGE__ *__FASTCALL__ la_Find(linearArray * obj,const void *key,
  674. func_compare compare)
  675. {
  676. void __HUGE__ * ret = NULL;
  677. if(obj)
  678. if(obj->nItems)
  679. ret = HLFind(key,obj->data,obj->nItems,obj->itemSize,compare);
  680. return ret;
  681. }
  682. unsigned long __FASTCALL__ la_FindNearest(linearArray *obj,const void *key,
  683. func_compare compare)
  684. {
  685. unsigned long ret = 0L;
  686. if(obj)
  687. if(obj->nItems)
  688. ret = HLFindNearest(key,obj->data,obj->nItems,obj->itemSize,compare);
  689. return ret;
  690. }