src/terralib/binding/v8/common/Utils.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file Utils.cpp
22 
23  \brief Utility functions for dealing with JNI.
24  */
25 
26 // TerraLib
27 #include "../../../common/Exception.h"
28 #include "../../../core/encoding/CharEncoding.h"
29 #include "../../../core/translator/Translator.h"
30 #include "Utils.h"
31 
32 // STL
33 #include <cassert>
34 #include <cstdio>
35 
36 ::v8::Handle<::v8::String> te::v8::common::ReadFile(const std::string& fileName)
37 {
38  FILE* file = fopen(te::core::CharEncoding::fromUTF8(fileName).c_str(), "rb");
39 
40  if(file == 0)
41  throw te::common::Exception(TR_V8COMMON("Could not read the informed file"));
42 
43  fseek(file, 0, SEEK_END);
44 
45  std::size_t fileSize = ftell(file);
46 
47  rewind(file);
48 
49  char* buff = new char[fileSize + 1]; // add room for trailing '\0'
50 
51  buff[fileSize] = '\0';
52 
53  std::size_t read = 0;
54 
55  for(std::size_t i = 0; i < fileSize; i += read)
56  read = fread(&buff[i], sizeof(char), fileSize - i, file); // we need to check the return value
57 
58  fclose(file);
59 
60  ::v8::Handle<::v8::String> result = ::v8::String::New(buff, static_cast<int>(fileSize));
61 
62  delete [] buff;
63 
64  return result;
65 }
66 
67 void te::v8::common::Cache(::v8::Local<::v8::Object>& obj, const std::string& methodName, ::v8::Persistent<::v8::Function>& outFtor)
68 {
69  ::v8::Local<::v8::String> jsMethodName = ::v8::String::New(methodName.c_str());
70 
71  ::v8::Local<::v8::Value> jsMethod = obj->Get(jsMethodName);
72 
73  if(jsMethod.IsEmpty())
74  throw te::common::Exception(TR_V8COMMON("Could not find the informed method in the JavaScript object!"));
75 
76  if(!jsMethod->IsFunction())
77  throw te::common::Exception(TR_V8COMMON("A method from a JavaScript object must be a function!"));
78 
79  ::v8::Handle<::v8::Function> jsMethodFtor = ::v8::Handle<::v8::Function>::Cast(jsMethod);
80 
81  outFtor = ::v8::Persistent<::v8::Function>::New(jsMethodFtor);
82 }
83 
84 ::v8::Handle<::v8::Value> te::v8::common::Print(const ::v8::Arguments& args)
85 {
86  ::v8::HandleScope hs;
87 
88  bool first = true;
89 
90  for(int i = 0; i < args.Length(); ++i)
91  {
92  if(first)
93  first = false;
94  else
95  printf(" ");
96 
97  ::v8::String::Utf8Value str(args[i]);
98 
99  const char* cstr = *str;
100 
101  printf("%s", cstr);
102  }
103 
104  printf("\n");
105 
106  fflush(stdout);
107 
108  return ::v8::Undefined();
109 }
110 
TEV8COMMONEXPORT::v8::Handle<::v8::String > ReadFile(const std::string &fileName)
It reads a file into a v8 string.
static std::string fromUTF8(const std::string &src)
Convert a string in UTF-8 to the current locale encoding.
TEV8COMMONEXPORT void Cache(::v8::Local<::v8::Object > &obj, const std::string &methodName,::v8::Persistent<::v8::Function > &outFtor)
It caches the object method into the persistent output functor.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
TEV8COMMONEXPORT::v8::Handle<::v8::Value > Print(const ::v8::Arguments &args)
This function will print all the arguments to the standard output.
#define TR_V8COMMON(message)
It marks a string in order to get translated. This is a special mark used in the Vector Geometry modu...
file(WRITE ${CMAKE_BINARY_DIR}/config_qhelp.cmake"configure_file (${TERRALIB_ABSOLUTE_ROOT_DIR}/doc/qhelp/help.qhcp.in ${CMAKE_BINARY_DIR}/share/terraview/help/help.qhcp @ONLY)") add_custom_command(OUTPUT del_dir COMMAND $