Merge "Size negotiation example" 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 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 = std::string::npos;
52
53     idx = arg.find( ".json" );
54     if( idx != std::string::npos )
55     {
56       jSONFileName = arg;
57     }
58     else
59     {
60       idx = arg.find( ".js" );
61       if( idx != std::string::npos )
62       {
63         javaScriptFileName = arg;
64       }
65     }
66   }
67
68   if( !jSONFileName.empty() )
69   {
70     bool exists = CheckIfFileExists( jSONFileName );
71     if( !exists )
72     {
73       DALI_ASSERT_ALWAYS( 0 && "JSON file not found ")
74     }
75   }
76   if( !javaScriptFileName.empty() )
77   {
78     bool exists = CheckIfFileExists( javaScriptFileName );
79     if( !exists )
80     {
81         DALI_ASSERT_ALWAYS( 0 && "JavaScript file not found ")
82     }
83   }
84   if( jSONFileName.empty() && javaScriptFileName.empty() )
85   {
86     printf("Please specify a JSON and/or JavaScript file to load, e.g. scripting.example mylayout.json my-test.js\n");
87     return -1;
88   }
89
90
91   Launcher daliApplication( Dali::Application::New( &argc, &argv ), jSONFileName, javaScriptFileName );
92
93   daliApplication.MainLoop();
94
95   return 0;
96 }