| [286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
|---|
| [973] | 2 | // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. |
|---|
| [286] | 3 | // See LICENSE.txt for details. |
|---|
| [109] | 4 | |
|---|
| 5 | #include "genotypeloader.h" |
|---|
| [382] | 6 | #include <common/virtfile/stdiofile.h> |
|---|
| [109] | 7 | |
|---|
| 8 | /** |
|---|
| 9 | @file |
|---|
| 10 | Sample code: Loading genotypes from Framsticks files |
|---|
| 11 | |
|---|
| 12 | \include loader_test.cpp |
|---|
| [520] | 13 | */ |
|---|
| [109] | 14 | |
|---|
| [520] | 15 | int main(int argc, char*argv[]) |
|---|
| [109] | 16 | { |
|---|
| [520] | 17 | if (argc < 2) |
|---|
| [109] | 18 | { |
|---|
| [520] | 19 | fprintf(stderr, "Arguments: filename [genotype name or index (1-based) [field name]]\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 a genotype and a field name is given, the field value (instead of the raw genotype) is printed. If the second argument is not given, the genotype names from the file will be listed.\n" |
|---|
| [986] | 21 | "Example: loader_test_geno walking.gen \"Basic Quadruped\" | theater -g -\n" |
|---|
| [973] | 22 | ); |
|---|
| [520] | 23 | return 1; |
|---|
| [109] | 24 | } |
|---|
| 25 | |
|---|
| [520] | 26 | long count = 0, totalsize = 0; |
|---|
| 27 | StdioFileSystem_autoselect stdiofilesys; |
|---|
| [732] | 28 | GenotypeMiniLoader loader(argv[1]); |
|---|
| [520] | 29 | const char* selected = (argc < 3) ? NULL : argv[2]; |
|---|
| 30 | const char* field_name = (argc < 4) ? NULL : argv[3]; |
|---|
| [973] | 31 | int selected_index = (selected && isdigit(selected[0])) ? atol(selected) : 0; |
|---|
| [520] | 32 | // using char* constructor (passing the file name to open) |
|---|
| [732] | 33 | GenotypeMini *loaded; |
|---|
| [520] | 34 | while (loaded = loader.loadNextGenotype()) |
|---|
| [109] | 35 | { // if loaded != NULL then the "org:" object data was |
|---|
| [520] | 36 | // loaded into MiniGenotype object |
|---|
| 37 | count++; |
|---|
| [973] | 38 | totalsize += loaded->genotype.length(); |
|---|
| [520] | 39 | if (selected) |
|---|
| [115] | 40 | { |
|---|
| [520] | 41 | if (selected_index) |
|---|
| [115] | 42 | { |
|---|
| [520] | 43 | if (selected_index != count) |
|---|
| 44 | continue; |
|---|
| [115] | 45 | } |
|---|
| [520] | 46 | else |
|---|
| [115] | 47 | { |
|---|
| [520] | 48 | if (strcmp(loaded->name.c_str(), selected)) |
|---|
| 49 | continue; |
|---|
| [115] | 50 | } |
|---|
| [520] | 51 | if (field_name) |
|---|
| [473] | 52 | { |
|---|
| [732] | 53 | Param p(genotypemini_paramtab, loaded); |
|---|
| [520] | 54 | int field_index = p.findId(field_name); |
|---|
| 55 | if (field_index < 0) |
|---|
| [473] | 56 | { |
|---|
| [520] | 57 | printf("Field '%s' not found\n", field_name); |
|---|
| 58 | return 3; |
|---|
| [473] | 59 | } |
|---|
| [520] | 60 | else |
|---|
| 61 | puts(p.get(field_index).c_str()); |
|---|
| 62 | } |
|---|
| [473] | 63 | else |
|---|
| 64 | puts(loaded->genotype.c_str()); |
|---|
| [520] | 65 | return 0; |
|---|
| [115] | 66 | } |
|---|
| [973] | 67 | fprintf(stderr, "%d. %s\t(%d characters)\n", count, loaded->name.c_str(), loaded->genotype.length()); |
|---|
| [109] | 68 | } |
|---|
| [520] | 69 | // the loop repeats until loaded==NULL, which could be beacause of error |
|---|
| [732] | 70 | if (loader.getStatus() == GenotypeMiniLoader::OnError) |
|---|
| [986] | 71 | fprintf(stderr, "Error: %s\n", loader.getError().c_str()); |
|---|
| [520] | 72 | // (otherwise it was the end of the file) |
|---|
| 73 | if (selected) |
|---|
| [115] | 74 | { |
|---|
| [986] | 75 | fprintf(stderr, "Genotype %s not found in %s\n", selected, argv[1]); |
|---|
| [520] | 76 | return 2; |
|---|
| [115] | 77 | } |
|---|
| [520] | 78 | else |
|---|
| [115] | 79 | { |
|---|
| [986] | 80 | fprintf(stderr, "\nTotal: %d items, %d characters\n", count, totalsize); |
|---|
| [520] | 81 | return 0; |
|---|
| [115] | 82 | } |
|---|
| [109] | 83 | } |
|---|