/[pcre]/code/trunk/maketables.c
ViewVC logotype

Diff of /code/trunk/maketables.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 24 by nigel, Sat Feb 24 21:38:25 2007 UTC revision 25 by nigel, Sat Feb 24 21:38:45 2007 UTC
# Line 30  See the file Tech.Notes for some informa Line 30  See the file Tech.Notes for some informa
30  */  */
31    
32    
33  /* This is a support program to generate the file chartables.c, containing  /* This file is compiled on its own as part of the PCRE library. However,
34  character tables of various kinds. They are built according to the local C  it is also included in the compilation of deftables.c, in which case the macro
35  locale. */  DEFTABLES is defined. */
   
 #include <ctype.h>  
 #include <stdio.h>  
 #include <string.h>  
36    
37    #ifndef DEFTABLES
38  #include "internal.h"  #include "internal.h"
39    #endif
40    
41  int main(void)  
42    
43    /*************************************************
44    *           Create PCRE character tables         *
45    *************************************************/
46    
47    /* This function builds a set of character tables for use by PCRE and returns
48    a pointer to them. They are build using the ctype functions, and consequently
49    their contents will depend upon the current locale setting. When compiled as
50    part of the library, the store is obtained via pcre_malloc(), but when compiled
51    inside deftables, use malloc().
52    
53    Arguments:   none
54    Returns:     pointer to the contiguous block of data
55    */
56    
57    unsigned const char *
58    pcre_maketables(void)
59  {  {
60    unsigned char *yield, *p;
61  int i;  int i;
 unsigned char cbits[cbit_length];  
62    
63  printf(  #ifndef DEFTABLES
64    "/*************************************************\n"  yield = (pcre_malloc)(tables_length);
65    "*      Perl-Compatible Regular Expressions       *\n"  #else
66    "*************************************************/\n\n"  yield = malloc(tables_length);
67    "/* This file is automatically written by the makechartables auxiliary \n"  #endif
   "program. If you edit it by hand, you might like to edit the Makefile to \n"  
   "prevent its ever being regenerated. */\n\n"  
   "/* This table is a lower casing table. */\n\n"  
   "unsigned char pcre_lcc[] = {\n");  
68    
69  printf("  ");  if (yield == NULL) return NULL;
70  for (i = 0; i < 256; i++)  p = yield;
   {  
   if ((i & 7) == 0 && i != 0) printf("\n  ");  
   printf("%3d", tolower(i));  
   if (i != 255) printf(",");  
   }  
 printf(" };\n\n");  
71    
72  printf(  /* First comes the lower casing table */
   "/* This table is a case flipping table. */\n\n"  
   "unsigned char pcre_fcc[] = {\n");  
73    
74  printf("  ");  for (i = 0; i < 256; i++) *p++ = tolower(i);
75  for (i = 0; i < 256; i++)  
76    {  /* Next the case-flipping table */
   if ((i & 7) == 0 && i != 0) printf("\n  ");  
   printf("%3d", islower(i)? toupper(i) : tolower(i));  
   if (i != 255) printf(",");  
   }  
 printf(" };\n\n");  
77    
78  printf(  for (i = 0; i < 256; i++) *p++ = islower(i)? toupper(i) : tolower(i);
   "/* This table contains bit maps for digits, letters, 'word' chars, and\n"  
   "white space. Each map is 32 bytes long and the bits run from the least\n"  
   "significant end of each byte. */\n\n"  
   "unsigned char pcre_cbits[] = {\n");  
79    
80  memset(cbits, 0, sizeof(cbits));  /* Then the character class tables */
81    
82    memset(p, 0, cbit_length);
83  for (i = 0; i < 256; i++)  for (i = 0; i < 256; i++)
84    {    {
85    if (isdigit(i)) cbits[cbit_digit  + i/8] |= 1 << (i&7);    if (isdigit(i)) p[cbit_digit  + i/8] |= 1 << (i&7);
   if (isalpha(i)) cbits[cbit_letter + i/8] |= 1 << (i&7);  
86    if (isalnum(i) || i == '_')    if (isalnum(i) || i == '_')
87                    cbits[cbit_word   + i/8] |= 1 << (i&7);                    p[cbit_word   + i/8] |= 1 << (i&7);
88    if (isspace(i)) cbits[cbit_space  + i/8] |= 1 << (i&7);    if (isspace(i)) p[cbit_space  + i/8] |= 1 << (i&7);
89    }    }
90    p += cbit_length;
91    
92  printf("  ");  /* Finally, the character type table */
 for (i = 0; i < cbit_length; i++)  
   {  
   if ((i & 7) == 0 && i != 0)  
     {  
     if ((i & 31) == 0) printf("\n");  
     printf("\n  ");  
     }  
   printf("0x%02x", cbits[i]);  
   if (i != cbit_length - 1) printf(",");  
   }  
 printf(" };\n\n");  
93    
 printf(  
   "/* This table identifies various classes of character by individual bits:\n"  
   "  0x%02x   white space character\n"  
   "  0x%02x   letter\n"  
   "  0x%02x   decimal digit\n"  
   "  0x%02x   hexadecimal digit\n"  
   "  0x%02x   alphanumeric or '_'\n"  
   "  0x%02x   regular expression metacharacter or binary zero\n*/\n\n",  
   ctype_space, ctype_letter, ctype_digit, ctype_xdigit, ctype_word,  
   ctype_meta);  
   
 printf("unsigned char pcre_ctypes[] = {\n");  
   
 printf("  ");  
94  for (i = 0; i < 256; i++)  for (i = 0; i < 256; i++)
95    {    {
96    int x = 0;    int x = 0;
# Line 130  for (i = 0; i < 256; i++) Line 100  for (i = 0; i < 256; i++)
100    if (isxdigit(i)) x += ctype_xdigit;    if (isxdigit(i)) x += ctype_xdigit;
101    if (isalnum(i) || i == '_') x += ctype_word;    if (isalnum(i) || i == '_') x += ctype_word;
102    if (strchr("*+?{^.$|()[", i) != 0) x += ctype_meta;    if (strchr("*+?{^.$|()[", i) != 0) x += ctype_meta;
103      *p++ = x;
   if ((i & 7) == 0 && i != 0)  
     {  
     printf(" /* ");  
     if (isprint(i-8)) printf(" %c -", i-8);  
       else printf("%3d-", i-8);  
     if (isprint(i-1)) printf(" %c ", i-1);  
       else printf("%3d", i-1);  
     printf(" */\n  ");  
     }  
   printf("0x%02x", x);  
   if (i != 255) printf(",");  
104    }    }
105    
106  printf("};/* ");  return yield;
 if (isprint(i-8)) printf(" %c -", i-8);  
   else printf("%3d-", i-8);  
 if (isprint(i-1)) printf(" %c ", i-1);  
   else printf("%3d", i-1);  
 printf(" */\n\n/* End of chartables.c */\n");  
   
 return 0;  
107  }  }
108    
109  /* End of maketables.c */  /* End of maketables.c */

Legend:
Removed from v.24  
changed lines
  Added in v.25

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12