Пример #1
0
Файл: io.c Проект: hfst/hfst
void io_free(struct io_buf_handle *iobh) {
    if (iobh->io_buf != NULL) {
        xxfree(iobh->io_buf);
        (iobh->io_buf) = NULL;
    }
    xxfree(iobh);
}
Пример #2
0
void sigma_cleanup (struct fsm *net, int force) {
    int i,j,first,maxsigma,*attested;
    struct fsm_state *fsm;
    struct sigma *sig, *sig_prev, *sign;
    
    if (force == 0) {
        if (sigma_find_number(IDENTITY, net->sigma) != -1)
            return;
        if (sigma_find_number(UNKNOWN, net->sigma) != -1)
            return;
    }

    maxsigma = sigma_max(net->sigma);
    if (maxsigma < 0) { return; }
    attested = xxmalloc(sizeof(int)*(maxsigma+1));
    for (i=0; i<=maxsigma; i++)
        *(attested+i) = 0;
    fsm = net->states;
    for (i=0; (fsm+i)->state_no != -1; i++) {
        if ((fsm+i)->in >=0)
            *(attested+(fsm+i)->in) = 1;
        if ((fsm+i)->out >=0)
            *(attested+(fsm+i)->out) = 1;
    }
    for (i=3,j=3; i<=maxsigma;i++ ) {
        if (*(attested+i)) {
            *(attested+i) = j;
            j++;
        }
    }
    for (i=0; (fsm+i)->state_no != -1; i++) {        
        if ((fsm+i)->in > 2)
            (fsm+i)->in = *(attested+(fsm+i)->in);
        if ((fsm+i)->out > 2)
            (fsm+i)->out = *(attested+(fsm+i)->out);
    }
    sig_prev = NULL;
    for (sig = net->sigma; sig != NULL && sig->number != -1; sig = sign) {
        first = 1;
	sign = sig->next;
        if (!*(attested+(sig->number))) {
	    xxfree(sig->symbol);
	    xxfree(sig);
            if (sig_prev != NULL) {
                sig_prev->next = sign;
                first = 0;
            } else {
                first = 0;
                net->sigma = sign;
            }
        } else {
            sig->number = sig->number >= 3 ? *(attested+(sig->number)) : sig->number;
        }
        if (first)
            sig_prev = sig;
    }
    xxfree(attested);
    return;
}
Пример #3
0
static void nhash_free(struct nhash_list *nptr, int size) {
    struct nhash_list *nptr2, *nnext;
    int i;
    for (i=0; i < size; i++) {
        for (nptr2 = (nptr+i)->next; nptr2 != NULL; nptr2 = nnext) {
            nnext = nptr2->next;
            xxfree(nptr2);
        }
    }
    xxfree(nptr);
}
Пример #4
0
int fsm_isstarfree(struct fsm *net) {
    #define DFS_WHITE 0
    #define DFS_GRAY 1
    #define DFS_BLACK 2

    struct fsm *sfnet;
    struct state_array *state_array;
    struct fsm_state *curr_ptr;
    int v, vp, is_star_free;
    short int in;
    char *dfs_map;

    sfnet = fsm_subset(net, SUBSET_TEST_STAR_FREE);
    is_star_free = 1;

    state_array = map_firstlines(net);
    ptr_stack_clear();
    ptr_stack_push(state_array->transitions);

    dfs_map = xxcalloc(sfnet->statecount, sizeof(char));
    while(!ptr_stack_isempty()) {

        curr_ptr = ptr_stack_pop();
    nopop:
        v = curr_ptr->state_no; /* source state number */
        vp = curr_ptr->target;  /* target state number */

        if (v == -1 || vp == -1) {
            *(dfs_map+v) = DFS_BLACK;
            continue;
        }
        *(dfs_map+v) = DFS_GRAY;

        in = curr_ptr->in;
        if (*(dfs_map+vp) == DFS_GRAY && in == maxsigma) {
            /* Not star-free */
            is_star_free = 0;
            break;
        }
        if (v == (curr_ptr+1)->state_no) {
            ptr_stack_push(curr_ptr+1);
        }
        if (*(dfs_map+vp) == DFS_WHITE) { 
            curr_ptr = (state_array+vp)->transitions;
            goto nopop;
        }
    }
    ptr_stack_clear();
    xxfree(dfs_map);
    xxfree(state_array);
    //stack_add(sfnet);
    return(is_star_free);
}
Пример #5
0
void apply_clear_index(struct apply_handle *h) {
    if (h->index_in) {
	apply_clear_index_list(h, h->index_in);
	xxfree(h->index_in);
	h->index_in = NULL;
    }
    if (h->index_out) {
	apply_clear_index_list(h, h->index_out);
	xxfree(h->index_out);
	h->index_out = NULL;
    }
}
Пример #6
0
  static void * my_realloc_hook (void * ptr, size_t sz, const void *) {
    // NULL ptr = malloc.
    if (ptr == NULL) {
      return xxmalloc(sz);
    }

    if (sz == 0) {
      xxfree (ptr);
#if defined(__APPLE__)
      // 0 size = free. We return a small object.  This behavior is
      // apparently required under Mac OS X and optional under POSIX.
      return xxmalloc(1);
#else
      // For POSIX, don't return anything.
      return NULL;
#endif
    }

    size_t objSize = xxmalloc_usable_size(ptr);
    
#if 0
    // Custom logic here to ensure we only do a logarithmic number of
    // reallocations (with a constant space overhead).

    // Don't change size if the object is shrinking by less than half.
    if ((objSize / 2 < sz) && (sz <= objSize)) {
      // Do nothing.
      return ptr;
    }
    // If the object is growing by less than 2X, double it.
    if ((objSize < sz) && (sz < objSize * 2)) {
      sz = objSize * 2;
    }
#endif

    void * buf = xxmalloc(sz);

    if (buf != NULL) {
      // Successful malloc.
      // Copy the contents of the original object
      // up to the size of the new block.
      size_t minSize = (objSize < sz) ? objSize : sz;
      memcpy (buf, ptr, minSize);
      xxfree (ptr);
    }

    // Return a pointer to the new one.
    return buf;
  }
