home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / GPTX01AS.ZIP / HREGEX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-22  |  3.6 KB  |  120 lines

  1. /*  hregex.c - use re_search () and re_match () on huge buffers
  2.     Copyright (C) 1990 Free Software Foundation, Inc.
  3.     Francois Pinard <pinard@iro.umontreal.ca>, 1988.
  4.     Thorsten Ohl <ohl@gnu.ai.mit.edu>, 1990.
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 1, or (at your option)
  9.     any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     $Header: e:/gnu/gptx/RCS/hregex.c 0.1.0.1 90/09/22 00:23:03 tho Exp $
  21.  */
  22.  
  23. #if defined (_MSC_VER) && (_MSC_VER >= 600)
  24. #define HUGE _huge
  25. #else
  26. #define HUGE huge
  27. #endif
  28.  
  29. #define LONG long
  30. #define IMAX 0x7fffL
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <dos.h>
  35. #include <regex.h>
  36.  
  37. extern char *program_name;
  38.  
  39. /* Normalize a "huge" ix86 pointer by moving part of the offset into
  40.    the segment.  (We do it only if the offset is sufficiently large.)
  41.    Note:  Don't do this with pointers to memory blocks owned by malloc,
  42.       as a subsequent free () or realloc () will fail!  */
  43.  
  44. #define FP_NORM(fp)                \
  45.   if (FP_OFF (fp) & 0xf000)            \
  46.     {                        \
  47.       FP_SEG (fp) += (FP_OFF(fp) >> 4);        \
  48.       FP_OFF (fp) &= 0x0f;            \
  49.     }
  50.  
  51. /* This is a kludge for using the GNU regex library on huge (>32k)
  52.    buffers.  This "implementation" assumes that a match will be found in
  53.    the first 32k and will simply abort with a diagnostic message if not.
  54.    While this is certainly not a clean solution, it is appropriate in
  55.    cases where a failure is extremely unlikely, since it avoids the
  56.    enormous computational overhead of using huge pointers for regexp
  57.    matching, which is furthermore very error-prone.
  58.    Note: once these functions are real implementation, they should return
  59.    a long, but for the moment ints are more convenient.  */
  60.  
  61. int
  62. re_hsearch (struct re_pattern_buffer *pbufp, char HUGE *string, long size,
  63.         long startpos, long range, struct re_registers *regs)
  64. {
  65.   int match;
  66.   int isize = (int) min (size, IMAX);
  67.   int istartpos = (int) min (startpos, IMAX);
  68.   int irange = (int) min (range, IMAX);
  69.  
  70.   FP_NORM (string);
  71.  
  72.   match = re_search (pbufp, string, isize, istartpos, irange, regs);
  73.  
  74.   if (match == -1 && size > IMAX)
  75.     {
  76.       fprintf (stderr, "%s: re_search() failed in truncated buffer\n"
  77.                "        buffer: `%.30s...'\n"
  78.                "        length: %ld\n",
  79.                program_name, string, size);
  80.       abort ();
  81.     }
  82.  
  83.   return match;
  84. }
  85.  
  86. int
  87. re_hmatch (struct re_pattern_buffer *pbufp, char HUGE *string, long size,
  88.        long pos, struct re_registers *regs)
  89. {
  90.   int match;
  91.   int isize = (int) min (size, IMAX);
  92.   int ipos = (int) min (pos, IMAX);
  93.  
  94.   FP_NORM (string);
  95.  
  96.   match = re_match (pbufp, string, isize, ipos, regs);
  97.  
  98.   if (match == -1 && size > IMAX)
  99.     {
  100.       fprintf (stderr, "%s: re_match() failed against truncated buffer\n"
  101.                "        buffer: `%.30s...'\n"
  102.                "        length: %ld\n",
  103.                program_name, string, size);
  104.       abort ();
  105.     }
  106.  
  107.   return match;
  108. }
  109.  
  110.  
  111. /*
  112.    Local Variables:
  113.    mode:C
  114.    ChangeLog:ChangeLog
  115.    compile-command:make
  116.    End:
  117.  */
  118.  
  119.  
  120.