[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
| 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
| 5 | #include "genotypeloader.h" |
---|
| 6 | |
---|
| 7 | /** |
---|
| 8 | @file |
---|
| 9 | Sample code: Loading genotypes from Framsticks files |
---|
| 10 | |
---|
| 11 | \include loader_test.cpp |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | int main(int argc,char*argv[]) |
---|
| 15 | { |
---|
| 16 | if (argc<2) |
---|
| 17 | { |
---|
[115] | 18 | fprintf(stderr,"Arguments: filename [optional: genotype name or index (1-based)]\n" |
---|
| 19 | "If a genotype is indicated (by providing the optional genotype identifier), the program will output the raw genotype, suitable for Framsticks Theater's genotype viewer mode. If the second optional argument is not given, the genotype names from the file will be listed.\n" |
---|
[126] | 20 | "Example: loader_test walking.gen \"Basic Quadruped\" | theater -g -\n" |
---|
[115] | 21 | ); |
---|
[109] | 22 | return 1; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | long count=0,totalsize=0; |
---|
| 26 | MiniGenotypeLoader loader(argv[1]); |
---|
[115] | 27 | const char* selected=(argc<3)?NULL:argv[2]; |
---|
| 28 | int selected_index=(selected&&isdigit(selected[0]))?atol(selected):0; |
---|
[109] | 29 | // using char* constructor (passing the file name to open) |
---|
| 30 | MiniGenotype *loaded; |
---|
| 31 | while(loaded=loader.loadNextGenotype()) |
---|
| 32 | { // if loaded != NULL then the "org:" object data was |
---|
| 33 | // loaded into MiniGenotype object |
---|
| 34 | count++; |
---|
| 35 | totalsize+=loaded->genotype.len(); |
---|
[115] | 36 | if (selected) |
---|
| 37 | { |
---|
| 38 | if (selected_index) |
---|
| 39 | { |
---|
| 40 | if (selected_index!=count) |
---|
| 41 | continue; |
---|
| 42 | } |
---|
| 43 | else |
---|
| 44 | { |
---|
| 45 | if (strcmp((const char*)loaded->name,selected)) |
---|
| 46 | continue; |
---|
| 47 | } |
---|
| 48 | puts((const char*)loaded->genotype); |
---|
| 49 | return 0; |
---|
| 50 | } |
---|
| 51 | fprintf(stderr,"%d. %s\t(%d characters)\n",count,(const char*)loaded->name,loaded->genotype.len()); |
---|
[109] | 52 | } |
---|
| 53 | // the loop repeats until loaded==NULL, which could be beacause of error |
---|
| 54 | if (loader.getStatus()==MiniGenotypeLoader::OnError) |
---|
[115] | 55 | fprintf(stderr,"Error: %s",(const char*)loader.getError()); |
---|
[109] | 56 | // (otherwise it was the end of the file) |
---|
[115] | 57 | if (selected) |
---|
| 58 | { |
---|
| 59 | fprintf(stderr,"genotype %s not found in %s\n",selected,argv[1]); |
---|
| 60 | return 2; |
---|
| 61 | } |
---|
| 62 | else |
---|
| 63 | { |
---|
| 64 | fprintf(stderr,"\ntotal: %d items, %d characters\n",count,totalsize); |
---|
| 65 | return 0; |
---|
| 66 | } |
---|
[109] | 67 | } |
---|