Пример #7
0
static void e_closure_free() {
    int i;
    struct e_closure_memo *eptr, *eprev;
    xxfree(marktable);
    for (i=0;i < num_states; i++) {
        eptr = (e_closure_memo+i)->next;
        for (eprev = NULL; eptr != NULL; ) {
            eprev = eptr;
            eptr = eptr->next;
            xxfree(eprev);
        }
        
    }
    xxfree(e_closure_memo);
}
Пример #8
0
struct fsm_read_handle *fsm_read_init(struct fsm *net) {
    struct fsm_read_handle *handle;
    struct fsm_state *fsm;
    int i, j, k, num_states, num_initials, num_finals, sno, *finals_head, *initials_head, *states_head;

    unsigned char *lookuptable;
    if (net == NULL) {return (NULL);}

    num_states = net->statecount;
    lookuptable = xxcalloc(num_states, sizeof(unsigned char));
    
    num_initials = num_finals = 0;

    for (i=0, fsm=net->states; (fsm+i)->state_no != -1; i++) {
        sno = (fsm+i)->state_no;
        if ((fsm+i)->start_state) {
            if (!(*(lookuptable+sno) & 1)) {
                *(lookuptable+sno) |= 1;
                num_initials++;
            }
            
        }
        if ((fsm+i)->final_state) {
            if (!(*(lookuptable+sno) & 2)) {
                *(lookuptable+sno) |= 2;
                num_finals++;
            }
        }
    }
    
    finals_head = xxcalloc(num_finals+1,sizeof(int));
    initials_head = xxcalloc(num_initials+1,sizeof(int));
    states_head = xxcalloc(num_states+1,sizeof(int));

    for (i=j=k=0; i < num_states; i++) {
        if (*(lookuptable+i) & 1) {
            *(initials_head+j) = i;
            j++;
        }
        if (*(lookuptable+i) & 2) {
            *(finals_head+k) = i;
            k++;
        }
        *(states_head+i) = i;
    }
    *(initials_head+j) = -1;    
    *(finals_head+k) = -1;
    *(states_head+i) = -1;

    xxfree(lookuptable);
    handle = xxcalloc(1,sizeof(struct fsm_read_handle));
    handle->finals_head = finals_head;
    handle->initials_head = initials_head;
    handle->states_head = states_head;
    
