void gabufr_reset_tbls() { gaint i; if (tbl_b != NULL) { free(tbl_b); } if (tbl_d_entries != NULL) { for (i = 0; i < GABUFR_TBL_SIZE; i++) { gabufr_free_varids(tbl_d_entries[i]); } free(tbl_d_entries); } tbl_b = NULL; tbl_d_entries = NULL; }
/* parses the raw data for a msg into a list of val structures */ gaint gabufr_parsevals(gabufr_msg * msg, gaint parse_data) { gabufr_parseinf inf; gabufr_varid * msg_descs; gaint extra; inf.delrep = 0; inf.z = -1; inf.datapos = msg->section4 + 4; inf.databits = 0; inf.msg = msg; msg_descs = gabufr_extract_msg_desc(msg, parse_data); if (msg_descs == NULL) { return GABUFR_ERR; } if (parse_data == GABUFR_PARSE_DATA || msg->is_new_tbl) { for (inf.sub = 0; inf.sub < msg->subcnt; inf.sub++) { if (GABUFR_DEBUG) printf("\n\n@@@ parsing subset %d @@@\n", inf.sub); inf.parentpos = NULL; inf.curval = NULL; if (gabufr_parselist(&inf, msg_descs) == GABUFR_ERR) { return GABUFR_ERR; } } if (GABUFR_DEBUG) printf("data position is (%p + %d); end of data is %p\n", inf.datapos, inf.databits, inf.msg->end); extra = inf.msg->end - inf.datapos; if (extra > 1) { printf("Corrupt message: %d extra bytes in data section\n", extra); return GABUFR_ERR; } } else { return GABUFR_ERR; } gabufr_free_varids(msg_descs); return GABUFR_OK; }
/* builds a list of varid's from the message header */ gabufr_varid * gabufr_extract_msg_desc(gabufr_msg * msg, gaint parse_data) { unsigned char * pos, * start, * end; gabufr_varid * head, * current, * next; gabufr_varinf * varinf; if (msg == NULL) { return NULL; } if (GABUFR_DEBUG || parse_data == GABUFR_NO_PARSE_DATA) { printf("\n\n\n>>> start of message\n"); } head = current = NULL; start = msg->section3 + 7; end = msg->section4; for (pos = start; pos < end; pos += 2) { next = (gabufr_varid *) malloc(sizeof(gabufr_varid)); if (next == NULL) { printf("Memory allocation failed during parsing\n"); gabufr_free_varids(head); return NULL; } next->f = gagbb(pos, 0, 2); next->x = gagbb(pos, 2, 6); next->y = gagbb(pos, 8, 8); if (next->f == 0 && next->x == 0 && next->y == 0) { free(next); continue; } next->next = NULL; if (!gabufr_valid_varid(next->f, next->x, next->y)) { printf("error: corrupt message (contains invalid FXY %d-%.2d-%.3d)\n", next->f, next->x, next->y); free(next); gabufr_free_varids(head); return NULL; } if (next->f == 0) { varinf = gabufr_get_varinf(next->x, next->y); if (varinf->width == 0) { printf("error: no table information for FXY %d-%.2d-%.3d\n", next->f, next->x, next->y); free(next); gabufr_free_varids(head); return NULL; } } if (GABUFR_DEBUG || parse_data == GABUFR_NO_PARSE_DATA) gabufr_print_varid(next, 0); if (head) { current->next = next; } else { head = next; } current = next; } if (GABUFR_DEBUG || parse_data == GABUFR_NO_PARSE_DATA) { printf("\n<<< end of message"); } return head; }
gabufr_val * gabufr_update_ncep_tbl_d(gabufr_dset * file, gabufr_msg * msg, gabufr_val * pos) { gaint new_x, new_y; gaint y_expected, z_expected; char fstr[2], xstr[3], ystr[4]; gabufr_varid * head, * next; gabufr_varid ** entry_ptr; new_x = new_y = 0; head = NULL; z_expected = pos->z; for (y_expected = 11; y_expected <= 12; y_expected++) { if (!pos) { printf("ran out of data in middle of entry!\n"); return pos; } if (pos->y != y_expected) { printf("expected y = %d; got y = %d\n", y_expected, pos->y); return pos; } if (pos->z != z_expected) { printf("expected z = %d; got z = %d\n", z_expected, pos->z); return pos; } if (!pos->sval) { printf("expected string data!\n"); return pos; } switch (pos->y) { case 11: new_x = strtol(pos->sval, NULL, 10); break; case 12: new_y = strtol(pos->sval, NULL, 10); break; } pos = pos->next; } if (GABUFR_TBL_DEBUG) printf("new table D entry is (3, %d, %d)\n", new_x, new_y); entry_ptr = &tbl_d_entries[gabufr_tbl_index(new_x, new_y)]; gabufr_free_varids(*entry_ptr); head = *entry_ptr = NULL; while (pos) { if (pos->x == -1 && pos->y == -1) { if (GABUFR_TBL_DEBUG) printf("sequence description: [%s]\n", pos->sval); } else if (pos->x == 0 && pos->y == 30) { next = (gabufr_varid *) calloc(sizeof(gabufr_varid), 1); if (next == NULL) { printf("Memory allocation failed during parsing\n"); return NULL; } memcpy(fstr, pos->sval, 1); fstr[1] = '\0'; memcpy(xstr, pos->sval+1, 2); xstr[2] = '\0'; memcpy(ystr, pos->sval+3, 3); ystr[3] = '\0'; next->f = strtol(fstr, NULL, 10); next->x = strtol(xstr, NULL, 10); next->y = strtol(ystr, NULL, 10); if (GABUFR_TBL_DEBUG) printf("\tadding (%d, %d, %d) to sequence\n", next->f, next->x, next->y); if (head) { head->next = next; } else { *entry_ptr = next; } head = next; } else { break; } pos = pos->next; } if (GABUFR_TBL_DEBUG) printf("\n"); return pos; }