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. |
---|
4 | |
---|
5 | #include "genotypeloader.h" |
---|
6 | #include <frams/virtfile/stdiofile.h> |
---|
7 | |
---|
8 | /** |
---|
9 | @file |
---|
10 | Sample code: Loading genotypes from Framsticks files |
---|
11 | |
---|
12 | \include loader_test.cpp |
---|
13 | */ |
---|
14 | |
---|
15 | int main(int argc,char*argv[]) |
---|
16 | { |
---|
17 | if (argc<2) |
---|
18 | { |
---|
19 | fprintf(stderr,"Arguments: filename [optional: genotype name or index (1-based)]\n" |
---|
20 | "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" |
---|
21 | "Example: loader_test walking.gen \"Basic Quadruped\" | theater -g -\n" |
---|
22 | ); |
---|
23 | return 1; |
---|
24 | } |
---|
25 | |
---|
26 | long count=0,totalsize=0; |
---|
27 | StdioFileSystem_autoselect stdiofilesys; |
---|
28 | MiniGenotypeLoader loader(argv[1]); |
---|
29 | const char* selected=(argc<3)?NULL:argv[2]; |
---|
30 | int selected_index=(selected&&isdigit(selected[0]))?atol(selected):0; |
---|
31 | // using char* constructor (passing the file name to open) |
---|
32 | MiniGenotype *loaded; |
---|
33 | while(loaded=loader.loadNextGenotype()) |
---|
34 | { // if loaded != NULL then the "org:" object data was |
---|
35 | // loaded into MiniGenotype object |
---|
36 | count++; |
---|
37 | totalsize+=loaded->genotype.len(); |
---|
38 | if (selected) |
---|
39 | { |
---|
40 | if (selected_index) |
---|
41 | { |
---|
42 | if (selected_index!=count) |
---|
43 | continue; |
---|
44 | } |
---|
45 | else |
---|
46 | { |
---|
47 | if (strcmp(loaded->name.c_str(),selected)) |
---|
48 | continue; |
---|
49 | } |
---|
50 | puts(loaded->genotype.c_str()); |
---|
51 | return 0; |
---|
52 | } |
---|
53 | fprintf(stderr,"%d. %s\t(%d characters)\n",count,loaded->name.c_str(),loaded->genotype.len()); |
---|
54 | } |
---|
55 | // the loop repeats until loaded==NULL, which could be beacause of error |
---|
56 | if (loader.getStatus()==MiniGenotypeLoader::OnError) |
---|
57 | fprintf(stderr,"Error: %s",loader.getError().c_str()); |
---|
58 | // (otherwise it was the end of the file) |
---|
59 | if (selected) |
---|
60 | { |
---|
61 | fprintf(stderr,"genotype %s not found in %s\n",selected,argv[1]); |
---|
62 | return 2; |
---|
63 | } |
---|
64 | else |
---|
65 | { |
---|
66 | fprintf(stderr,"\ntotal: %d items, %d characters\n",count,totalsize); |
---|
67 | return 0; |
---|
68 | } |
---|
69 | } |
---|