LCOV - code coverage report
Current view: top level - tests - json_to_string.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 69 91 75.8 %
Date: 2014-11-22 Functions: 5 7 71.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* json_to_string.cpp -- written by Alexis WILKE for Made to Order Software Corp. (c) 2005-2014 */
       2             : 
       3             : /*
       4             : 
       5             : Copyright (c) 2005-2014 Made to Order Software Corp.
       6             : 
       7             : http://snapwebsites.org/project/as2js
       8             : 
       9             : Permission is hereby granted, free of charge, to any
      10             : person obtaining a copy of this software and
      11             : associated documentation files (the "Software"), to
      12             : deal in the Software without restriction, including
      13             : without limitation the rights to use, copy, modify,
      14             : merge, publish, distribute, sublicense, and/or sell
      15             : copies of the Software, and to permit persons to whom
      16             : the Software is furnished to do so, subject to the
      17             : following conditions:
      18             : 
      19             : The above copyright notice and this permission notice
      20             : shall be included in all copies or substantial
      21             : portions of the Software.
      22             : 
      23             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
      24             : ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
      25             : LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
      26             : FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
      27             : EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      28             : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
      29             : WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      30             : ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      31             : SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      32             : SOFTWARE.
      33             : 
      34             : */
      35             : 
      36             : #include    "as2js/json.h"
      37             : #include    "as2js/exceptions.h"
      38             : #include    "as2js/message.h"
      39             : #include    "as2js/as2js.h"
      40             : #include    "license.h"
      41             : 
      42             : #include    <advgetopt/advgetopt.h>
      43             : 
      44             : #include    <unistd.h>
      45             : 
      46             : 
      47          17 : class messages : public as2js::MessageCallback
      48             : {
      49             : public:
      50          17 :     messages()
      51          17 :     {
      52          17 :         as2js::Message::set_message_callback(this);
      53          17 :     }
      54             : 
      55             :     // implementation of the output
      56           0 :     virtual void output(as2js::message_level_t message_level, as2js::err_code_t error_code, as2js::Position const& pos, std::string const& message)
      57             :     {
      58           0 :         std::cerr << "error:" << static_cast<int>(message_level)
      59           0 :                        << ":" << static_cast<int>(error_code)
      60           0 :                        << ":" << pos
      61           0 :                        << ":" << message << std::endl;
      62           0 :     }
      63             : };
      64             : 
      65             : 
      66             : 
      67          17 : int main(int argc, char **argv)
      68             : {
      69             :     static const advgetopt::getopt::option options[] = {
      70             :         {
      71             :             '\0',
      72             :             0,
      73             :             nullptr,
      74             :             nullptr,
      75             :             "Usage: %p [--opt] [test-name]",
      76             :             advgetopt::getopt::help_argument
      77             :         },
      78             :         {
      79             :             '\0',
      80             :             0,
      81             :             nullptr,
      82             :             nullptr,
      83             :             "with --opt being one or more of the following:",
      84             :             advgetopt::getopt::help_argument
      85             :         },
      86             :         {
      87             :             'h',
      88             :             0,
      89             :             "help",
      90             :             nullptr,
      91             :             "print out this help screen",
      92             :             advgetopt::getopt::no_argument
      93             :         },
      94             :         {
      95             :             '\0',
      96             :             0,
      97             :             "license",
      98             :             nullptr,
      99             :             "prints out the license of the tests",
     100             :             advgetopt::getopt::no_argument
     101             :         },
     102             :         {
     103             :             '\0',
     104             :             0,
     105             :             "licence",
     106             :             nullptr,
     107             :             nullptr, // hide this one from the help screen
     108             :             advgetopt::getopt::no_argument
     109             :         },
     110             :         {
     111             :             'o',
     112             :             0,
     113             :             "output",
     114             :             nullptr,
     115             :             "the output filename",
     116             :             advgetopt::getopt::required_argument
     117             :         },
     118             :         {
     119             :             'V',
     120             :             0,
     121             :             "version",
     122             :             nullptr,
     123             :             "print out the as2js project version these unit tests pertain to",
     124             :             advgetopt::getopt::no_argument
     125             :         },
     126             :         {
     127             :             '\0',
     128             :             0,
     129             :             "filename",
     130             :             nullptr,
     131             :             nullptr, // hidden argument in --help screen
     132             :             advgetopt::getopt::default_multiple_argument
     133             :         },
     134             :         {
     135             :             '\0',
     136             :             0,
     137             :             nullptr,
     138             :             nullptr,
     139             :             nullptr,
     140             :             advgetopt::getopt::end_of_options
     141             :         }
     142             :     };
     143             : 
     144          17 :     std::vector<std::string> configuration_files;
     145          34 :     advgetopt::getopt opt(argc, argv, options, configuration_files, "UNITTEST_OPTIONS");
     146             : 
     147          17 :     if(opt.is_defined("help"))
     148             :     {
     149           0 :         opt.usage(advgetopt::getopt::no_error, "Usage: json_to_string [--opt] [test-name]");
     150             :         /*NOTREACHED*/
     151             :     }
     152             : 
     153          17 :     if(opt.is_defined("version"))
     154             :     {
     155           0 :         std::cout << AS2JS_VERSION << std::endl;
     156           0 :         exit(1);
     157             :     }
     158             : 
     159          17 :     if(opt.is_defined("license") || opt.is_defined("licence"))
     160             :     {
     161           0 :         as2js_tools::license::license();
     162           0 :         exit(1);
     163             :     }
     164             : 
     165          17 :     if(!opt.is_defined("filename"))
     166             :     {
     167           0 :         std::cerr << "error: no filename specified." << std::endl;
     168           0 :         exit(1);
     169             :     }
     170             : 
     171          17 :     if(!opt.is_defined("output"))
     172             :     {
     173           0 :         std::cerr << "error: no output specified." << std::endl;
     174           0 :         exit(1);
     175             :     }
     176             : 
     177          34 :     std::string output_filename(opt.get_string("output"));
     178          34 :     as2js::FileOutput::pointer_t out(new as2js::FileOutput());
     179          17 :     if(!out->open(output_filename))
     180             :     {
     181           0 :         std::cerr << "error: could not open output file \"" << output_filename << "\" for writing." << std::endl;
     182           0 :         exit(1);
     183             :     }
     184             : 
     185          34 :     messages msg;
     186          17 :     int err(0);
     187          17 :     int max_filenames(opt.size("filename"));
     188          34 :     for(int idx(0); idx < max_filenames; ++idx)
     189             :     {
     190             :         // first we use JSON to load the file, if we detect an
     191             :         // error return 1 instead of 0
     192          17 :         std::string filename(opt.get_string("filename", idx));
     193          34 :         as2js::JSON::pointer_t load_json(new as2js::JSON);
     194          34 :         as2js::JSON::JSONValue::pointer_t loaded_value(load_json->load(filename));
     195          17 :         if(loaded_value)
     196             :         {
     197          17 :             as2js::FileInput::pointer_t in(new as2js::FileInput());
     198          17 :             if(in->open(filename))
     199             :             {
     200          17 :                 as2js::FileInput::char_t c(0);
     201       37437 :                 while(c >= 0)
     202             :                 {
     203             :                     // read one line of JSON
     204       37403 :                     as2js::String str;
     205       74806 :                     as2js::String indent;
     206             :                     for(;;)
     207             :                     {
     208     1606961 :                         c = in->getc();
     209     1606961 :                         if(c < 0 || c == '\n')
     210             :                         {
     211             :                             break;
     212             :                         }
     213     1569558 :                         if((c == ' ' || c == '\t') && str.empty())
     214             :                         {
     215             :                             // left trim
     216     1129629 :                             indent += c;
     217     1129629 :                             continue;
     218             :                         }
     219      439929 :                         if(str.empty() && c == '/')
     220             :                         {
     221        1547 :                             c = in->getc();
     222        1547 :                             if(c == '/')
     223             :                             {
     224             :                                 // skip comments
     225        1547 :                                 str += "/";
     226       33939 :                                 do
     227             :                                 {
     228       33939 :                                     str += c;
     229       33939 :                                     c = in->getc();
     230             :                                 }
     231       33939 :                                 while(c > 0 && c != '\n');
     232             :                                 // keep the comments, but not inside the JSON strings
     233        1547 :                                 out->write(indent);
     234        1547 :                                 out->write(str);
     235        1547 :                                 if(str[str.length() - 1] == '\\')
     236             :                                 {
     237             :                                     // we add a $ when str ends with a '\'
     238          19 :                                     out->write("$");
     239             :                                 }
     240        1547 :                                 out->write("\n");
     241        1547 :                                 indent.clear();
     242        1547 :                                 str.clear();
     243        1547 :                                 continue;
     244             :                             }
     245             :                         }
     246      438382 :                         if(c == '"')
     247             :                         {
     248             :                             // add 1 '\' characters in front of the '"'
     249       57518 :                             str += "\\\"";
     250             :                         }
     251      380864 :                         else if(c == '\\')
     252             :                         {
     253             :                             // add 2 '\' character for each '\'
     254        1629 :                             str += "\\\\";
     255             :                         }
     256             :                         else
     257             :                         {
     258      379235 :                             str += c;
     259             :                         }
     260             :                     }
     261       37403 :                     if(!str.empty())
     262             :                     {
     263             :                         // if string ends with "\" then we need to add a "\n"
     264       36789 :                         if(str[str.length() - 1] == '\\')
     265             :                         {
     266         457 :                             str += "\\n";
     267             :                         }
     268             :                         // output this one line as a C++ string
     269       36789 :                         out->write(indent);
     270       36789 :                         out->write("\"");
     271       36789 :                         out->write(str);
     272       36789 :                         out->write("\"\n");
     273             :                     }
     274       37403 :                 }
     275             :             }
     276             :             else
     277             :             {
     278           0 :                 as2js::Message err_msg(as2js::message_level_t::MESSAGE_LEVEL_FATAL, as2js::err_code_t::AS_ERR_CANNOT_COMPILE, loaded_value->get_position());
     279           0 :                 err_msg << "could not re-open this JSON input file \"" << filename << "\".";
     280           0 :                 err = 1;
     281          17 :             }
     282             :         }
     283             :         else
     284             :         {
     285           0 :             err = 1;
     286             :         }
     287          17 :     }
     288             : 
     289          17 :     if(err == 1)
     290             :     {
     291             :         // on error make sure to delete because otherwise cmake thinks
     292             :         // that the target is all good.
     293           0 :         unlink(opt.get_string("output").c_str());
     294             :     }
     295             : 
     296          34 :     return err;
     297          51 : }
     298             : 
     299             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.10