                           /* words.c, 28 Apr 10 */

#include <ctype.h>
#include <stdio.h>
#include <string.h>
 
#include "global.h"
#include "errors.h"
#include "isprop.h"
#include "utils.h"
#include "matchnum.h"
#include "singlefile.h"
#include "words.h"


typedef DIGIT_CHAR word_t[MAX_WORD_LEN];

BOOL do_anal_words = FALSE;
int word_buf_ptr = 0;
word_t *word_buf;

static int cur_word_len;
static word_t cur_word;


void process_word(void)
{
// Copy a sequence of word-like characters into a buffer.
// Allow words to contains digits, ie, be identifier-like.
if (do_print_word)
   {
   cur_word_len=1;
   cur_word[0]=cur_char;
   get_next_char();

   while (is_ident_ch(cur_char))
      {
      if (cur_word_len < MAX_WORD_LEN)
         {
         cur_word[cur_word_len]=cur_char;
         cur_word_len++;
         }
      get_next_char();
      }
   if (cur_word_len < MAX_WORD_LEN)
      cur_word[cur_word_len]=NULL_CHAR;
   else
      cur_word[cur_word_len-1]=NULL_CHAR;

   strcpy(word_buf[word_buf_ptr], cur_word);
   word_buf_ptr=(word_buf_ptr+1) % max_words;
   }
else // Skip over word characters
   {
   get_next_char();
   while (is_ident_ch(cur_char))
      get_next_char();
   }
}


void reset_word_buffer(void)
{
int wb;

for (wb=0; wb < max_words; wb++)
   word_buf[0][wb]=NULL_CHAR;
}


void print_word_buf(void)
{
int start_ptr = (word_buf_ptr+1) % max_words;

while (start_ptr != word_buf_ptr)
   {
   if (word_buf[0][start_ptr] != NULL_CHAR)
      printf("w %s\n", word_buf[start_ptr]);
   start_ptr=(start_ptr+1) % max_words;
   }
printf("%s\n", word_buf[word_buf_ptr]);

}


void init_words(int num_to_track)
{
word_buf=get_heap(sizeof(word_t)*max_words);
}

