Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-demo.git] / examples / scripting / scripting-example.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // EXTERNAL INCLUDES
19 #include <string>
20 #include <stdio.h>
21 #include <sys/stat.h>
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include "launcher.h"
26
27 namespace
28 {
29 bool CheckIfFileExists( const std::string& filename )
30 {
31   struct stat buf;
32   // fstat returns -1 on error
33   if (stat( filename.c_str(), &buf) != -1)
34   {
35       return true;
36   }
37   return false;
38 }
39
40 }
41 int DALI_EXPORT_API main( int argc, char* argv[] )
42 {
43   // pull out the JSON file and JavaScript file from the command line arguments
44   std::string javaScriptFileName;
45   std::string jSONFileName;
46
47   for( int i = 1 ; i < argc ; ++i )
48   {
49     std::string arg( argv[i] );
50
51     size_t idx = arg.find( ".json" );
52     if( idx != std::string::npos )
53     {
54       jSONFileName = arg;
55     }
56     else
57     {
58       idx = arg.find( ".js" );
59       if( idx != std::string::npos )
60       {
61         javaScriptFileName = arg;
62       }
63     }
64   }
65
66   if( !jSONFileName.empty() )
67   {
68     bool exists = CheckIfFileExists( jSONFileName );
69     if( !exists )
70     {
71       DALI_ASSERT_ALWAYS( 0 && "JSON file not found ")
72     }
73   }
74   if( !javaScriptFileName.empty() )
75   {
76     bool exists = CheckIfFileExists( javaScriptFileName );
77     if( !exists )
78     {
79         DALI_ASSERT_ALWAYS( 0 && "JavaScript file not found ")
80     }
81   }
82   if( jSONFileName.empty() && javaScriptFileName.empty() )
83   {
84     printf("Please specify a JSON and/or JavaScript file to load, e.g. scripting.example mylayout.json my-test.js\n");
85     return -1;
86   }
87
88
89   Launcher daliApplication( Dali::Application::New( &argc, &argv, DEMO_THEME_PATH ), jSONFileName, javaScriptFileName );
90
91   daliApplication.MainLoop();
92
93   return 0;
94 }