    handle->fsm_sigma_list = sigma_to_list(net->sigma);
    handle->sigma_list_size = sigma_max(net->sigma)+1;
    handle->arcs_head = fsm;
    return(handle);
}
Пример #9
0
Файл: io.c Проект: JSefara/foma
struct fsm *fsm_read_text_file(char *filename) {
    struct fsm_trie_handle *th;
    char *text, *textp1, *textp2;
    int lastword;

    text = file_to_mem(filename);
    if (text == NULL) {
	return NULL;
    }
    textp1 = text;
    th = fsm_trie_init();

    for (lastword = 0 ; lastword == 0 ; textp1 = textp2+1) {
	for (textp2 = textp1 ; *textp2 != '\n' && *textp2 != '\0'; textp2++) {
	}
	if (*textp2 == '\0') {
	    lastword = 1;
	    if (textp2 == textp1)
		break;
	}
	*textp2 = '\0';
	if (strlen(textp1) > 0)
	    fsm_trie_add_word(th, textp1);
    }
    xxfree(text);
    return(fsm_trie_done(th));
}
Пример #10
0
void apply_mark_flagstates(struct apply_handle *h) {
    int i;
    struct fsm_state *fsm;

    /* Create bitarray with those states that have a flag symbol on an arc */
    /* This is needed to decide whether we can perform a binary search.    */

    if (!h->has_flags || h->flag_lookup == NULL) {
	return;
    }
    if (h->flagstates) {
	xxfree(h->flagstates);
    }
    h->flagstates = xxcalloc(BITNSLOTS(h->last_net->statecount), sizeof(uint8_t));
    fsm = h->last_net->states;
    for (i=0; (fsm+i)->state_no != -1; i++) {
	if ((fsm+i)->target == -1) { 
	    continue;
	}
	if ((h->flag_lookup+(fsm+i)->in)->type) {
	    BITSET(h->flagstates,(fsm+i)->state_no);
	}
	if ((h->flag_lookup+(fsm+i)->out)->type) {
	    BITSET(h->flagstates,(fsm+i)->state_no);
	}
    }
}
Пример #11
0
int add_defined (struct fsm *net, char *string) {
  struct defined *defined, *defined_prev;
  int redefine;

  redefine = 0;
  if (net == NULL) { return 0; }
  fsm_count(net);
  if (defines == NULL) {
    defined = xxmalloc(sizeof(struct defined));
    defines = defined;
    defined->next = NULL;
  } else {
    for (defined = defines; defined != NULL; defined = defined->next) {
      defined_prev = defined;
      if (strcmp(defined->name, string) == 0) {
	redefine = 1;
	break;
      }
    }
    if (redefine == 0) {
      defined_prev->next = xxmalloc(sizeof(struct defined));
      defined = defined_prev->next;
      defined->next = NULL;
    }
  }
  if (redefine) {
      fsm_destroy(defined->net);
      xxfree(defined->name);
  }
  defined->name = xxstrdup(string);
  defined->net = net;
  return(redefine);
}
Пример #12
0
  static void * my_memalign_hook (size_t size, size_t alignment, const void *) {
    // Check for non power-of-two alignment, or mistake in size.
    if ((alignment == 0) ||
	(alignment & (alignment - 1)))
      {
	return NULL;
      }

    // Try to just allocate an object of the requested size.
    // If it happens to be aligned properly, just return it.
    void * ptr = xxmalloc (size);
    if (((size_t) ptr & (alignment - 1)) == (size_t) ptr) {
      // It is already aligned just fine; return it.
      return ptr;
    }

    // It was not aligned as requested: free the object.
    xxfree (ptr);

    // Now get a big chunk of memory and align the object within it.
    // NOTE: this REQUIRES that the underlying allocator be able
    // to free the aligned object, or ignore the free request.
    void * buf = xxmalloc (2 * alignment + size);
    void * alignedPtr = (void *) (((size_t) buf + alignment - 1) & ~(alignment - 1));

    return alignedPtr;
  }
Пример #13
0
struct fsm *fsm_construct_done(struct fsm_construct_handle *handle) {
    int i;
    struct fsm *net;
    struct fsm_state_list *sl;
    struct fsm_trans_list *trans, *transnext;
    struct fsm_sigma_hash *sigmahash, *sigmahashnext;

