int newickmain(int argc, char **argv) { NEWICKTREE *tree; int err; char *label; if(argc != 2) fprintf(stderr, "Newick tree loader\n"); else { tree = loadnewicktree(argv[1], &err); if(!tree) { switch(err) { case -1: printf("Out of memory\n"); break; case -2: printf("parse error\n"); break; case -3: printf("Can't load file\n"); break; default: printf("Error %d\n", err); } } else { printf("Loaded\n"); printnewicktree(tree); } killnewicktree(tree); } label = makenewicklabel("My name is \'Fred()\'"); printf("***%s***\n", label); free(label); return 0; }
/* load newick tree from an opened stream Params: fp - input stream error - return for error Returns: pointer to object, 0 on fail Error codes -1 out of memory -2 parse error Notes: format allows several trees to be stroed in a file */ NEWICKTREE *floadnewicktree(FILE *fp, int *error) { NEWICKTREE *answer; int err; int ch; answer = malloc(sizeof(NEWICKTREE)); if(!answer) { err = -1; goto error_exit; } skipspace(fp); ch = fgetc(fp); if(ch == '(' ) { answer->root = loadnode(fp, &err); if(!answer->root || err != 0) goto error_exit; } skipspace(fp); ch = fgetc(fp); if(ch != ';') { err = -2; goto error_exit; } if(error) *error = 0; return answer; error_exit: killnewicktree(answer); if(error) *error = err; return 0; }
NEWICKTREE *TreeOPE::parsetree(char *str, int *error, NEWICKTREE *testtree) { NEWICKTREE *answer = NULL; int err; char *ch = NULL; int i = 0, idx = 0; answer = (NEWICKTREE *) malloc(sizeof(NEWICKTREE)); // skip space char *str_copy = (char *) malloc(sizeof(char) * (strlen(str) + 2)); for(i = 0; i < strlen(str) + 1; i++) { if((*(str + i)) != ' ') { *(str_copy + idx) = *(str + i); idx++; } if(*(str + i) == '\0') break; } *(str_copy + idx) = '\0'; if (!answer) { err = -1; goto error_exit; } ch = str_copy; if (*ch == '(') { ch++; answer->root = parsenode(&ch, &err); if (!answer->root || err != 0) goto error_exit; } if(testtree != NULL) { cout << "in2-----" << endl; TreeOPE::printnewicktree(testtree); } if (*ch != ';') { err = -2; // parse error goto error_exit; } if (error) *error = 0; free(str_copy); // std::cout << "root->child:" << (long) answer->root->child << std::endl;//-- WHtest return answer; error_exit: cout << "Wrong in parsetree()!\n\n";//---- free(str_copy); killnewicktree(answer); if (error) *error = err; return NULL; }