/*{{{  #includes*/
#ifdef CONFIG_H
#   include "config.h"
#endif

#include <sys/types.h>
#include <ctype.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>

#include <local/bool.h>

#define SET_C
#define I_MESSAGES_C
#define I_SCREEN_C

#include "origami.h"

#define GETRC

#include "../h/rcformat.h"
/*}}}  */

/*{{{  variables*/
private Set *setlist;
/*}}}  */

/*{{{  init_set*/
public bool init_set(FILE *rc)
{ int no=rc_get_w(rc);
  int i;

  if (!(setlist=(Set*)malloc(no)))
   { oputs((char *)M_NO_MEMORY);
     oputc('\n');
     return(TRUE);
   }
  no=no>>5;
  while (no--)
     for (i=0;i<sizeof(Set);i++)
        setlist[no][i]=rc_get_c(rc);

  return(FALSE);
}
/*}}}  */
/*{{{  set_copy*/
/*{{{  poke_char*/
private void poke_char(int x,Set setcode)
{ setcode[x>>3] |= (1<<(x & 7));
}
/*}}}  */

public void set_copy(int to,int from)
{ int i;

  /*{{{  clear set*/
  for (i=0;i<32;setlist[to][i]=0,i++);
  /*}}}  */
  /*{{{  copy new valu*/
  switch (from)
   { case ALPHA:
     case UPPER:
     case LOWER:
      /*{{{  set the chars*/
        if (from!=LOWER)
         /*{{{  mark all uppercase*/
           for (i=0;i<256;i++)
              if (isalpha(i)&&isupper(i))
                 poke_char(i,setlist[to]);
         /*}}}  */
        if (from!=UPPER)
         /*{{{  mark all lowercase*/
            for (i=0;i<256;i++)
               if (isalpha(i)&&islower(i))
                  poke_char(i,setlist[to]);
         /*}}}  */
        break;
      /*}}}  */
     case DIGIT:
      /*{{{  mark all digits*/
        for (i=0;i<256;i++)
           if (isdigit(i))
             poke_char(i,setlist[to]);
        break;
      /*}}}  */
     default:
      /*{{{  simply copy*/
        for (i=0;i<32;setlist[to][i]=setlist[from][i],i++);
        break;
      /*}}}  */
   }
  /*}}}  */
}
/*}}}  */
/*{{{  test_char_set*/
public bool test_char_set(int x,int setno)
{  switch (setno)
   { case ALPHA: return((bool)isalpha(x));
     case UPPER: return((bool)(isalpha(x)&&isupper(x)));
     case LOWER: return((bool)(isalpha(x)&&islower(x)));
     case DIGIT: return((bool)isdigit(x));
     default:
      { return((bool)((1<<(x&7))&setlist[setno][x>>3]));}
   }
}
/*}}}  */