    sl = handle->fsm_state_list;
    if (handle->maxstate == -1 || handle->numfinals == 0) {
        return(fsm_empty_set());
    }
    fsm_state_init((handle->maxsigma)+1);
    for (i=0; i <= handle->maxstate; i++) {
        fsm_state_set_current_state(i, (sl+i)->is_final, (sl+i)->is_initial);
        for (trans = (sl+i)->fsm_trans_list; trans != NULL; trans = trans->next) {
            fsm_state_add_arc(i, trans->in, trans->out, trans->target, (sl+i)->is_final, (sl+i)->is_initial);
        }
        fsm_state_end_state();
    }

    net = fsm_create("");
    xxfree(net->sigma);
    fsm_state_close(net);
    
    net->sigma = fsm_construct_convert_sigma(handle);
    if (handle->name != NULL) {        
        strncpy(net->name, handle->name, 40);
        xxfree(handle->name);
    } else {
        sprintf(net->name, "%X",rand());
    }

    /* Free transitions */
    for (i=0; i < handle->fsm_state_list_size; i++) {
        trans = (((handle->fsm_state_list)+i)->fsm_trans_list);
        while (trans != NULL) {
            transnext = trans->next;
            xxfree(trans);
            trans = transnext;
        }
    }
    /* Free hash table */
    for (i=0; i < SIGMA_HASH_SIZE; i++) {
        sigmahash = (((handle->fsm_sigma_hash)+i)->next);
        while (sigmahash != NULL) {
            sigmahashnext = sigmahash->next;
            xxfree(sigmahash);
            sigmahash = sigmahashnext;
        }
    }
    xxfree(handle->fsm_sigma_list);
    xxfree(handle->fsm_sigma_hash);
    xxfree(handle->fsm_state_list);
    xxfree(handle);
    sigma_sort(net);
    return(net);   
}
Пример #14
0
int sigma_sort(struct fsm *net) {
#ifdef ORIGINAL
  int(*comp)() = ssortcmp;
#else
  int(*comp)(const void*, const void*) = ssortcmp;
#endif
  int size, i, max, *replacearray;
  struct ssort *ssort;
  struct sigma *sigma;
  struct fsm_state *fsm_state;
  
  size = sigma_max(net->sigma);
  if (size < 0) { return 1; }
  ssort = xxmalloc(sizeof(struct ssort)*size);

  for (i=0, sigma=net->sigma; sigma != NULL; sigma=sigma->next) {
    if (sigma->number > IDENTITY) {
      ssort[i].symbol = (char *)sigma->symbol;
      ssort[i].number = sigma->number;
      i++;
    }
  }
  max = i;
  qsort(ssort, max, sizeof(struct ssort), comp);
  replacearray = xxmalloc(sizeof(int)*(size+3));
  for (i=0; i<max; i++)
      replacearray[(ssort+i)->number] = i+3;

  /* Replace arcs */
  for(i=0, fsm_state = net->states; (fsm_state+i)->state_no != -1; i++) {
    if ((fsm_state+i)->in > IDENTITY)
      (fsm_state+i)->in = replacearray[(fsm_state+i)->in];
    if ((fsm_state+i)->out > IDENTITY)
      (fsm_state+i)->out = replacearray[(fsm_state+i)->out];
  }
  /* Replace sigma */
  for (i=0, sigma=net->sigma; sigma != NULL; sigma=sigma->next) {
    if (sigma->number > IDENTITY) {
      sigma->number = i+3;
      sigma->symbol = (ssort+i)->symbol;
      i++;
    }
  }
  xxfree(replacearray);
  xxfree(ssort);
  return(1);
}
Пример #15
0
void sh_done(struct sh_handle *sh) {
    int i;
    struct sh_hashtable *hash, *hashp;
    for (i=0; i < STRING_HASH_SIZE; i++) {
        hash = sh->hash + i;
        if (hash->string != NULL)
            xxfree(hash->string);
        for (hash=hash->next ; hash != NULL ; hash = hashp) {
            hashp = hash->next;
            if (hash->string != NULL)
                xxfree(hash->string);
            xxfree(hash);
        }
    }
    xxfree(sh->hash);
    xxfree(sh);
}
Пример #16
0
static void memoize_e_closure(struct fsm_state *fsm) {
    
    int i, state, laststate, *redcheck;
    struct e_closure_memo *ptr;
    
    e_closure_memo = xxcalloc(num_states,sizeof(struct e_closure_memo));
    marktable = xxcalloc(num_states,sizeof(int));
    /* Table for avoiding redundant epsilon arcs in closure */
    redcheck = xxmalloc(num_states*sizeof(int));

    for (i=0; i < num_states; i++) {
        ptr = e_closure_memo+i;
        ptr->state = i;
        ptr->target = NULL;
        *(redcheck+i) = -1;
    }

    laststate = -1;

    for (i=0; ;i++) {
        
        state = (fsm+i)->state_no;
        
        if (state != laststate) {
            if (!int_stack_isempty()) {                
                deterministic = 0;
                ptr = e_closure_memo+laststate;
                ptr->target = e_closure_memo+int_stack_pop();
                while (!int_stack_isempty()) {
                    ptr->next = xxmalloc(sizeof(struct e_closure_memo));
                    ptr->next->state = laststate;
                    ptr->next->target = e_closure_memo+int_stack_pop();
                    ptr->next->next = NULL;
                    ptr = ptr->next;
                }
            }
        }
        if (state == -1) {
            break;
        }
        if ((fsm+i)->target == -1) {
            continue;
        }
        /* Check if we have a redundant epsilon arc */
        if ((fsm+i)->in == EPSILON && (fsm+i)->out == EPSILON) {
            if (*(redcheck+((fsm+i)->target)) != (fsm+i)->state_no) {
                if ((fsm+i)->target != (fsm+i)->state_no) {
                    int_stack_push((fsm+i)->target);
                    *(redcheck+((fsm+i)->target)) = (fsm+i)->state_no;
                }
            }
            laststate = state;
        }
    }
    xxfree(redcheck);
}
Пример #17
0
struct sigma *sigma_remove(char *symbol, struct sigma *sigma) {
  struct sigma *sigma_start, *sigma_prev = NULL;
  sigma_prev = NULL;
  sigma_start = sigma;
  for ( ; sigma != NULL && sigma->number != -1; sigma_prev = sigma, sigma=sigma->next) {
    if (strcmp(sigma->symbol,symbol) == 0) {
      if (sigma_prev == NULL) {
	sigma_start = sigma->next;
	xxfree(sigma->symbol);
	xxfree(sigma);
      } else {
	(sigma_prev)->next = sigma->next;
	xxfree(sigma->symbol);
	xxfree(sigma);
      }
      break;
    }
  }
  return(sigma_start);
}
Пример #18
0
struct sigma *sigma_remove_num(int num, struct sigma *sigma) {
  struct sigma *sigma_start, *sigma_prev = NULL;
  sigma_prev = NULL;
  sigma_start = sigma;
  for ( ; sigma != NULL && sigma->number != -1; sigma_prev = sigma, sigma=sigma->next) {
    if (sigma->number == num) {
      if (sigma_prev == NULL) {
	sigma_start = sigma->next;
	xxfree(sigma->symbol);
	xxfree(sigma);
      } else {
	(sigma_prev)->next = sigma->next;
	xxfree(sigma->symbol);
	xxfree(sigma);
      }
      break;
    }
  }
  return(sigma_start);
}
Пример #19
0
void
xxflush(int intr)
{
	struct xx *xp, *xq;

	for (xp = xx_head; xp != 0 && !(intr && wwinterrupt()); xp = xq) {
		switch (xp->cmd) {
		case xc_move:
			if (xp->link == 0)
				(*tt.tt_move)(xp->arg0, xp->arg1);
			break;
		case xc_scroll:
			xxflush_scroll(xp);
			break;
		case xc_inschar:
			(*tt.tt_move)(xp->arg0, xp->arg1);
			tt.tt_nmodes = xp->arg3;
			(*tt.tt_inschar)(xp->arg2);
			break;
		case xc_insspace:
			(*tt.tt_move)(xp->arg0, xp->arg1);
			(*tt.tt_insspace)(xp->arg2);
			break;
		case xc_delchar:
			(*tt.tt_move)(xp->arg0, xp->arg1);
			(*tt.tt_delchar)(xp->arg2);
			break;
		case xc_clear:
			(*tt.tt_clear)();
			break;
		case xc_clreos:
			(*tt.tt_move)(xp->arg0, xp->arg1);
			(*tt.tt_clreos)();
			break;
		case xc_clreol:
			(*tt.tt_move)(xp->arg0, xp->arg1);
			(*tt.tt_clreol)();
			break;
		case xc_write:
			(*tt.tt_move)(xp->arg0, xp->arg1);
			tt.tt_nmodes = xp->arg3;
			(*tt.tt_write)(xp->buf, xp->arg2);
			break;
		}
		xq = xp->link;
		xxfree(xp);
	}
	if ((xx_head = xp) == 0) {
		xx_tail = 0;
		xxbufp = xxbuf;
	}
	ttflush();
}
Пример #20
0
void apply_clear_index_list(struct apply_handle *h, struct apply_state_index **index) {
    int i, j, statecount;
    struct apply_state_index *iptr, *iptr_tmp, *iptr_zero;
    if (index == NULL)
	return;
    statecount = h->last_net->statecount;
    for (i = 0; i < statecount; i++) {
	iptr = *(index+i);
	if (iptr == NULL) {
	    continue;
	}
	iptr_zero = *(index+i);
	for (j = h->sigma_size - 1 ; j >= 0; j--) { /* Make sure to not free the list in EPSILON    */
	    iptr = *(index+i) + j;                  /* as the other states lists' tails point to it */
	    for (iptr = iptr->next ; iptr != NULL && iptr != iptr_zero; iptr = iptr_tmp) {
		iptr_tmp = iptr->next;
		xxfree(iptr);
	    }
	}
	xxfree(*(index+i));
    }
}
Пример #21
0
/* no check for duplicates                    */
int sigma_substitute(char *symbol, char *sub, struct sigma *sigma) {
    if (sigma->number == -1) {
        return -1;
    }
    for (; sigma != NULL && sigma->number != -1 ; sigma = sigma->next) {
        if (strcmp(sigma->symbol, symbol) == 0) {
	    xxfree(sigma->symbol);
	    sigma->symbol = strdup(sub);
            return(sigma->number);
        }
    }
    return -1;
}
Пример #22
0
void fsm_trie_add_word(struct fsm_trie_handle *th, char *word) {
    int i, len;
    char *wcopy;
    wcopy = xxstrdup(word);
    len = strlen(wcopy);
    for (i=0 ; *word != '\0' && i < len; word = word + utf8skip(word)+1, i++) {
	strncpy(wcopy, word, utf8skip(word)+1);
	*(wcopy+utf8skip(word)+1) = '\0';
	fsm_trie_symbol(th, wcopy, wcopy);
    }
    xxfree(wcopy);
    fsm_trie_end_word(th);
}
Пример #23
0
void fsm_read_done(struct fsm_read_handle *handle) {
    xxfree(handle->lookuptable);
    xxfree(handle->fsm_sigma_list);
    xxfree(handle->finals_head);
    xxfree(handle->initials_head);
    xxfree(handle->states_head);
    xxfree(handle);
}
Пример #24
0
Файл: io.c Проект: JSefara/foma
struct fsm *fsm_read_binary_file_multiple(fsm_read_binary_handle fsrh) {
    char *net_name;
    struct fsm *net;
    struct io_buf_handle *iobh;
    iobh = (struct io_buf_handle *) fsrh;
    net = io_net_read(iobh, &net_name);
    if (net == NULL) {
	io_free(iobh);
	return(NULL);
    } else {
	xxfree(net_name);
	return(net);
    }
}
Пример #25
0
int remove_defined (char *string) {
  struct defined *defined, *defined_prev;
  int exists = 0;
  defined_prev = NULL;
  /* Undefine all */
  if (string == NULL) {
      for (defined = defines; defined != NULL; ) {
          fsm_destroy(defined->net);
          defined_prev = defined;
          defined = defined->next;
	  xxfree(defined_prev->name);
          xxfree(defined_prev);
          defines = NULL;
      }
      return(0);
  }
  for (defined = defines; defined != NULL; defined = defined->next) {
      if (strcmp(defined->name, string) == 0) {
          exists = 1;
          break;
      }
      defined_prev = defined;
  }
  if (exists == 0) { 
      //if (mode == 1) printf("*** Error: %s has no network definition.\n", string);
      return 1;
  }
  if (defined_prev != NULL) {
      defined_prev->next = defined->next;
  } else {
      defines = defined->next;
  }
  fsm_destroy(defined->net);
  xxfree(defined->name);
  xxfree(defined);
  return(0);
}
Пример #26
0
struct fsm *fsm_trie_done(struct fsm_trie_handle *th) {
    struct trie_hash *thash, *thashp;
    struct fsm *newnet;
    struct fsm_construct_handle *newh;
    unsigned int i;

