Example #1
0
void BaseType::setType(unsigned type_id,unsigned offset,unsigned count)
{
	//Raises an error if the type count is invalid
	if(count==0 || count > this->types_count)
		throw Exception(ERR_OBT_TYPES_INV_QUANTITY,__PRETTY_FUNCTION__,__FILE__,__LINE__);
	//Raises an error if the type id is invalid
	else if(!isTypeValid(type_id,offset,count))
		throw Exception(ERR_ASG_INV_TYPE_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
	else
		type_idx=type_id;
}
Example #2
0
ListEntry* Parser(char *tok)
{
    ListEntry *Entry;
    int num_fields = 0;
    int desc_len;
    int value_1;
    int value_2;
    char *copy, *ptr;

    Entry = malloc(sizeof(ListEntry));
    if(!Entry){
        fprintf(stderr, "Error in allocating memory: %s\n", strerror(errno));
        return 0;
    }
    memset(Entry, 0, sizeof(ListEntry));

    while (tok != NULL)
    {
        if(num_fields > 3) {
            fprintf(stderr, "Too many fields\n");
            free(Entry);
            return NULL;
        }

        if(num_fields == 0) {
            if(!isTypeValid(tok)){
                free(Entry);
                return NULL;
            }
            strcpy(Entry->trans_type, tok);
        }

        if(num_fields == 1){
            if(!isTimestampValid(tok)){
                free(Entry);
                return NULL;
            }
            Entry->timestamp = strtoul(tok, NULL, 10);
        }

        if(num_fields == 2){
            if(!isAmountValid(tok)){
                free(Entry);
                return NULL;
            }else {
                /* value = strtod(tok, NULL); */
                sscanf(tok, "%d.%d", &value_1, &value_2);
                value_1 = 100*value_1 + value_2;
                if(value_1 >= 1000000000){
                    fprintf(stderr, "Amount is greater than 10 million %s\n", tok);
                    free(Entry);
                    return NULL;
                }
                Entry->trans_amt = value_1;
            }
        }

        if(num_fields == 3){
            desc_len = strlen(tok);
            if(!desc_len){
                free(Entry);
                fprintf(stderr, "Entry has no description\n");
                return NULL;
            }
            copy = malloc(desc_len * sizeof(char) + 1);
            if(!copy){
                fprintf(stderr, "Error in allocating memory: %s\n", strerror(errno));
                free(Entry);
                return NULL;
            }
            memset(copy, 0, (desc_len * sizeof(char) + 1));
            strncpy(copy, tok, (desc_len +1));
            ptr = copy;
            while(*copy == ' ') {
                copy++;
            }
            if(copy && *copy == '\n'){
                fprintf(stderr, "Entry has with timestamp %ld has no description\n", Entry->timestamp); 
                free(Entry);
                free(ptr);
                return NULL;
            }
            desc_len = strlen(copy);
            if(!desc_len) {
                free(ptr);
                free(Entry);
                return NULL;
            }
            Entry->trans_des = malloc(desc_len * sizeof(char) + 1);
            if(!Entry->trans_des){
                fprintf(stderr, "Error in allocating memory: %s\n", strerror(errno));
                free(Entry);
                free(ptr);
                return NULL;
            }
            memset(Entry->trans_des, 0, (desc_len * sizeof(char) + 1));
            strncpy(Entry->trans_des, copy, (desc_len +1));
            if(Entry->trans_des[desc_len-1] == '\n'){
                Entry->trans_des[desc_len-1] = '\0';
            }
            free(ptr);

        }
        num_fields++;
        tok = strtok(NULL, "\t");
    }
    return Entry;
}