- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Models/CCA/Io/Mof/Main.cpp

    00001
    00002 //
    00003 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00009 // Organization:        Model-based Embedded and Robotic Systems Group
    00010 //                                      Massachusetts Institute of Technology
    00011 //
    00013
    00014 //#include <cstdlib>
    00015 #include <iostream>
    00016 #include "MOFReader.h"
    00017
    00018 using namespace Mers::Models::CCA::Io::Mof;
    00019 using Mers::Models::CCA::CCAModel;
    00020 using std::cout;
    00021 using std::endl;
    00022
    00023 void printUse(char const * programName)
    00024 {
    00025         fprintf(stderr, "Use:\n\t%s <CCA file> [file|string]\n", programName);
    00026         fputs("\n\tLoads the CCA file and prints out the resulting model\n", stderr);
    00027         fputs("\n\tfile - loads the file using the MOFReader's file loading mechanism (default)\n", stderr);
    00028         fputs("\tstring - loads the file using the MOFReader's string loading mechanism\n", stderr);
    00029 }
    00030
    00031 void strLowerCase(char * str){
    00032         while(*str != '\0'){
    00033                 if(*str >= 'A' && *str <= 'Z')
    00034                         *str -= 'A' - 'a';
    00035                 str++;
    00036         }
    00037 }
    00038
    00044 int
    00045 main(int argc, char *argv[])
    00046 {
    00047         bool useString = false;
    00048         CCAModel * mdl = NULL;
    00049
    00050         if (argc != 2 && argc != 3)
    00051         {
    00052                 if(argc == 0)
    00053                         printUse("MOFReader");
    00054                 else
    00055                         printUse(argv[0]);
    00056                 return -1;
    00057         }
    00058
    00059         if(argc == 3){
    00060                 strLowerCase(argv[2]);
    00061                 if(strcmp(argv[2], "string") == 0){
    00062                         useString = true;
    00063                 }else if(strcmp(argv[2], "file") == 0){
    00064                         useString = false;
    00065                 }else{
    00066                         fprintf(stderr, "Error, expected \"string\" or \"file\" but found \"%s\"\n", argv[2]);
    00067                         fprintf(stderr, "Invalid second argument\n\n");
    00068                         printUse(argv[0]);
    00069                 }
    00070         }
    00071
    00072         if(useString){
    00073                 char * buf;
    00074                 FILE * input;
    00075                 int len;
    00076
    00077                 cout << "Loading string from file " << argv[1] << endl;
    00078                 input = fopen(argv[1], "rb");
    00079                 if(!input){
    00080                         perror(argv[1]);
    00081                         printUse(argv[0]);
    00082                         return 1;
    00083                 }
    00084
    00085                 fseek(input, 0, SEEK_END);
    00086                 len = ftell(input);
    00087
    00088                 if(len <= 0){
    00089                         fprintf(stderr, "Error determining the file size\n");
    00090                         printUse(argv[0]);
    00091                         return 2;
    00092                 }
    00093
    00094                 buf = new char[len];
    00095                 if(!buf){
    00096                         fprintf(stderr, "Unable to allocate a %d Byte buffer\n", len);
    00097                         return 2;
    00098                 }
    00099
    00100                 fseek(input, 0, SEEK_SET);
    00101
    00102                 if(fread(buf, 1, len, input) != (unsigned int)len){
    00103                         fprintf(stderr, "Unable to read %d bytes from the file\n", len);
    00104                         printUse(argv[0]);
    00105                         delete[] buf;
    00106                         return 2;
    00107                 }
    00108
    00109                 if(MOFReader::loadMOFFromString(buf, mdl)){
    00110                         printUse(argv[0]);
    00111                         if(mdl)
    00112                                 delete mdl;
    00113                         delete[] buf;
    00114                         return 3;
    00115                 }
    00116
    00117                 delete[] buf;
    00118         }else{
    00119                 cout << "Loading file " << argv[1] << endl;
    00120                 if(MOFReader::loadMOF(argv[1], mdl))
    00121                 {
    00122                         printUse(argv[0]);
    00123                         if(mdl)
    00124                                 delete mdl;
    00125                         return 1;
    00126                 }
    00127         }
    00128
    00129         cout << "Model reading succeeded!" << endl;
    00130
    00131         cout << *mdl << endl;
    00132
    00133         if(mdl)
    00134                 delete mdl;
    00135         return 0;
    00136 }
    00137