source: cpp/frams/_demos/loader_test.cpp @ 498

Last change on this file since 498 was 473, checked in by sz, 9 years ago
  • MiniGenotype? extended to handle all Genotype fields contained in .gen files
  • loader_test now displays any MiniGenotype? field instead of the raw genotype when given the field name
  • MiniGenotypeLoader? and stdiofile.o included in SDK_LIB_OBJS
  • Property svn:eol-style set to native
File size: 2.4 KB
Line 
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 <common/virtfile/stdiofile.h>
7
8/**
9 @file
10 Sample code: Loading genotypes from Framsticks files
11
12 \include loader_test.cpp
13*/
14
15int main(int argc,char*argv[])
16{
17if (argc<2)
18        {
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"
21             "Example: loader_test walking.gen \"Basic Quadruped\" | theater -g -\n"
22                );
23        return 1;
24        }
25
26long count=0,totalsize=0;
27StdioFileSystem_autoselect stdiofilesys;
28MiniGenotypeLoader loader(argv[1]);
29const char* selected=(argc<3)?NULL:argv[2];
30const char* field_name=(argc<4)?NULL:argv[3];
31int selected_index=(selected&&isdigit(selected[0]))?atol(selected):0;
32// using char* constructor (passing the file name to open)
33MiniGenotype *loaded;
34while(loaded=loader.loadNextGenotype())
35        { // if loaded != NULL then the "org:" object data was
36         // loaded into MiniGenotype object
37        count++;
38        totalsize+=loaded->genotype.len();
39        if (selected)
40                {
41                if (selected_index)
42                        {
43                        if (selected_index!=count)
44                                continue;
45                        }
46                else
47                        {
48                        if (strcmp(loaded->name.c_str(),selected))
49                                continue;
50                        }
51                if (field_name)
52                        {
53                        Param p(minigenotype_paramtab,loaded);
54                        int field_index=p.findId(field_name);
55                        if (field_index<0)
56                                {
57                                printf("Field '%s' not found\n",field_name);
58                                return 3;
59                                }
60                        else
61                                puts(p.get(field_index).c_str());
62                                }
63                        else
64                                puts(loaded->genotype.c_str());
65                return 0;
66                }
67        fprintf(stderr,"%d. %s\t(%d characters)\n",count,loaded->name.c_str(),loaded->genotype.len());
68        }
69// the loop repeats until loaded==NULL, which could be beacause of error
70if (loader.getStatus()==MiniGenotypeLoader::OnError)
71        fprintf(stderr,"Error: %s",loader.getError().c_str());
72// (otherwise it was the end of the file)
73if (selected)
74        {
75        fprintf(stderr,"genotype %s not found in %s\n",selected,argv[1]);
76        return 2;
77        }
78else
79        {
80        fprintf(stderr,"\ntotal: %d items, %d characters\n",count,totalsize);
81        return 0;
82        }
83}
Note: See TracBrowser for help on using the repository browser.