Changeset 520 for cpp/frams/_demos


Ignore:
Timestamp:
06/22/16 16:41:58 (8 years ago)
Author:
Maciej Komosinski
Message:

Code formatting

Location:
cpp/frams/_demos
Files:
3 edited

Legend:

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

    r515 r520  
    1111
    1212 \include loader_test.cpp
    13 */
     13 */
    1414
    15 int main(int argc,char*argv[])
     15int main(int argc, char*argv[])
    1616{
    17 if (argc<2)
     17        if (argc < 2)
    1818        {
    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;
     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;
    2424        }
    2525
    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 const char* field_name=(argc<4)?NULL:argv[3];
    31 int selected_index=(selected&&isdigit(selected[0]))?atol(selected):0;
    32 // using char* constructor (passing the file name to open)
    33 MiniGenotype *loaded;
    34 while(loaded=loader.loadNextGenotype())
     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        const char* field_name = (argc < 4) ? NULL : argv[3];
     31        int selected_index = (selected&&isdigit(selected[0])) ? atol(selected) : 0;
     32        // using char* constructor (passing the file name to open)
     33        MiniGenotype *loaded;
     34        while (loaded = loader.loadNextGenotype())
    3535        { // if loaded != NULL then the "org:" object data was
    36          // loaded into MiniGenotype object
    37         count++;
    38         totalsize+=loaded->genotype.len();
    39         if (selected)
     36                // loaded into MiniGenotype object
     37                count++;
     38                totalsize += loaded->genotype.len();
     39                if (selected)
    4040                {
    41                 if (selected_index)
     41                        if (selected_index)
    4242                        {
    43                         if (selected_index!=count)
    44                                 continue;
     43                                if (selected_index != count)
     44                                        continue;
    4545                        }
    46                 else
     46                        else
    4747                        {
    48                         if (strcmp(loaded->name.c_str(),selected))
    49                                 continue;
     48                                if (strcmp(loaded->name.c_str(), selected))
     49                                        continue;
    5050                        }
    51                 if (field_name)
     51                        if (field_name)
    5252                        {
    53                         Param p(minigenotype_paramtab,loaded);
    54                         int field_index=p.findId(field_name);
    55                         if (field_index<0)
     53                                Param p(minigenotype_paramtab, loaded);
     54                                int field_index = p.findId(field_name);
     55                                if (field_index < 0)
    5656                                {
    57                                 printf("Field '%s' not found\n",field_name);
    58                                 return 3;
     57                                        printf("Field '%s' not found\n", field_name);
     58                                        return 3;
    5959                                }
    60                         else
    61                                 puts(p.get(field_index).c_str());
    62                                 }
     60                                else
     61                                        puts(p.get(field_index).c_str());
     62                        }
    6363                        else
    6464                                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
     70        if (loader.getStatus() == MiniGenotypeLoader::OnError)
     71                fprintf(stderr, "Error: %s", loader.getError().c_str());
     72        // (otherwise it was the end of the file)
     73        if (selected)
     74        {
     75                fprintf(stderr, "genotype %s not found in %s\n", selected, argv[1]);
     76                return 2;
     77        }
     78        else
     79        {
     80                fprintf(stderr, "\ntotal: %d items, %d characters\n", count, totalsize);
    6581                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
    70 if (loader.getStatus()==MiniGenotypeLoader::OnError)
    71         fprintf(stderr,"Error: %s",loader.getError().c_str());
    72 // (otherwise it was the end of the file)
    73 if (selected)
    74         {
    75         fprintf(stderr,"genotype %s not found in %s\n",selected,argv[1]);
    76         return 2;
    77         }
    78 else
    79         {
    80         fprintf(stderr,"\ntotal: %d items, %d characters\n",count,totalsize);
    81         return 0;
    8282        }
    8383}
  • cpp/frams/_demos/loader_test_param.cpp

    r391 r520  
    1414 Additional information and messages are printed to standard error.
    1515 You can redirect one or both streams if needed.
    16  
     16
    1717 A sample input file for this program is "loader_test_param.in",
    1818 so you can run this program from the "cpp" directory as
    1919 ./loader_test_param  frams/_demos/loader_test_param.in
    20  
     20
    2121 \include loader_test_param.cpp
    2222 */
  • cpp/frams/_demos/saver_test_geno.cpp

    r517 r520  
    1111
    1212 \include saver_test_geno.cpp
    13 */
     13 */
    1414
    15 int main(int argc,char*argv[])
     15int main(int argc, char*argv[])
    1616{
    17 if (argc<3)
     17        if (argc < 3)
    1818        {
    19         fprintf(stderr,"Arguments: filename number_of_genotypes\n"
    20              "Example: saver_test_geno file.gen 3\n"
    21                 );
    22         return 1;
     19                fprintf(stderr, "Arguments: filename number_of_genotypes\n"
     20                        "Example: saver_test_geno file.gen 3\n"
     21                        );
     22                return 1;
    2323        }
    2424
    25 StdioFileSystem_autoselect stdiofilesys;
    26 VirtFILE *f=Vfopen(argv[1],"w");
    27 if (f)
     25        StdioFileSystem_autoselect stdiofilesys;
     26        VirtFILE *f = Vfopen(argv[1], "w");
     27        if (f)
    2828        {
    29         int N=atoi(argv[2]);
    30         MiniGenotype g;
    31         Param p(minigenotype_paramtab,&g);
    32         g.clear();
    33         printf("Saving %d genotypes to %s\n",N,argv[1]);
    34         for(int i=1;i<=N;i++)
     29                int N = atoi(argv[2]);
     30                MiniGenotype g;
     31                Param p(minigenotype_paramtab, &g);
     32                g.clear();
     33                printf("Saving %d genotypes to %s\n", N, argv[1]);
     34                for (int i = 1; i <= N; i++)
    3535                {
    36                 g.name=SString::sprintf("Genotype#%d",i);
    37                 g.genotype=""; for(int x=0;x<i;x++) g.genotype+="X";
    38                 g.velocity=0.1*i;
    39                 g.energy0=1;
    40                 g.info="Saved by saver_test_geno.cpp";
    41                 g.is_valid=1;
    42                 p.save(f,"org");
     36                        g.name = SString::sprintf("Genotype#%d", i);
     37                        g.genotype = ""; for (int x = 0; x < i; x++) g.genotype += "X";
     38                        g.velocity = 0.1*i;
     39                        g.energy0 = 1;
     40                        g.info = "Saved by saver_test_geno.cpp";
     41                        g.is_valid = 1;
     42                        p.save(f, "org");
    4343                }
    44         delete f;
    45         return 0;
     44                delete f;
     45                return 0;
    4646        }
    47 else
     47        else
    4848        {
    49         printf("Could not write to %s\n",argv[1]);
    50         return 1;
     49                printf("Could not write to %s\n", argv[1]);
     50                return 1;
    5151        }
    5252}
Note: See TracChangeset for help on using the changeset viewer.