Esempio n. 1
0
/**
 * Sort an array so that those elements that exist in the ordering are
 * first in the array, and in the same order as the ordering. The algorithm
 * is O(count * ocount) and designed for small arrays.
 *
 * TODO: Move this to common / lib?
 *
 * @param dest		Array with elements to sort, also destination array
 * @param count		Number of elements to sort
 * @param order		Array containing ordering elements
 * @param ocount	Number of ordering elements
 * @return number of elements in dest that are in order (these will be at the
 *	start of dest).
 */
static int sort_array_by_ordering(int *dest, int count, int *order,
				   int ocount)
{
	int temp[count];
	int dest_count;
	int same;	/* number of elements which are the same */
	int i;

	/* setup output items, copy items to be sorted into our temp area */
	memcpy(temp, dest, count * sizeof(*dest));
	dest_count = 0;

	/* work through the ordering, move over the elements we agree on */
	for (i = 0; i < ocount; i++) {
		if (array_search(temp, count, order[i]) != -1)
			dest[dest_count++] = order[i];
	}
	same = dest_count;

	/* now move over the elements that are not in the ordering */
	for (i = 0; i < count; i++) {
		if (array_search(order, ocount, temp[i]) == -1)
			dest[dest_count++] = temp[i];
	}
	assert(dest_count == count);
	return same;
}
Esempio n. 2
0
int do_mq_createmqgroup(){
	printf("do_mq_createmqgroup: beginning of method\n");
	mqgroup_t *group_attr = (mqgroup_t *)malloc(sizeof(mqgroup_t));
	group_attr->grouptype = m_in.m1_i1;

	char *senders = (char *)malloc(sizeof(char)*m_in.m1_i2*5);
	char *receivers = (char *)malloc(sizeof(char)*m_in.m1_i2*5);

	sys_datacopy(who_e, (vir_bytes)m_in.m1_p1, SELF, (vir_bytes)senders, m_in.m1_i2*5);
	sys_datacopy(who_e, (vir_bytes)m_in.m1_p2, SELF, (vir_bytes)receivers, m_in.m1_i2*5);

	uid_t my_uid = mproc[who_p].mp_realuid;
	
	group_attr->creator = my_uid;
	printf("do_mq_createmqgroup: before malloc sending and receiving...\n");
	group_attr->sendingusers = (int *)malloc(sizeof(int)*MAX_ADMINS);
	group_attr->receivingusers = (int *)malloc(sizeof(int)*MAX_ADMINS);

	group_attr->sendingusers[0] = atoi(strtok(senders,","));
	int i;
	for(i = 1; i<m_in.m1_i2; i++) {
		group_attr->sendingusers[i] = atoi(strtok(NULL,","));
	}	

	group_attr->receivingusers[0] = atoi(strtok(receivers,","));
	for(i = 1; i<m_in.m1_i2; i++) {
		group_attr->receivingusers[i] = atoi(strtok(NULL,","));
	}

	printf("do_mq_createmqgroup: after parsing senders and receivers, before array search admin\n");

	if(array_search(admin_ids, MAX_ADMINS, my_uid) >= 0) {
		// do stuff
		printf("do_mq_createmqgroup: after array search admin, before searching empty_spot\n");
		int empty_spot = array_search(groups_ids, MAX_ADMINS, 0);

		if (empty_spot < 0) {
			printf("Error: max group number reached\n");
			return FAIL;
		}
		printf("do_mq_createmqgroup: after searching empty_spot(%d), before using it in global arrays\n",empty_spot);

		groups_ids[empty_spot] = group_id_counter++;
		groups_attr[empty_spot] = group_attr;

		return group_id_counter - 1;

	} else {
		// not enough privileges
		return FALSE;
	}

}
Esempio n. 3
0
string_t
get_word_stem(array_t** stack, lang_t* lang, const string_t word, bool_t is_core)
{
    string_t  changed, copy, *r;
    array_t*  a;

    if(SMRZR_TRUE == is_core) {
        changed = word;
    } else {
        if(NULL == (changed = get_word_core(stack, lang, word)))
            return(NULL);
    }

    if(isupper(changed[0]) && strlen(changed) > 1) return(changed);

    copy = array_push_alloc(stack, strlen(changed)+1);
    strcpy(copy, changed);

    a = lang->manual;

    if(NULL!=(r=(string_t*)array_search(a, changed, comp_string_with_rule))){
        replace_word(stack, changed, *r);
    }

    a = lang->pre;

    for(r = (string_t*)ARR_FIRST(a); !ARR_END(a); r = (string_t*)ARR_NEXT(a)) {
        if(SMRZR_TRUE == replace_word_head(changed, *r)) break;
    }

    a = lang->post;

    for(r = (string_t*)ARR_FIRST(a); !ARR_END(a); r = (string_t*)ARR_NEXT(a)) {
        if(SMRZR_TRUE == replace_word_tail(changed, *r)) break;
    }

    a = lang->synonyms;

    if(NULL!=(r=(string_t*)array_search(a, changed, comp_string_with_rule))){
        replace_word(stack, changed, *r);
    }

    /* quality check */
    if(strlen(changed) < 3) {
        memmove(changed, copy, strlen(copy)+1);
        (*stack)->curr = PTR_ADD(elem_t, changed, strlen(changed)+1);
    } else {
        array_pop_free(*stack, copy);
    }

    return(changed);
}
Esempio n. 4
0
File: array.c Progetto: dtaht/mosh
void *
array_del(array_t array, void *element, cmp_fun_t cmp_fun)
{
    void **cell;
    void *result;
    int i;

    if (!array) goto not_found;
    i = array_search(array, element, cmp_fun);
    if (i < 0) goto not_found;

    cell = array->cell;
    result = cell[i];
    array->num_elements --;
    if (i < array->num_elements)
        memmove(&cell[i], &cell[i + 1],
                (array->num_elements - i) * sizeof(void *));
    if (array->num_elements < array->size / 4 && array->size > MIN_SIZE) {
        array_t tmp;
        tmp = realloc(array, RESIZE(array, array->size / 2));
        assert(tmp && tmp == array);
        assert(array->size >= MIN_SIZE);
    }
    return result;
 not_found:
    errno = EEXIST;
    return NULL;
}
Esempio n. 5
0
int do_mq_deletemqgroup(){
	uid_t my_uid = mproc[who_p].mp_realuid;
	int group_id = m_in.m1_i1;

	int group_index = array_search(groups_ids, MAX_ADMINS, group_id);
	
	if (group_index < 0) {
		printf("Error, groupd not found!\n");
		return FAIL;
	}
	
	mqgroup_t *group_attr = groups_attr[group_index];
	// or root user
	if(my_uid == group_attr->creator || my_uid == 0) {
		// do stuff

		printf("Closing queue/s of group...\n");
		close_mq_from_group(group_id);

		printf("Deleting group %d...\n", group_id);
		groups_ids[group_index] = 0;
		free(groups_attr[group_index]);

		return TRUE;

	} else {
		// not privileged
		printf("do_mq_deletemqgroup: Error, not privileged user\n");
		return FALSE;
	}
}
Esempio n. 6
0
void notify_rec(int *receivers)
{
	printf("inside notify_rec..., argument receivers[0]=%d\n",receivers[0]);
	int i, status;
	for (i = 0; i < MAX_REQ_NOT; i++)
	{
		if (req_receivers[i].pid > 0)
		{
			printf("inside notify_rec... at least 1 pid (%d) requested notify\n", req_receivers[i].pid);

			status = array_search(receivers, MAX_PROCESSES, req_receivers[i].pid);
			if (status != FAIL)
			{
				printf("inside notify_rec... found receiver %d in array_search, executing kill\n",req_receivers[i].pid);

				int kill_status = sys_kill(req_receivers[i].proc_nr, req_receivers[i].signum);
				if ( kill_status < 0)
				{
					printf("Error %d: some proccess (%d) didn't receive the notification (%d)\n", kill_status, req_receivers[i].proc_nr, req_receivers[i].signum);
				}	

			}
		}
	}	
}
Esempio n. 7
0
File: arrays.c Progetto: Vuzi/vuziks
return_code arrays_array_pop(Object* o, Linked_list *args, Variable* eval_value, int as_constructor) {

    (void)args;

    if(!o) {
        err_add(E_ERROR, OP_IMPOSSIBLE, "Can't use this built-in function (array.pop) out of its object");
        return RC_ERROR;
    }

    Array* a = (Array*) o->data;

    if(as_constructor) {
        err_add(E_ERROR, OP_IMPOSSIBLE, "Can't use this built-in function (array.pop) as a constructor");
        return RC_ERROR;
    }

    // Pop avec arguments
    if(args) {
        Variable *key = (Variable*)args->value;

        if(key->type == T_NUM) {
            int val = (int)key->value.v_num;
            int i = array_search(a, val);

            if( i >= 0) {
                // On récupère
                eval_value->type = a->tab_value[i].type;
                eval_value->value = a->tab_value[i].value;

                // On recopie
                for(; i < (int)a->nb_cases + 1; i++) {
                    KEY(a, i) = KEY(a, i+1);
                    VAL(a, i) = VAL(a, i+1);
                }

                a->nb_cases--;
            } else {
                eval_value->type = T_NONEXISTENT;
            }

        } else {
            err_add(E_WARNING, OP_IMPOSSIBLE, "arrays' key can only be numbers");
            return RC_WARNING;
        }
    }
    // Sans argument
    else {
        if(a->nb_cases > 0) {
            eval_value->type = a->tab_value[a->nb_cases - 1].type;
            eval_value->value = a->tab_value[a->nb_cases - 1].value;
            a->nb_cases--;
        } else {
            eval_value->type = T_NONEXISTENT;
        }
    }

    return RC_OK;
}
Esempio n. 8
0
int do_mq_removeadminuser(){
	uid_t my_uid = mproc[who_p].mp_realuid;
	uid_t de_admin_id = m_in.m1_i1;

	if(my_uid == 0) {
		// remove
		int target_element = array_search(admin_ids, MAX_ADMINS, de_admin_id);
		if (target_element < 0) {
			return FAIL;
		} else {
			admin_ids[target_element] = 0;
			return TRUE;	
		}
	} else {
		// not root, exit
		printf("remove-admin: error, user not authorized (should be root)\n");
		return FALSE;
	}
}
Esempio n. 9
0
int do_mq_addadminuser(){
	uid_t my_uid = mproc[who_p].mp_realuid;
	uid_t new_admin_id = m_in.m1_i1;

	if(my_uid == 0) {
		//add
		int free_element;
			if (free_element = array_search(admin_ids, MAX_ADMINS, 0) < 0) {
				printf("add-admin: exceeded maximum number of admins (%d)\n", MAX_ADMINS);
				return FAIL;
			} 

		admin_ids[free_element] = new_admin_id;
		return TRUE;

	} else {
		
		printf("add-admin: error, user not authorized (should be root)\n");
		return FAIL;
	}
}
Esempio n. 10
0
void
array_remove(array_t* a, const elem_t key, compfunc_t cf)
{
    elem_t  elem, from;

    elem = array_search(a, key, cf);

    if(NULL == elem) return;

    from = PTR_ADD(elem_t, elem, a->elem_sz);

    assert(PTR_DIFF(a->curr, from) >= 0);

    if(from != a->curr) {
        memmove(elem, from, PTR_DIFF(a->curr, from));
    }

    a->curr = PTR_ADD(elem_t, a->curr, -(a->elem_sz));

    return;
}
Esempio n. 11
0
File: array.c Progetto: dtaht/mosh
int
array_add(array_t *array, void *element, cmp_fun_t cmp_fun)
{
    array_t arr = *array;
    void **cell;
    int i;

    if (UNLIKELY(!arr)) {
        arr = malloc(RESIZE(arr, MIN_SIZE));
        if (!arr) return -1;
        arr->size = MIN_SIZE;
        arr->num_elements = 0;
        *array = arr;
    } else if (arr->size <= arr->num_elements) {
        array_t tmp;
        int size = arr ? 2 * arr->size : MIN_SIZE;
        tmp = realloc(arr, RESIZE(arr, size));
        if (!tmp) return -1;
        *array = tmp;
        arr = tmp;
        tmp->size = size;
    }

    i = array_search(arr, element, cmp_fun);
    if (i >= 0) {
        errno = EEXIST;
        return -1;
    }

    i = -i - 1;
    cell = arr->cell;
    assert(0 <= i && i <= arr->num_elements && i < arr->size);
    if (i < arr->num_elements)
        memmove(&cell[i + 1], &cell[i],
                (arr->num_elements - i) * sizeof(void *));
    cell[i] = element;
    arr->num_elements++;
    return 0;
}
Esempio n. 12
0
void test1(void)
{
	static char * argv[] = 
	{
		"./test1",
		"-DHELLO=\"world\"",
		NULL
	};
	static libconf_opt_t options[] = {
		{ "define", 'D', TP_YES, PT_STRING_LIST, "define", TP_YES, PT_STRING_LIST, "define a macro", DOE_WARNING },
		{ NULL, 0, TP_NO, PT_NONE, NULL, TP_NO, PT_NONE, NULL, DOE_ERROR }
	};
	int argc = 2;
	int rc = 0;
	libconf_t * handle = NULL;
	libconf_opt_t ** t_options = libconf_optconst(options);
	array_t * array = NULL;
	struct test1_helper1_data data;
	
	handle = libconf_init(SRCDIR"/global_conf_test1", NULL, t_options, NULL, argc, argv);
	free(t_options);
	if (!rc) rc = libconf_phase1(handle);
	if (!rc) rc = libconf_phase2(handle);
	if (!rc) rc = libconf_phase3(handle);
	if (!rc) rc = libconf_phase4(handle);
	if (!rc) rc = libconf_phase5(handle);
	assert(!rc);
	if (!rc) rc = libconf_getopt(handle, "define", &array);
	if (!rc)
	{
		data.flag = 0;
		array_foreach(array, test1_helper1, &data);
		assert(!data.flag);
		array_search(array, "HELLO", test1_helper2);
	}
	
	libconf_fini(handle);
}
Esempio n. 13
0
int do_mq_send()
{
	mqd_t mqd = m_in.m7_i1;

	if (mqd >= curr_num_queues || mqd < 0) {
		printf("Wrong mq identifier (mqd)\n");
		return FAIL;
	} else if (queues_mask[mqd] == 0) {
		printf("Error: queue does not exist\n");
		return FAIL;
	}

	message_t *data = malloc(sizeof(message_t));
	size_t message_length = m_in.m7_i2;
	unsigned int priority = m_in.m7_i3;

	uid_t my_uid = mproc[who_p].mp_realuid;
	int mq_grp = queues[mqd].attr->grp_id;

	int grp_index = array_search(groups_ids, MAX_ADMINS, mq_grp);

	printf("Sending to queue[%d]\n", mqd);

	if (grp_index < 0) {
		printf("Error: queue Group not found in the group list\n");
		return FAIL;
	}

	int grp_policy = groups_attr[grp_index]->grouptype;

	int uid_index = array_search(groups_attr[grp_index]->sendingusers, MAX_ADMINS, my_uid);

	/*              public             ||             secure               */
	if ((grp_policy && uid_index >= 0) || (grp_policy == 0 && uid_index < 0)) {
		if (my_uid != groups_attr[grp_index]->creator || my_uid != 0) { 
			printf("Error: user not allowed to send in the queue\n");
			return FALSE;
		}
	} 

	if (message_length > queues[mqd].attr->max_message_size)
	{
		printf("The length of the message is longer than we accept.\n");
		return FAIL;
	}

	char *pid_list = (char *)malloc(sizeof(char)*128);
	data->receiver_pids = malloc(sizeof(int)*m_in.m7_i4);
	data->data = malloc(sizeof(char)*message_length);
	
	sys_datacopy(who_e, (vir_bytes)m_in.m7_p1, SELF,(vir_bytes)pid_list, sizeof(char)*128);
	sys_datacopy(who_e, (vir_bytes)m_in.m7_p2, SELF,(vir_bytes)data->data, sizeof(char)*message_length);

	data->sender_pid = atoi(strtok(pid_list,","));
	printf("do_mq_send: parsing sender_pid = %d\n", data->sender_pid);

	int i;
	for (i = 0; i < m_in.m7_i4; i++)
	{
		data->receiver_pids[i] = atoi(strtok(NULL,","));
		printf("do_mq_send: before enqueue, writing data->receiver_pid[%d]=%d\n", i, data->receiver_pids[i]);
	}

	int success;
	switch (priority)
	{
		case 1:
			if (queues[mqd].queue_high->count == queues[mqd].queue_high->size)
			{
				printf("The queue is already full.\n");
				return FAIL;	// queue is full
			}
			success = enqueue(queues[mqd].queue_high, data);
			break;
		case 2:
			if (queues[mqd].queue_norm->count == queues[mqd].queue_norm->size)
			{
				printf("The queue is already full.\n");
				return FAIL;	// queue is full
			}
			success = enqueue(queues[mqd].queue_norm, data);
			break;
		case 3:
			if (queues[mqd].queue_low->count == queues[mqd].queue_low->size)
			{
				printf("The queue is already full.\n");
				return FAIL;	// queue is full
			}
			success = enqueue(queues[mqd].queue_low, data);
			break;
		default:
			return FAIL;
			break;
	}
	if (success == TRUE)
	{
		queues[mqd].curr_num_messages_total++;
		printf("do_mq_send: before notify_rec, data->receiver_pids[0]=%d\n", data->receiver_pids[0]);
		notify_rec(data->receiver_pids);
	}

	printf("do_mq_send: success %d\n", success);
	return success;
	
}
Esempio n. 14
0
static void assert_ints_null(array *a, int *ints, int count) {
	for (int i = 0; i < count; i++) {
		int *res = array_search(a, &ints[i]);
		TEST_ASSERT_NULL(res);               
	}
}
Esempio n. 15
0
status_t
parse_article(const char* file_name, lang_t* lang, article_t* article)
{
    string_t    word, word_core, word_stem;
    sentence_t* sentence;
    word_t*     word_entry;
    stream_t*   stream = &article->stream;
    bool_t      is_new, is_para_end = SMRZR_FALSE;

    PROF_START;

    if(SMRZR_OK != stream_create(file_name, stream))
        ERROR_RET;

    while(!STREAM_END(stream)) {

        STREAM_FIND_WORD(stream);

        if(STREAM_END(stream)) break;

        sentence = sentence_new(&article->sentences, article->stream.curr);
        assert(NULL != sentence);

        if(SMRZR_TRUE == is_para_end) {
            sentence->is_para_begin = SMRZR_TRUE;
            is_para_end = SMRZR_FALSE;
        }

        while(!STREAM_END(stream)) {

            STREAM_GET_WORD(stream, word, is_para_end);
            
            sentence->num_words++;

            if(NULL == (word_core = get_word_core(&article->stack, lang, word)))
                ERROR_RET;

            if(NULL == array_search(lang->exclude, word_core, comp_strings)) {

                if(NULL == (word_stem = get_word_stem(&article->stack, lang,
                                                      word_core, SMRZR_TRUE)))
                    ERROR_RET;

                if(NULL == (word_entry = array_search_or_alloc(&article->words,
                                        word_stem, comp_word_by_stem, &is_new)))
                    ERROR_RET;

                if(SMRZR_TRUE == is_new) {
                    word_entry->num_occ = 1;
                    word_entry->stem = word_stem;
                } else {
                    ++(word_entry->num_occ);
                    array_pop_free(article->stack, word_stem);
                }
            } else {
                array_pop_free(article->stack, word_core);
            }

            if(end_of_line(lang, word)) {
                sentence->end = word + strlen(word);
                article->num_words += sentence->num_words;
                break;
            }
        }
    }

    PROF_END("article parsing");

    /*fprintf(stdout, "Number of sentences - %lu\n", ARR_SZ(article->sentences));
    fprintf(stdout, "Number of words - %lu\n", ARR_SZ(article->words));*/

    return(SMRZR_OK);
}
Esempio n. 16
0
int mq_close_helper(mqd_t mqd) {

	if ( mqd < 0 || queues_mask[mqd] == 0) {
		printf("Error: queue does not exist\n");
		return FALSE;
	}
	uid_t my_uid = mproc[who_p].mp_realuid;
	int mq_grp = queues[mqd].attr->grp_id;

	printf("mq_close_helper: mq_grp=%d, mqd=%d\n", mq_grp, mqd);

	int grp_index = array_search(groups_ids, MAX_ADMINS, mq_grp);

	if (grp_index < 0) {
		printf("Fatal Error: queue Group not found in the group list\n");
		int j;
		for (j=0; j<MAX_ADMINS; j++) {
			printf("groups_ids[%d]=%d\t",j,groups_ids[j]);
		}
		return FAIL;
	}

	int grp_policy = groups_attr[grp_index]->grouptype;

	int uid_index = array_search(groups_attr[grp_index]->sendingusers, MAX_ADMINS, my_uid);

	if(uid_index < 0) {
		uid_index = array_search(groups_attr[grp_index]->receivingusers, MAX_ADMINS, my_uid);
	}

	/*              public                  ||             secure               */
	if ((grp_policy != 0 && uid_index >= 0) || (grp_policy == 0 && uid_index < 0)) { 
		if (my_uid != groups_attr[grp_index]->creator || my_uid != 0) { 
			printf("Error: user not allowed to close the queue\n");
			return FALSE;
		}
	} 

	int success = deleteproc(queues[mqd].sender_pids, mproc[who_p].mp_pid);
	int success_sender = emptyprocs(queues[mqd].sender_pids);
	success = deleteproc(queues[mqd].receiver_pids, mproc[who_p].mp_pid);
	int success_receiver = emptyprocs(queues[mqd].receiver_pids);

	if (success_sender == TRUE && success_receiver == TRUE)
	{
		free(queues[mqd].attr);
		free(queues[mqd].sender_pids);
		free(queues[mqd].receiver_pids);
		free(queues[mqd].queue_high->messages);
		free(queues[mqd].queue_norm->messages);
		free(queues[mqd].queue_low->messages);
		free(queues[mqd].queue_high);
		free(queues[mqd].queue_norm);
		free(queues[mqd].queue_low);
		free(&queues[mqd]);
	}

	queues_mask[mqd] = 0;

	printf("Queue [%d] closed succesfully\n", mqd);

	return TRUE;
}
Esempio n. 17
0
status_t
grade_article(article_t* article, lang_t* lang, float ratio)
{
    array_t     * a, * temp;
    word_t      * w;
    sentence_t  * s, * s_score;
    size_t        top_occs[] = { 0, 0, 0, 0}, occs, i, max_words;
    word_t      * top_words[] = { 0, 0, 0, 0};
    string_t      ws, ws_stem;
    bool_t        is_first = SMRZR_TRUE;

    PROF_START;

    /* find top occs and corresponding words */
    a = article->words;

    for(w = (word_t*)ARR_FIRST(a); !ARR_END(a); w = (word_t*)ARR_NEXT(a)) {
        for(occs = 0; occs < TOP_OCCS_MAX; ++occs) {
            if(top_occs[occs] < w->num_occ) {
                for(i = TOP_OCCS_MAX-1; i > occs; --i) {
                    top_occs[i] = top_occs[i-1];
                    top_words[i] = top_words[i-1];
                }
                top_occs[occs] = w->num_occ;
                top_words[occs] = w;
                break;
            }
        }
    }

    /*for(occs = 0; occs < TOP_OCCS_MAX; ++occs) {
        fprintf(stdout, "top occ %lu - %lu [%s]\n", occs, top_occs[occs],
               top_words[occs]->stem);
    }*/

    /* score all sentences */
    a = article->sentences;

    for(s=(sentence_t*)ARR_FIRST(a); !ARR_END(a); s=(sentence_t*)ARR_NEXT(a)) {

        ws = s->begin;

        while(ws < s->end) {

            while(0 == *ws && ws < s->end) ++ws;

            if(ws >= s->end) break;

            if(NULL == (ws_stem = get_word_stem(&article->stack, lang, ws,
                                                SMRZR_FALSE)))
                ERROR_RET;

            if(NULL == (w = (word_t*)array_search(article->words, ws_stem,
                                                  comp_word_by_stem)))
            { /* possibly a word excluded */
                ws = ws + strlen(ws);
                continue;
            }

            occs = 0;
            while(top_occs[occs] != w->num_occ && occs < TOP_OCCS_MAX) ++occs;

            switch(occ2score[occs]) {
                case 3: /* score += occ * 3 */
                    s->score += ((w->num_occ << 1) + w->num_occ); break;
                case 2: /* score += occ * 2 */
                    s->score += (w->num_occ << 1); break;
                case 1: /* score += occ */
                    s->score += w->num_occ; break;
                default: 
                    ERROR_RET;
            }

            ws = ws + strlen(ws);
        }

        if(SMRZR_TRUE == s->is_para_begin) {
            s->score *= 1.6;
        } else if(SMRZR_TRUE == is_first) {
            s->score = (s->score << 1); /* super-boost 1st line */
            is_first = SMRZR_FALSE;
        }

        /*fprintf(stdout, "%u ", s->score);*/
    }

    /*fprintf(stdout, "\n");*/

    /* sort on sentence score */
    if(NULL == (temp = array_new(SMRZR_TRUE, sizeof(sentence_t),
                                 ARR_SZ(article->sentences), NULL)))
        ERROR_RET;

    for(s=(sentence_t*)ARR_FIRST(a); !ARR_END(a); s=(sentence_t*)ARR_NEXT(a)) {
        if(NULL == (s_score = array_sorted_alloc(&temp, (elem_t)(size_t)(s->score),
                                                 comp_sentence_by_score)))
            ERROR_RET;

        memcpy(s_score, s, sizeof(sentence_t));
        s_score->cookie = (elem_t)s;
    }

    /* pick sentences with highest scores until we get required ratio of words*/
    max_words = article->num_words * ratio;

    a = temp;
    for(s=(sentence_t*)ARR_FIRST(a); !ARR_END(a) && (ssize_t)max_words > 0;
                                     s=(sentence_t*)ARR_NEXT(a))
    {
        ((sentence_t*)s->cookie)->is_selected = SMRZR_TRUE;
        max_words -= s->num_words;
        /*fprintf(stdout, "Selected sentence: score %u, %lu words, %ld
          remaining\n", s->score, s->num_words, (ssize_t)max_words);*/
    }

    array_free(temp);

    PROF_END("article grading");

    return(SMRZR_OK);
}
Esempio n. 18
0
mqd_t do_mq_open()
{
	size_t name_size = sizeof(char) * MAX_NAME_SIZE;
	char *name = (char *)malloc(name_size);
	sys_datacopy(who_e, (vir_bytes)m_in.m7_p1, SELF, (vir_bytes)name, name_size);
	int open_flag = m_in.m7_i1;
	int blocking_flag = m_in.m7_i2;
	int max_mess = m_in.m7_i3;
	int grp_id = m_in.m7_i4;
	uid_t my_uid = mproc[who_p].mp_realuid;

	int grp_index = array_search(groups_ids, MAX_ADMINS, grp_id);

	if (grp_index < 0) {
		printf("Error: queue Group not found in the group list\n");
		return FAIL;
	}

	int grp_policy = groups_attr[grp_index]->grouptype;

	int uid_index = array_search(groups_attr[grp_index]->sendingusers, MAX_ADMINS, my_uid);

	if(uid_index < 0) {
		uid_index = array_search(groups_attr[grp_index]->receivingusers, MAX_ADMINS, my_uid);
	}

	/*              public                  ||             secure               */
	if ((grp_policy != 0 && uid_index >= 0) || (grp_policy == 0 && uid_index < 0)) { 
		if (my_uid != groups_attr[grp_index]->creator || my_uid != 0) { 
			printf("Error: user not allowed to open the queue\n");
			return FAIL;
		}
	} 

	// printf("do_mq_open: printing of variables\n");
	// printf("      name           %s\n", name);
	// printf("      open_flag      %d\n", open_flag);
	// printf("      blocking_flag  %d\n", blocking_flag);
	// printf("      max_mess       %d\n", max_mess);

	if (curr_num_queues == MAX_QUEUES)
	{
		printf("Maximum number of queues have already been opened.\n");
		return FAIL;
	}

	pid_t calling_proc = mproc[who_p].mp_pid;

	if (calling_proc < 1)
	{
		printf("Unable to acquire the calling process's PID.\n");
		return FAIL;
	}

	if (open_flag != O_RDONLY && open_flag != O_WRONLY && open_flag != O_RDWR)
	{
		printf("The open flag must either be O_RDONLY, O_WRONLY, or O_RDWR.\n");
		return FAIL;
	}


	// printf("do_mq_open: name of queue: %s\n", name);
	int i;
	for (i = 0; i < curr_num_queues; i++)
	{
		if (strcmp(queues[i].attr->name, name) == 0)
		{
			if (open_flag == O_RDONLY)
			{
				int success = addproc(queues[i].receiver_pids, calling_proc);
				if (success == FAIL)
					return FAIL;
			}
			else if (open_flag == O_WRONLY)
			{
				int success = addproc(queues[i].sender_pids, calling_proc);
				if (success == FAIL)
					return FAIL;
			}
			else
			{
				int success = addproc(queues[i].sender_pids, calling_proc);
				if (success == FAIL)
					return FAIL;
				success = addproc(queues[i].receiver_pids, calling_proc);
				if (success == FAIL)
				{
					deleteproc(queues[i].sender_pids, calling_proc);
					return FAIL;
				}
			}
			return i;
		}
	}

	queues_mask[curr_num_queues++] = 1;

	// printf("do_mq_open: curr_num_queues %d\n", curr_num_req);

	mq_t *new_queue = malloc(sizeof(mq_t));
	new_queue->attr = malloc(sizeof(mq_attr_t));
	new_queue->attr->name = (char *)name;
	new_queue->attr->send_blocking = blocking_flag;
	new_queue->attr->receive_blocking = blocking_flag;
	new_queue->attr->max_message_size = MAX_MESSAGE_SIZE;
	new_queue->attr->grp_id = grp_id;

	printf("do_mq_open: new_queue->attr->grp_id = %d\n", new_queue->attr->grp_id);

	if (max_mess)
	{
		new_queue->attr->max_messages = max_mess;
		
	}
	else
	{
		new_queue->attr->max_messages = MAX_MESSAGES;
	}

	new_queue->curr_num_messages_total = 0;
	new_queue->sender_pids = malloc(sizeof(SIZE_INT * MAX_PROCESSES));
	new_queue->receiver_pids = malloc(sizeof(SIZE_INT * MAX_PROCESSES));
	new_queue->queue_high = malloc(sizeof(queue_t));
	new_queue->queue_norm = malloc(sizeof(queue_t));
	new_queue->queue_low = malloc(sizeof(queue_t));

	int num_messages = new_queue->attr->max_messages;
	int size_messages = new_queue->attr->max_message_size;

	// TODO: go back and fix this size of
	new_queue->queue_high->messages = malloc(sizeof(message_t) * num_messages * size_messages);
	new_queue->queue_norm->messages = malloc(sizeof(message_t) * num_messages * size_messages);
	new_queue->queue_low->messages = malloc(sizeof(message_t) * num_messages * size_messages);

	init_queue(new_queue->queue_high, num_messages);
	init_queue(new_queue->queue_norm, num_messages);
	init_queue(new_queue->queue_low, num_messages);

	queues[curr_num_queues - 1] = *new_queue;

	return curr_num_queues - 1;
	
}
Esempio n. 19
0
int do_mq_receive()
{
	printf("do_mq_receive: hello! before getting any variable...\n");
	mqd_t mqd = m_in.m1_i1;
	size_t buffer_length = (size_t)m_in.m1_i2;
	unsigned int priority = (unsigned int)m_in.m1_i3;

	if (mqd < 0 || queues_mask[mqd] == 0) {
		printf("Error: queue does not exist\n");
		return FAIL;
	}

	uid_t my_uid = mproc[who_p].mp_realuid;
	int mq_grp = queues[mqd].attr->grp_id;

	printf("do_mq_receive: searchging for group %d...\n", mq_grp);

	int grp_index = array_search(groups_ids, MAX_ADMINS, mq_grp);

	if (grp_index < 0) {
		printf("Error: queue Group not found in the group list\n");
		return FAIL;
	}

	printf("do_mq_receive: fetching from groups_attr[%d]\n", grp_index);
	int grp_policy = groups_attr[grp_index]->grouptype;

	int uid_index = array_search(groups_attr[grp_index]->receivingusers, MAX_ADMINS, my_uid);

	/*              public             ||             secure               */
	if ((grp_policy && uid_index >= 0) || (grp_policy == 0 && uid_index < 0)) { 
		if (my_uid != groups_attr[grp_index]->creator || my_uid != 0) { 
			printf("Error: user not allowed to receive from this queue\n");
			return FALSE;
		}
	} 

	char *buffer_ptr = (char *)malloc(sizeof(char) * buffer_length);
	
	printf("do_mq_receive: after malloc before checking size...\n");

	if (buffer_length < queues[mqd].attr->max_message_size)
	{
		printf("The length of the buffer is not long enough for the message.\n");
		return FAIL;	// not a big enough buffer
	}
	
	printf("do_mq_receive: after buffer_length, before dequeueing...\n");

	int success;
	switch (priority)
	{
		case 1:
			if (queues[mqd].queue_high->count == 0)
			{
				printf("The queue is empty.\n");
				return FALSE;	// no messages
			}
			success = dequeue(queues[mqd].queue_high, &buffer_ptr);
			break;
		case 2:
			if (queues[mqd].queue_norm->count == 0)
			{
				printf("The queue is empty.\n");
				return FALSE;	// no messages
			}
			success = dequeue(queues[mqd].queue_norm, &buffer_ptr);
			break;
		case 3:
			if (queues[mqd].queue_low->count == 0)
			{
				printf("The queue is empty.\n");
				return FALSE;	// no messages
			}
			success = dequeue(queues[mqd].queue_low, &buffer_ptr);
			break;
		default:
			return FALSE;
			break;
	}
	if (success == TRUE)
	{
		queues[mqd].curr_num_messages_total--;
	}


	printf("do_mq_receive: dequeue of message: %s\n", buffer_ptr);

	sys_datacopy(SELF, (vir_bytes)buffer_ptr,
		who_e, (vir_bytes)m_in.m1_p1, queues[mqd].attr->max_message_size);

	return TRUE;
}