Changeset 115 for cpp


Ignore:
Timestamp:
01/26/14 06:01:27 (10 years ago)
Author:
sz
Message:

loader_test can now extract raw genotype from a Framsticks file (*.gen/*.expt), which makes it useful in cooperation with other tools, for example:
loader_test walking.gen "Basic Quadruped" | theater -g -

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/_demos/loader_test.cpp

    r109 r115  
    1414int main(int argc,char*argv[])
    1515{
    16 puts("This example shows how to load genotypes from the standard Framsticks genotype file");
    1716if (argc<2)
    1817        {
    19         puts("(Please give the file name as command line argument)");
     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"
     20             "Example: loader_test walking.gen \"Basic Quadruped\" | theater -g -"
     21                );
    2022        return 1;
    2123        }
     
    2325long count=0,totalsize=0;
    2426MiniGenotypeLoader loader(argv[1]);
     27const char* selected=(argc<3)?NULL:argv[2];
     28int selected_index=(selected&&isdigit(selected[0]))?atol(selected):0;
    2529// using char* constructor (passing the file name to open)
    2630MiniGenotype *loaded;
     
    3034        count++;
    3135        totalsize+=loaded->genotype.len();
    32         printf("%d. %s\t(%d characters)\n",count,(const char*)loaded->name,loaded->genotype.len());
     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());
    3352        }
    3453// the loop repeats until loaded==NULL, which could be beacause of error
    3554if (loader.getStatus()==MiniGenotypeLoader::OnError)
    36         printf("Error: %s",(const char*)loader.getError());
     55        fprintf(stderr,"Error: %s",(const char*)loader.getError());
    3756// (otherwise it was the end of the file)
    38 
    39 printf("\ntotal: %d items, %d characters\n",count,totalsize);
    40 return 0;
     57if (selected)
     58        {
     59        fprintf(stderr,"genotype %s not found in %s\n",selected,argv[1]);
     60        return 2;
     61        }
     62else
     63        {
     64        fprintf(stderr,"\ntotal: %d items, %d characters\n",count,totalsize);
     65        return 0;
     66        }
    4167}
Note: See TracChangeset for help on using the changeset viewer.