Exemplo n.º 1
0
/** Inserts an item into a specific index in the list. Moves the
    existing items at that index or at a higher index to the right. If
    needed, it will increase the capacity of the list to match the new
    length of the list.

    @param l The list to insert an item into.
    
    @param index The index to put the new item. If index == length,
    the item is appended to the end of the array. If index is negative
    or greater than the length of the list, an error occurs.

    @param item A pointer to the item to store in the list.
    
    @return 1 if successful, 0 otherwise. Failure can occur if we
    needed to allocate more space but failed to, if the index was not
    valid.
 */
int list_insert(list *l, int index, void *item)
{
	list_sanity_check(l);
	
	/* If we are inserting the item at the end of the list */
	if(index == l->length)
		return list_set(l, index, item);

	if(list_move(l, index, index+1,
	             l->length - index) == 0)
		return 0;

	/* Set the new item */
	return list_set(l, index, item);
}
Exemplo n.º 2
0
int
main (void)
{
    list * test = list_new ();
    int i, * tmp, penis;
    for (i = 0; i < 15; i++)
    {
        tmp = (int *) calloc (1, sizeof (int));
        *tmp = i;
        list_append (test, *tmp);
    }

    printf ("length: %d\n", test->length);

    tmp = (int *) calloc (1, sizeof (int));
    *tmp = 69,
    list_set (test, 127, *tmp);

    for (i = 0; i < 15; i++)
    {
        list_get (test, i, penis);
        printf ("element: %d\tvalue: %d\n", i, penis);
    }

    list_get (test, 127, penis);
    printf ("element: 127\tvalue: %d\n", penis);

    list_free (test);

    return 0;
}
Exemplo n.º 3
0
/* Replace all file names in a list with temporary files with the same
 * name extension as the original name. */