    newh = fsm_construct_init("name");
    for (i = 0; i < THASH_TABLESIZE; i++) {
	thash = (th->trie_hash)+i;
	for ( ; thash != NULL; thash = thash->next) {
	    if (thash->insym != NULL) {
		fsm_construct_add_arc(newh, thash->sourcestate, thash->targetstate, thash->insym, thash->outsym);
	    } else {
		break;
	    }		
	}
    }
    for (i = 0; i <= th->used_states; i++) {
	if ((th->trie_states+i)->is_final == 1) {
	    fsm_construct_set_final(newh, i);
	}
    }
    fsm_construct_set_initial(newh, 0);
    newnet = fsm_construct_done(newh);
    /* Free all mem */
    for (i=0; i < THASH_TABLESIZE; i++) {
	for (thash=((th->trie_hash)+i)->next; thash != NULL; thash = thashp) {
	    thashp = thash->next;
	    xxfree(thash);
	}
    }
    sh_done(th->sh_hash);
    xxfree(th->trie_states);
    xxfree(th->trie_hash);
    xxfree(th);
    return(newnet);
}
Пример #27
0
/* Frees memory associated with applies */
void apply_clear(struct apply_handle *h) {
    struct sigma_trie_arrays *sta, *stap;
    for (sta = h->sigma_trie_arrays; sta != NULL; ) {
	stap = sta;
	xxfree(sta->arr);
	sta = sta->next;
	xxfree(stap);
    }
    if (h->statemap != NULL) {
        xxfree(h->statemap);
        h->statemap = NULL;
    }
    if (h->marks != NULL) {
        xxfree(h->marks);
        h->marks = NULL;
    }
    if (h->searchstack != NULL) {
        xxfree(h->searchstack);
        h->searchstack = NULL;
    }
    if (h->sigs != NULL) {
        xxfree(h->sigs);
        h->sigs = NULL;
    }
    if (h->flag_lookup != NULL) {
        xxfree(h->flag_lookup);
        h->flag_lookup = NULL;
    }
    if (h->sigmatch_array != NULL) {
	xxfree(h->sigmatch_array);
	h->sigmatch_array = NULL;
    }
    h->last_net = NULL;
    h->iterator = 0;
    xxfree(h->outstring);
    xxfree(h);
}
Пример #28
0
Файл: io.c Проект: JSefara/foma
struct fsm *fsm_read_spaced_text_file(char *filename) {
    struct fsm_trie_handle *th;
    char *text, *textorig, *insym, *outsym, *t1, *t2, *l1, *l2;

