jcov_file.c

来自「This is a resource based on j2me embedde」· C语言 代码 · 共 525 行 · 第 1/2 页

C
525
字号
    char           last_modifiers[256];    char           ch;    int            state = 6;    JVMPI_Method   *m = NULL;    jcov_class_t   *found_class = NULL;    jcov_method_t  *found_method = NULL;    merge_action_t merge_action = MERGE;    int ct_size;    last_class.name              = (char*)jcov_calloc(MAX_PATH_LEN);    last_class.src_name          = (char*)jcov_calloc(MAX_PATH_LEN);    last_method.method_name      = (char*)jcov_calloc(MAX_PATH_LEN);    last_method.method_signature = (char*)jcov_calloc(MAX_PATH_LEN);    while (read_line()) {        get_next_token(DELIM);        if (!token || strlen(token) == 0) {            continue;        }        if (EQUALS(token, KEYWORD_CLASS)) {            CHK_STATE(6, 1);            get_next_token(DELIM);            CHK_TOKEN;            strcpy(last_class.name, token);            get_next_token("[]");            if (token == NULL) {                last_modifiers[0] = '\0';            } else {                strcpy(last_modifiers, token);            }        } else if (EQUALS(token, KEYWORD_SRCFILE)) {            CHK_STATE(1, 2);            get_next_token(DELIM);            if (token == NULL) {                tmp = dummy_src_name(last_class.name);                strcpy(last_class.src_name, tmp);                jcov_free(tmp);            } else {                strcpy(last_class.src_name, token);            }        } else if (EQUALS(token, KEYWORD_TIMESTAMP)) {            CHK_STATE(2, 3);            get_next_token(DELIM);            CHK_TOKEN;            last_class.timestamp = strdup(token);        } else if (EQUALS(token, KEYWORD_DATA)) {            CHK_STATE(3, 4);            get_next_token(DELIM);            CHK_TOKEN;            ch = token[0];            if (strlen(token) > 1 ||                 (ch != JCOV_DATA_B && ch != JCOV_DATA_C && ch != JCOV_DATA_M && ch != JCOV_DATA_A)) {                sprintf(buf, "bad DATA section (line %d)", (int)line_number);                ERROR_AND_EXIT(buf);            }            last_class.data_type = ch;            found_class = find_class(&last_class);            merge_action = guess_merge_action(found_class, &last_class);            switch (merge_action) {            case MERGE:                write_class_header(&last_class, last_modifiers);                found_class->unloaded = 1;                break;            case TAKE_FROM_MEM:                write_class(&found_class, NULL);                found_class->unloaded = 1;                break;            case TAKE_FROM_DISK:                write_class_header(&last_class, last_modifiers);                break;            case TAKE_BOTH:                write_class(&found_class, NULL);                found_class->unloaded = 1;                write_class_header(&last_class, last_modifiers);                break;            case SKIP_BOTH:                break;            }        } else if (EQUALS(token, KEYWORD_METHOD)) {            if (state != 6 && state != 4) {                sprintf(buf, "malformed jcov file (line %d)", (int)line_number);                ERROR_AND_EXIT(buf);            }            state = 5;            if (merge_action == SKIP_BOTH || merge_action == TAKE_FROM_MEM) {                continue;            }            if (found_class == NULL) {                fprintf(temp_file, "%s\n", line);                continue;            }            get_next_token(DELIM);            CHK_TOKEN;            tmp = strchr(token, '(');            if (tmp == NULL) {                sprintf(buf, "bad method signature : %s (line %d)",                        token, (int)line_number);                ERROR_AND_EXIT(buf);            }            m_nam = last_method.method_name;            m_sig = last_method.method_signature;            memcpy(m_nam, token, tmp - token);            m_nam[tmp - token] = '\0';            strcpy(m_sig, tmp);            for (ind = found_class->num_methods-1; ind >= 0; ind--) {                m = &(found_class->methods[ind]);                if (!strcmp(m_nam, m->method_name) && !strcmp(m_sig, m->method_signature)) {                    break;                }            }            if (ind == -1) {                sprintf(buf, "unexpected method %s%s (%s)", m_nam, m_sig, last_class.name);                ERROR_AND_EXIT(buf);            }            found_method = find_method(found_class->methods[ind].method_id);            if (found_method == NULL) {                sprintf(buf, "(internal) cannot find method %s.%s%s",                        last_class.name, m_nam, m_sig);                ERROR_AND_EXIT(buf);            }            jcov_item_ind = 0;            fprintf(temp_file, "%s\n", line);        } else if (EQUALS(token, KEYWORD_FILTER)) {            get_next_token(DELIM);            CHK_TOKEN;            if (filters_total > filters_max) {                char **tmp_filters;                int i;                filters_max *= 2;                tmp_filters = jcov_calloc(sizeof(char*) * filters_max);                for (i = 0; i < filters_total; i++) {		    tmp_filters[i] = filters[i];		}                jcov_free(filters);                filters = tmp_filters;            }            filters[filters_total++] = jcov_strdup(token);            fprintf(temp_file, "%s\n", line);        } else { /* else parse jcov item data */            if (state != 5 && state != 6) {                sprintf(buf, "malformed jcov file (line %d)", (int)line_number);                ERROR_AND_EXIT(buf);            }            state = 6;            switch (merge_action) {            case TAKE_FROM_MEM:            case SKIP_BOTH:                continue;            case TAKE_FROM_DISK:            case TAKE_BOTH:                fprintf(temp_file, "%s\n", line);                continue;            case MERGE:                break;            }            ct_size = found_method->covtable_size;            if (jcov_item_ind > ct_size - 1) {                sprintf(buf, "numbers of jcov items don't match (line %d)", (int)line_number);                ERROR_AND_EXIT(buf);            }            do {                ji = &(found_method->covtable[jcov_item_ind++]);            } while (ji->type == 5 && ji->where_line == 0 && jcov_item_ind < ct_size);            jcov_item.type = (unsigned char)strtoul(token, NULL, 10);            get_next_token(DELIM);            CHK_TOKEN;            jcov_item.where_line = strtoul(token, NULL, 10);            get_next_token(DELIM);            CHK_TOKEN;            jcov_item.where_pos = strtoul(token, NULL, 10);            get_next_token(DELIM);            CHK_TOKEN;            jcov_item.count = strtoul(token, NULL, 10);            if (jcov_item.type != ji->type || jcov_item.where_line != ji->where_line ||                jcov_item.where_pos != ji->where_pos) {                sprintf(buf, "jcov items don't match (line %d)", (int)line_number);                ERROR_AND_EXIT(buf);            }            fprintf(temp_file, SECT_PATTERN, ji->type, ji->where_line, ji->where_pos,                    ji->count + jcov_item.count);        }    }    CHK_STATE(6, 0);    iterate_all_classes(write_class);}void save_jcov_data(char *filename) {    iterate_all_classes(clear_unloaded);    do {        sprintf(buf, "%s.%d", filename, rand());    } while(jcov_file_exists(buf));    strcpy(temp_file_name, buf);    if (verbose_mode > 0) {        sprintf(buf, "Saving jcov data : file %s, temp file %s",                filename, temp_file_name);        jcov_info(buf);    }    if ((temp_file = fopen(temp_file_name, "w+")) == NULL ) {        sprintf(buf, "cannot create file : %s\n", temp_file_name);        jcov_error_stop(buf);    }    fprintf(temp_file, "%s %d.%d\n", KEYWORD_HEADER,             JCOV_FILE_MAJOR_VER, JCOV_FILE_MINOR_VER);    /* does the file exist or do we have to overwrite it? */    if (!jcov_file_exists(filename) || overwrite_jcov_file) {        /* yes - write new jcov data file */        iterate_all_classes(write_class);        if (fclose(temp_file) != 0) {            jcov_error("cannot close file");        }    } else {        /* no - merge jcov data with existing data file */        if ((result_file = fopen(filename, "rb")) == NULL) {            sprintf(buf,"cannot open file : %s\n", filename);            jcov_error_stop(buf);        }        read_line();        get_next_token(DELIM);        CHK_TOKEN;        if (!EQUALS(token, KEYWORD_HEADER)) {            ERROR_AND_EXIT("malformed jcov file header");        }        get_next_token(".");        CHK_TOKEN;        if (strtoul(token, NULL, 10) > JCOV_FILE_MAJOR_VER) {            sprintf(buf, "jcov file version is higher than current (%d.%d)",                    JCOV_FILE_MAJOR_VER, JCOV_FILE_MINOR_VER);            ERROR_AND_EXIT(buf);        }        get_next_token(DELIM);        CHK_TOKEN;        if (strtoul(token, NULL, 10) > JCOV_FILE_MINOR_VER) {            sprintf(buf, "jcov file version is higher than current (%d.%d)",                    JCOV_FILE_MAJOR_VER, JCOV_FILE_MINOR_VER);            ERROR_AND_EXIT(buf);        }        read_and_merge_data();        jcov_close(&result_file);        jcov_close(&temp_file);        jcov_remove(filename);    }    if (rename(temp_file_name, filename) == -1) {        sprintf(buf,"cannot rename file : %s -> %s\n", temp_file_name, filename);        jcov_error_stop(buf);    }}void jcov_file_init(void) {    line_number = 0;    filters_max = 32;    filters_total = 0;    filters = (char**)jcov_calloc(sizeof(char*) * filters_max);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?