static void m2c_replace_temp_file_name(struct list_t *file_list)
{
	int index;

	char *name;
	char *name_ext;
	char new_name[MAX_STRING_SIZE];

	LIST_FOR_EACH(file_list, index)
	{
		/* Get old name and its extension */
		name = list_get(file_list, index);
		name_ext = rindex(name, '.');
		if (!name_ext || rindex(name_ext, '/'))
			name_ext = "";

		/* Create new file */
		strcpy(new_name, "/tmp/XXXXXX");
		mktemp(new_name);
		if (name_ext)
			strcat(new_name, name_ext);

		/* Replace it */
		free(name);
		list_set(file_list, index, xstrdup(new_name));
	}
Exemplo n.º 4
0
struct mips_file_desc_t *mips_file_desc_table_entry_new_guest_fd(
    struct mips_file_desc_table_t *table, enum mips_file_desc_kind_t kind,
    int guest_fd, int host_fd, char *path, int flags) {
  struct mips_file_desc_t *desc;

  int i;

  /* Look for a free entry */
  for (i = 0; i < list_count(table->mips_file_desc_list) && guest_fd < 0; i++)
    if (!list_get(table->mips_file_desc_list, i)) guest_fd = i;

  /* If no free entry was found, add new entry. */
  if (guest_fd < 0) {
    guest_fd = list_count(table->mips_file_desc_list);
    list_add(table->mips_file_desc_list, NULL);
  }

  /* Specified guest_fd may still be too large */
  for (i = list_count(table->mips_file_desc_list); i <= guest_fd; ++i) {
    list_add(table->mips_file_desc_list, NULL);
  }

  /* Create guest mips_file descriptor and return. */
  desc = mips_file_desc_create(kind, guest_fd, host_fd, flags, path);
  list_set(table->mips_file_desc_list, guest_fd, desc);

  /* Return */
  return desc;
}
Exemplo n.º 5
0
/* process_string()
 *
 * Handle lines like:
 *
 * '@STRING{TL = {Tetrahedron Lett.}}'
 *
 * p should point to just after '@STRING'
 *
 * In BibTeX, if a string is defined several times, the last one is kept.
 *
 */
static int
process_string( char *p )
{
	int n, status = BIBL_OK;
	newstr s1, s2, *t;
	newstrs_init( &s1, &s2, NULL );
	while ( *p && *p!='{' && *p!='(' ) p++;
	if ( *p=='{' || *p=='(' ) p++;
	p = process_bibtexline( skip_ws( p ), &s1, &s2, 0, NULL );
	if ( p==NULL ) { status = BIBL_ERR_MEMERR; goto out; }
	if ( s2.data ) {
		newstr_findreplace( &s2, "\\ ", " " );
	}
	if ( s1.data ) {
		n = list_find( &find, s1.data );
		if ( n==-1 ) {
			t = list_add( &find, &s1 );
			if ( t==NULL ) { status = BIBL_ERR_MEMERR; goto out; }
			if ( s2.data ) t = list_add( &replace, &s2 );
			else t = list_addc( &replace, "" );
			if ( t==NULL ) { status = BIBL_ERR_MEMERR; goto out; }
		} else {
			if ( s2.data ) t = list_set( &replace, n, &s2 );
			else t = list_setc( &replace, n, "" );
			if ( t==NULL ) { status = BIBL_ERR_MEMERR; goto out; }
		}
	}
out:
	newstrs_free( &s1, &s2, NULL );
	return status;
}
Exemplo n.º 6
0
/* process a single dir for cleaning. dir can be a $PKGDIR, $PKGDIR/All/, $PKGDIR/$CAT */
static uint64_t
qpkg_clean_dir(char *dirp, set *vdb)
{
	set *ll = NULL;
	struct dirent **fnames;
	int i, count;
	char buf[_Q_PATH_MAX * 2];
	struct stat st;
	uint64_t num_all_bytes = 0;
	size_t disp_units = 0;
	char **t;
	bool ignore;

	if (dirp == NULL)
		return 0;
	if (chdir(dirp) != 0)
		return 0;
	if ((count = scandir(".", &fnames, filter_tbz2, alphasort)) < 0)
		return 0;

	/* create copy of vdb with only basenames */
	for ((void)list_set(vdb, &t); *t != NULL; t++)
		ll = add_set_unique(basename(*t), ll, &ignore);

	for (i = 0; i < count; i++) {
		fnames[i]->d_name[strlen(fnames[i]->d_name)-5] = 0;
		if (contains_set(fnames[i]->d_name, ll))
			continue;
		snprintf(buf, sizeof(buf), "%s.tbz2", fnames[i]->d_name);
		if (lstat(buf, &st) != -1) {
			if (S_ISREG(st.st_mode)) {
				disp_units = KILOBYTE;
				if ((st.st_size / KILOBYTE) > 1000)
					disp_units = MEGABYTE;
				num_all_bytes += st.st_size;
				qprintf(" %s[%s%s %3s %s %s%s]%s %s%s/%s%s\n",
						DKBLUE, NORM, GREEN,
						make_human_readable_str(st.st_size, 1, disp_units),
						disp_units == MEGABYTE ? "M" : "K",
						NORM, DKBLUE, NORM, CYAN,
						basename(dirp), fnames[i]->d_name, NORM);
			}
			if (!pretend)
				unlink(buf);
		}
	}

	free_set(ll);
	scandir_free(fnames, count);

	return num_all_bytes;
}
Exemplo n.º 7
0
void tm_set( tm_obj self, tm_obj k, tm_obj v){
	switch( self.type ){
	case TM_LST:
	{
        if( TM_NUM != k.type){
            tm_raise("tm_set(), expect a number but see @", _tm_type(k));
        }
		int n = get_num(k);
		list_set( get_list(self), n, v);
	}return;
	case TM_DCT: dict_set( get_dict(self), k, v);return;
	}
	tm_raise(" tm_set: @[@] = @", self, k, v );
}
Exemplo n.º 8
0
void evg_opencl_program_initialize_constant_buffers(struct evg_opencl_program_t *program)
{
	struct elf_file_t *elf_file;
	struct elf_symbol_t *elf_symbol;
	struct elf_buffer_t elf_buffer;
	struct evg_opencl_mem_t *mem;
	char symbol_name[MAX_STRING_SIZE];
	int i;

	elf_file = program->elf_file;

	/* We can't tell how many constant buffers exist in advance, but we
	 * know they should be enumerated, starting with '2'.  This loop
	 * searches until a constant buffer matching the format is not found. */
	for (i = 2; i < 25; i++) 
	{
		/* Create string of symbol name */
		sprintf(symbol_name, "__OpenCL_%d_global", i);

		/* Check to see if symbol exists */
		elf_symbol = elf_symbol_get_by_name(elf_file, symbol_name);
                if (elf_symbol == NULL) {

			break;
		}
		evg_opencl_debug("  constant buffer '%s' found with size %d\n",
			elf_symbol->name, elf_symbol->size);

		/* Read the elf symbol into a buffer */
		evg_opencl_program_read_symbol(program, elf_symbol->name, &elf_buffer);

		/* Create a memory object and copy the constant buffer data to it */
		mem = evg_opencl_mem_create();
		mem->type = 0;  /* FIXME */
		mem->size = elf_buffer.size;
		mem->flags = 0; /* TODO Change to CL_MEM_READ_ONLY */
		mem->host_ptr = 0;

		/* Assign position in device global memory */
		mem->device_ptr = evg_emu->global_mem_top;
		evg_emu->global_mem_top += mem->size;

		/* Copy constant buffer into device memory */
		mem_write(evg_emu->global_mem, mem->device_ptr, mem->size, elf_buffer.ptr);

		/* Add the memory object to the constant buffer list */
		list_set(program->constant_buffer_list, i, mem);
	}
} 
Exemplo n.º 9
0
/**
 * IronBee callback function to manipulate an HTTP header
 *
 * @param[in] tx - IronBee transaction
 * @param[in] dir - Request/Response
 * @param[in] action - Requested header manipulation
 * @param[in] hdr - Header
 * @param[in] value - Header Value
 * @param[in] rx - Compiled regexp of value (if applicable)
 * @return status (OK, Declined if called too late, Error if called with
 *                 invalid data).  NOTIMPL should never happen.
 */
static ib_status_t ib_header_callback(ib_tx_t *tx, ib_server_direction_t dir,
                                      ib_server_header_action_t action,
                                      const char *hdr, const char *value,
                                      ib_rx_t *rx, void *cbdata)
{
    /* This is more complex for nginx than for other servers because
     * headers_in and headers_out are different structs, and there
     * are lots of enumerated headers to watch out for.
     *
     * It appears the enumerated headers are in fact just pointers
     * into the generic lists.  So with luck it should be sufficient
     * to deal with just the lists.  Revisit if we seem to get
     * unexpected failures in manipulating headers.
     *
     * That won't work for setting/unsetting a header altogether.
     * It's no use if we set the list but leave the enumerated
     * pointers uninitialized or dangling.
     */
    ngxib_req_ctx *ctx = tx->sctx;

    /* the headers list is common between request and response */
    ngx_list_t *headers = (dir == IB_SERVER_REQUEST)
                          ? &ctx->r->headers_in.headers
                          : &ctx->r->headers_out.headers;

    if (ctx->hdrs_out || (ctx->hdrs_in && (dir == IB_SERVER_REQUEST)))
        return IB_DECLINED;  /* too late for requested op */

    switch (action) {
      case IB_HDR_SET:
        list_set(headers, hdr, value);
        return IB_OK;
      case IB_HDR_UNSET:
        list_unset(headers, hdr);
        return IB_OK;
      case IB_HDR_ADD:
        list_add(headers, hdr, value);
        return IB_OK;
      case IB_HDR_MERGE:
      case IB_HDR_APPEND:
        list_append(headers, hdr, value);
        return IB_OK;
      case IB_HDR_EDIT:
        return list_edit(headers, hdr, value, tx, rx);
    }
    return IB_ENOTIMPL;
}
Exemplo n.º 10
0
void obj_set(Object self, Object k, Object v) {
    // gc_mark_single(k); // used between gc scan
    // gc_mark_single(v); // only need to mark single.
    switch (TM_TYPE(self)) {
    case TYPE_LIST: {
        tm_assert_type(k, TYPE_NUM, "obj_set");
        double d = GET_NUM(k);
        tm_assert_int(d, "list_set");
        list_set(GET_LIST(self), (int)d, v);
    }
        return;
    case TYPE_DICT:
        dict_set0(GET_DICT(self), k, v);
        return;
    }
    tm_raise("obj_set: Self %o, Key %o, Val %o", self, k, v);
}
Exemplo n.º 11
0
int main(void)
{
    struct arr_list *arr;
    int r;

    arr = create_arr_list(3);
    printf("list push: 5, 6, 7\n");
    list_push(arr, 5);
    list_push(arr, 6);
    list_push(arr, 7);
    print_arr_list(arr);

    printf("list push: 8, will auto expand\n");
    list_push(arr, 8);
    print_arr_list(arr);

    printf("list remove at 2\n");
    list_removeat(arr, 2);
    print_arr_list(arr);

    printf("list pop \n");
    list_pop(arr);
    print_arr_list(arr);

    printf("list insert 0\n");
    list_insert(arr, 0, 3);
    print_arr_list(arr);

    r = list_index(arr, 3);
    printf("list index 3:%d\n", r);
    r = list_index(arr, 7);
    printf("list index 7:%d\n", r);

    printf("list remove 3\n");
    list_remove(arr, 3);
    print_arr_list(arr);

    printf("list set index 0 = 3\n");
    list_set(arr, 0, 3);
    print_arr_list(arr);

    free_arr_list(arr);
    return 0;
}
Exemplo n.º 12
0
/** Appends an item onto the end of the list (at index l->length). If
    the capacity of the list is too small, it will be doubled.

    @param l The list to append an item onto.

    @param item The item to append to the end of the list.
    
    @return 1 if successful, 0 if failure. Failure can occur if we
    needed to expand the capacity of the list and we failed to do
    so. Failure can also occur if the list or the item is NULL.
*/
int list_append(list *l, void *item)
{
	list_sanity_check(l);

	if(item == NULL)
	{
		msg(ERROR, "The pointer to the item to append to the list was NULL.");
		return 0;
	}

	/* Make sure that we double the capacity when needed. list_set()
	 * would also allocate more space for us---but will only increase
	 * the capacity just as large as needed. */
	if(l->length == l->capacity)
		if(list_ensure_capacity(l, l->capacity*2) == 0)
			return 0;

	return list_set(l, l->length, item);
}
Exemplo n.º 13
0
int add_user(char *user){
    list *groups_from_user;
//    list *the_user;
    //printf("add_user(%s)\n",user);
    if(!list_find(current_group->users,user)){
        current_group->users=list_add(current_group->users,user,strdup(user));
    }
    groups_from_user=list_find(users,user);
    if(!groups_from_user){
        // the user isn't registered yet
        groups_from_user=list_add(NULL,current_group_name,current_group_name);
        users=list_add(users,user,groups_from_user);
    } else {
    // add the group name to the list of groups the user is bound to.
        if(!list_find(groups_from_user,current_group_name)) // don't add it if it is already set
            list_set(users,user,list_add(groups_from_user,current_group_name,current_group_name));
    }
    return 0;
}
Exemplo n.º 14
0
void mips_file_desc_table_entry_free(struct mips_file_desc_table_t *table,
                                     int index) {
  struct mips_file_desc_t *desc;

  /* Get mips_file descriptor. If it is empty or out of range, exit. */
  desc = list_get(table->mips_file_desc_list, index);
  if (!desc) return;

  /* If it is a virtual mips_file, delete the temporary host path. */
  if (desc->kind == mips_file_desc_virtual) {
    if (unlink(desc->path))
      warning("%s: temporary host virtual mips_file could not be deleted",
              desc->path);
  }

  /* Free mips_file descriptor and remove entry in table. */
  list_set(table->mips_file_desc_list, index, NULL);
  mips_file_desc_free(desc);
}
Exemplo n.º 15
0
void test_list(void) {
    int valuebuf[] = { 1 };
    size_t i;
    ListObject *lp = list_new();
    assert(list_is_empty(lp));
    for(i = 0; i < 10; i++) {
        *valuebuf = i;
        list_add(lp, valuebuf);
    }
    list_print(list_rsslice(lp, 0, 10, 1));
    list_print(list_sslice(lp, 9, -11, -1));
    list_print(list_rsslice(lp, -1, -1, -1));
    list_print(list_rsslice(lp, -1, -110, -1));
    list_print(list_rsslice(lp, -1, -11, -2));
    list_print(list_sslice(lp, -1, -3, -1));
    list_print(list_sslice(lp, -3, -1, 1));
    list_print(list_sslice(lp, -1, 7, -1));
    printf("get: %d\n", *(int*)list_get(lp, 2));
    printf("pop: %d\n", *(int*)list_popi(lp, 0));
    *valuebuf = -2;
    list_insert(lp, 0, valuebuf);
    *valuebuf = -200;
    list_insert(lp, -1, valuebuf);
    list_print(lp);
    list_del(lp, 0);
    list_rinsert(lp, -1, valuebuf);
    list_rinsert(lp, -1, valuebuf);
    printf("get: %d\n", *(int*)list_get(lp, 0));
    list_print(lp);
    ListObject *nlp = list_copy(lp);
    printf("new copy's size is %u, allocated is %u\n", nlp->used, nlp->allocated);
    list_print(nlp);
    printf("count %d: %d\n", *valuebuf, list_count(lp, valuebuf));
    *valuebuf=12333;
    printf("change index 0's value to 12333\n");
    list_set(nlp,0,valuebuf);
    *valuebuf=2;
    printf("remove first value equals 2\n");
    list_remove(nlp,valuebuf);
    list_print(nlp);
    list_free(nlp);
}
Exemplo n.º 16
0
Arquivo: set.c Projeto: jpcoles/htrack
//============================================================================
//                                list_append
//============================================================================
int list_append(list_t *list, uint64_t val)
{
    return list_set(list, val, list->len);
}
Exemplo n.º 17
0
/* This algorithm will be recursively called to do the backtracking of DFS algorithm. */
static void routing_table_cycle_detection_dfs_visit(struct net_routing_table_t *routing_table, struct list_t *buffer_list,
	struct list_t *color_list, struct list_t *parent_list, int list_elem, int buffer_count)
{
	int j;

	struct net_t *net = routing_table->net;
	struct net_buffer_t *parent_index;
	struct net_node_t *buffer_color;
	struct net_node_t *node_elem;
	struct net_buffer_t *buffer_elem;

	list_set(color_list, list_elem, NET_NODE_COLOR_GRAY);

	buffer_elem = list_get(buffer_list, list_elem);

	node_elem = buffer_elem->node;

	for (j = 0; j < routing_table->dim && !routing_table->has_cycle; j++)
	{

		struct net_node_t *node_adj;
		struct net_routing_table_entry_t *entry;

		node_adj = list_get(net->node_list, j);
		entry = net_routing_table_lookup(routing_table, node_elem, node_adj);

		if (entry->output_buffer == buffer_elem)
		{

			struct net_routing_table_entry_t *entry_adj;
			entry_adj = net_routing_table_lookup(routing_table, entry->next_node, node_adj);

			for (int i = 0; i < buffer_count; i++)
			{
				struct net_buffer_t *buffer_adj;
				buffer_adj = list_get(buffer_list, i);

				if (buffer_adj == entry_adj->output_buffer)
				{

					buffer_color = list_get(color_list, i);
					if(buffer_color == NET_NODE_COLOR_WHITE)
					{
						list_set(parent_list, i, buffer_elem);
						routing_table_cycle_detection_dfs_visit(routing_table, buffer_list, color_list, parent_list, i, buffer_count);
					}

					buffer_color = list_get(color_list, i);
					parent_index = list_get(parent_list, i);

					if (buffer_color == NET_NODE_COLOR_GRAY  && parent_index != buffer_elem)
					{
						warning("network %s: cycle found in routing table.\n%s",
							net->name, err_net_cycle);
						routing_table->has_cycle = 1;
					}
				}
			}
		}
	}
	list_set(color_list, list_elem, NET_NODE_COLOR_BLACK);
}
int main()
{
	int x = 0; char a;
	struct arraylist *list;

	printf("\nCreating arraylist...");
	list = list_create();
	if(list == NULL)
	{
		printf("\n list not created...\n");
		goto out;
	}
	else
		printf("[ok]\n");
	
	printf("testing list_insert...\n\t(1)valid position...");
	x = list_insert(list, 0, 'a');
	if(x == 1)
		printf("[ok]\n");
	else
		goto fail;
	if(! list_insert(list, 1, 'b'))
		goto fail;
	if(! list_insert(list, 2, 'c'))
		goto fail;
	printf("\t(2)invalid postion...");
	x = list_insert(list, 7,'d');
	if( x == 1)
		goto fail;
	else
		printf("[ok]\n");

	printf("testing list_get...");
	a = list_get(list, 1);
	if( a != 'b')
		goto fail;
	a = list_get(list, 7);
	if(a != 0)
		goto fail;
	else
		printf("[ok]\n");

	printf("testing list_remove...");
	list_remove(list, 1);
	a = list_get(list, 1);
	if( a != 'c')
		goto fail;
	else
		printf("[ok]\n");
	
	printf("testing list_set...");
	list_set(list, 0, 'e');
	if(list_get(list, 0) == 'e')
		printf("[ok]\n");
	else
		goto fail;
	printf("testing list_count...");
	x =list_count(list);
	if(x == 2)
		printf("[ok]\n");
	else
		goto fail;
	return 0;
fail:
	printf("[failed]\n");
out:
	return -1;
}
Exemplo n.º 19
0
static void timing_dia_refresh(struct vgpu_compute_unit_t *compute_unit)
{
	struct vgpu_t *vgpu = compute_unit->vgpu;
	int vgpu_cycle;
	int i, j;
	int err;

	/* Free timing diagram */
	if (compute_unit->timing_dia)
		free(compute_unit->timing_dia);

	/* Free uops in instruction list */
	vgpu_uop_list_clear(compute_unit->timing_inst_uops);

	/* Get size of diagram */
	timing_dia_get_size(compute_unit, &compute_unit->timing_dia_width, &compute_unit->timing_dia_height);
	compute_unit->timing_dia_cycle_first = compute_unit->timing_dia_hscrollbar_value / timing_dia_col_width;
	compute_unit->timing_dia_uop_first = compute_unit->timing_dia_vscrollbar_value / timing_dia_row_height;

	/* Create timing diagram */
	vgpu_cycle = vgpu->cycle;
	compute_unit->timing_dia = calloc(compute_unit->timing_dia_width * compute_unit->timing_dia_height, sizeof(struct timing_dia_entry_t));
	for (i = 0; i < compute_unit->timing_dia_width; i++)
	{
		struct vgpu_uop_t *uop_head;

		/* Go to cycle */
		err = vgpu_trace_cycle(vgpu, compute_unit->timing_dia_cycle_first + i);
		if (err)
			break;

		/* Get uop at the head of the list */
		uop_head = list_get(compute_unit->uop_list, 0);
		if (!uop_head)
			continue;

		/* Fill cycle column */
		for (j = 0; j < compute_unit->timing_dia_height; j++)
		{
			struct vgpu_uop_t *uop, *dup_uop;
			struct timing_dia_entry_t *timing_dia_entry;

			/* Get associated uop and diagram entries */
			uop = list_get(compute_unit->uop_list, j - uop_head->id + compute_unit->timing_dia_uop_first);
			if (!uop)
				continue;
			assert(uop->engine >= 0 && uop->engine < VGPU_ENGINE_COUNT);
			assert(uop->stage >= 0 && uop->stage < VGPU_STAGE_COUNT);

			/* If uop is not present in instruction list, make a copy */
			dup_uop = list_get(compute_unit->timing_inst_uops, j);
			if (!dup_uop)
			{
				dup_uop = vgpu_uop_dup(uop);
				while (list_count(compute_unit->timing_inst_uops) <= j)
					list_add(compute_unit->timing_inst_uops, NULL);
				list_set(compute_unit->timing_inst_uops, j, dup_uop);
			}

			/* Timing diagram cell */
			if (uop->finished && vgpu->cycle > uop->stage_cycle)
				continue;
			timing_dia_entry = &compute_unit->timing_dia[i * compute_unit->timing_dia_height + j];
			snprintf(timing_dia_entry->text, sizeof timing_dia_entry->text, "%s", vgpu_stage_name[uop->stage]);
			timing_dia_entry->fill = TRUE;
			timing_dia_entry->fill_r = vgpu_stage_color[uop->engine][uop->stage][0];
			timing_dia_entry->fill_g = vgpu_stage_color[uop->engine][uop->stage][1];
			timing_dia_entry->fill_b = vgpu_stage_color[uop->engine][uop->stage][2];
		}
	}

	/* Restore original vgpu cycle */
	vgpu_trace_cycle(vgpu, vgpu_cycle);
}
Exemplo n.º 20
0
int megahal_parse(const char *string, list_t **words) {
	list_t *words_p;
	uint_fast32_t offset, len;
	uint32_t size;
	word_t word;
	char *tmp;
	int ret;

	WARN_IF(string == NULL);
	WARN_IF(words == NULL);

	*words = list_alloc();
	if (*words == NULL) return -ENOMEM;
	words_p = *words;

	len = strlen(string);
	if (len == 0) return OK;

	offset = 0;
	while (1) {
		/*
		 * If the current character is of the same type as the previous
		 * character, then include it in the word. Otherwise, terminate
		 * the current word.
		 */
		if (boundary(string, offset, len)) {
			/*
			 * Add the word to the dictionary
			 */
			tmp = strndup(string, offset);
			if (tmp == NULL) return -ENOMEM;

			/*
			 * Truncate overly long words because they won't fit in the
			 * dictionary when saving.
			 */
			if (offset > UINT8_MAX)
				tmp[UINT8_MAX + 1] = 0;
			megahal_upper(tmp);

			ret = db_word_use(tmp, &word);
			free(tmp);
			if (ret) return ret;

			ret = list_append(words_p, word);
			if (ret) return ret;

			if (offset == len) break;
			string += offset;
			len = strlen(string);
			offset = 0;
		} else {
			offset++;
		}
	}

	/*
	 * If the last word isn't punctuation, then replace it with a
	 * full-stop character.
	 */
	tmp = NULL;
	ret = list_size(words_p, &size);
	if (ret) return ret;

	ret = list_get(words_p, size - 1, &word);
	if (ret) return ret;

	ret = db_word_str(word, &tmp);
	if (ret) return ret;

	if (isalnum((unsigned char)tmp[0])) {
		free(tmp);

		ret = db_word_use(".", &word);
		if (ret) return ret;

		ret = list_append(words_p, word);
		if (ret) return ret;
	} else if (strchr("!.?", (unsigned char)tmp[strlen(tmp) - 1]) == NULL) {
		free(tmp);

		ret = db_word_use(".", &word);
		if (ret) return ret;

		ret = list_set(words_p, size - 1, word);
		if (ret) return ret;
	} else {
		free(tmp);
	}

	return OK;
}
Exemplo n.º 21
0
/* ----------------------------------------------------
 *  Function:           idlist_del
 * --------------------------------------------------*/
bool idlist_del(idlist_t *list, id_t id){
    return list_set(list, id, 0);
}