    text = textorig = file_to_mem(filename);
    
    if (text == NULL)
	return NULL;
    th = fsm_trie_init();
    for (;;) {
	for ( ; *text != '\0' && *text == '\n'; text++) { }
	t1 = spacedtext_get_next_line(&text);
	if (t1 == NULL)
	    break;
	if (strlen(t1) == 0)
	    continue;
	t2 = spacedtext_get_next_line(&text);
	if (t2 == NULL || strlen(t2) == 0) {
	    for (l1 = t1; (insym = spacedtext_get_next_token(&l1)) != NULL; ) {
		if (strcmp(insym, "0") == 0)
		    fsm_trie_symbol(th,  "@_EPSILON_SYMBOL_@", "@_EPSILON_SYMBOL_@");
		else if (strcmp(insym, "%0") == 0)
		    fsm_trie_symbol(th,  "0", "0");
		else
		    fsm_trie_symbol(th,  insym, insym);
	    }
	    fsm_trie_end_word(th);
	} else {
	    for (l1 = t1, l2 = t2; ; ) {
		insym = spacedtext_get_next_token(&l1);
		outsym = spacedtext_get_next_token(&l2);
		if (insym == NULL && outsym == NULL)
		    break;
		if (insym == NULL || strcmp(insym, "0") == 0)
		    insym = "@_EPSILON_SYMBOL_@";
		if (strcmp(insym, "%0") == 0)
		    insym = "0";
		if (outsym == NULL || strcmp(outsym, "0") == 0)
		    outsym = "@_EPSILON_SYMBOL_@";
		if (strcmp(outsym, "%0") == 0)
		    outsym = "0";
		fsm_trie_symbol(th, insym, outsym);
	    }
	    fsm_trie_end_word(th);
	}
    }
    xxfree(textorig);
    return(fsm_trie_done(th));
}
Пример #29
0
void apply_create_sigmatch(struct apply_handle *h) {
    char *symbol;
    struct sigma_trie *st;
    int i, j, inlen, lastmatch, consumes;
    /* We create a sigmatch array only in case we match against a real string */
    if (((h->mode) & ENUMERATE) == ENUMERATE) {
	return;
    }
    symbol = h->instring;
    inlen = strlen(symbol);
    h->current_instring_length = inlen;
    if (inlen >= h->sigmatch_array_size) {
	xxfree(h->sigmatch_array);
	h->sigmatch_array = xxmalloc(sizeof(struct sigmatch_array)*(inlen));
	h->sigmatch_array_size = inlen;
    }
    for (i=0; i < inlen;  ) {
	st = h->sigma_trie;
	for (j=0, lastmatch = 0; ; j++) {
	    if (*(symbol+i+j) == '\0') {
		break;
	    }
	    st = st+(unsigned char)*(symbol+i+j);
	    if (st->signum != 0) {
		lastmatch = st->signum;
		if (st->next == NULL)
		    break;
		st = st->next;
	    } else if (st->next != NULL) {
		st = st->next;
	    } else {
		break;
	    }
	}
	if (lastmatch != 0) {
	    (h->sigmatch_array+i)->signumber = lastmatch;
	    consumes = strlen(*(h->sigs+lastmatch));
	    (h->sigmatch_array+i)->consumes = consumes;
	    i = i + consumes;
	} else {
	    /* Not found */
	    (h->sigmatch_array+i)->signumber = IDENTITY;
	    (h->sigmatch_array+i)->consumes = 1;
	    i++;
	}
    }
}
Пример #30
0
void flag_purge (struct fsm *net, char *name) {
    struct fsm_state *fsm;
    struct sigma *sigma;
    int i, *ftable, sigmasize;
    char *csym;
    sigmasize = sigma_max(net->sigma)+1;
    ftable = xxmalloc(sizeof(int) * sigmasize);
    fsm = net->states;
    for (i=0; i<sigmasize; i++)
        *(ftable+i)=0;
    
    for (sigma = net->sigma; sigma != NULL && sigma->number != -1; sigma = sigma->next) {
        
        if (flag_check(sigma->symbol)) {
            if (name == NULL) {
                *(ftable+(sigma->number)) = 1;
            } else {
                csym = (sigma->symbol) + 3;
                if (strncmp(csym,name,strlen(name)) == 0 && (strlen(csym)>strlen(name)) && (strncmp(csym+strlen(name),".",1) == 0 || strncmp(csym+strlen(name),"@",1) == 0)) {
                    *(ftable+(sigma->number)) = 1;
                }
            }
        }
    }
    for (i = 0; i < sigmasize; i++) {
	if (*(ftable+i)) {
	    net->sigma = sigma_remove_num(i, net->sigma);
	}
    }

    for (i=0; (fsm+i)->state_no != -1; i++) {
        if ((fsm+i)->in >= 0 && (fsm+i)->out >= 0) {
            if (*(ftable+(fsm+i)->in))
                (fsm+i)->in = EPSILON;
            if (*(ftable+(fsm+i)->out))
                (fsm+i)->out = EPSILON;
        }
    }

    xxfree(ftable);
    net->is_deterministic = net->is_minimized = net->is_epsilon_free = NO;
    return;
}