From 8f1be1812d25db3e3b18356641361bbf8e0b67f8 Mon Sep 17 00:00:00 2001 From: Rodrigo Avancini Date: Wed, 26 Sep 2018 11:17:20 -0300 Subject: [PATCH 1/7] Close #2241 - Change terrame-examples default directory. --- src/lua/zerobrane.lua | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/lua/zerobrane.lua b/src/lua/zerobrane.lua index 8267f99d..aa237844 100644 --- a/src/lua/zerobrane.lua +++ b/src/lua/zerobrane.lua @@ -42,20 +42,19 @@ local home if info_.system == "mac" then ide = Directory(info_.path.."../ide/zerobrane") home = os.getenv("HOME") - path = home zbpreferencespath = os.getenv("HOME").."/Library/Preferences/ZeroBraneStudio Preferences" elseif info_.system == "windows" then ide = Directory(info_.path.."/ide/zerobrane") - home = os.getenv("HOMEDRIVE").."/"..os.getenv("HOMEPATH") - path = os.getenv("appdata") - zbpreferencespath = os.getenv("appdata").."/ZeroBraneStudio.ini" + home = os.getenv("HOMEDRIVE").."\\"..os.getenv("HOMEPATH") + zbpreferencespath = os.getenv("APPDATA").."/ZeroBraneStudio.ini" else ide = Directory(info_.path.."/ide/zerobrane") home = os.getenv("HOME") - path = home zbpreferencespath = os.getenv("HOME").."/.ZeroBraneStudio" end +path = home + local function setZeroBranePreferences() _Gtme.printNote("Updating ZeroBrane preferences") @@ -83,7 +82,8 @@ local function setZeroBranePreferences() while line do local value = string.gsub(line, "PATH", path.."/terrame-examples") if info_.system == "windows" then - value = string.gsub(value, "\\", "\\\\") + value = _Gtme.makePathCompatibleToAllOS(value) + value = string.gsub(value, "/", "\\\\") end table.insert(output, value) line = file:readLine() @@ -334,4 +334,3 @@ _Gtme.configureZeroBrane = function() qt.dialog.msg_critical(msg) end end - From 989e3867fb33218fd441532a3c239d9a307a7603 Mon Sep 17 00:00:00 2001 From: Rodrigo Avancini Date: Wed, 26 Sep 2018 11:21:15 -0300 Subject: [PATCH 2/7] Close #2245 close #2242- Allow latin accents in file and directory names. --- packages/base/lua/Directory.lua | 23 +++-- packages/base/lua/File.lua | 19 ++-- packages/base/lua/Utils.lua | 61 ++++++++++++ .../functional/alternative/Directory.lua | 14 +-- .../tests/functional/alternative/File.lua | 14 +-- .../tests/functional/basics/Directory.lua | 16 ++++ .../base/tests/functional/basics/File.lua | 7 ++ packages/gis/lua/TerraLib.lua | 92 ++++++++++++------- src/core/terrameLua.cpp | 34 +++++-- src/lua/terrame.lua | 9 +- src/observer/types/chartPlot/chartPlot.cpp | 2 +- src/observer/types/observerMap.cpp | 4 +- src/observer/types/observerScheduler.cpp | 2 +- src/observer/types/observerTable.cpp | 4 +- src/observer/types/observerTextScreen.cpp | 2 +- 15 files changed, 212 insertions(+), 91 deletions(-) diff --git a/packages/base/lua/Directory.lua b/packages/base/lua/Directory.lua index b215ae55..7902ff74 100644 --- a/packages/base/lua/Directory.lua +++ b/packages/base/lua/Directory.lua @@ -21,6 +21,12 @@ -- of this software and its documentation. -- ------------------------------------------------------------------------------------------- +local function checkInvalidChars(fullpath) + local invalidChars = string.gsub(fullpath, "[^\34\42\60\62\63\124]", "") + if #invalidChars ~= 0 then + customError("Directory path '"..fullpath.."' contains invalid character '"..invalidChars.."'.") + end +end Directory_ = { type_ = "Directory", @@ -247,11 +253,7 @@ function Directory(data) data.fullpath = data.name data.name = nil - local invalidCharIdx = data.fullpath:find("[*<>?|\"]") - if invalidCharIdx then - local invalidChar = data.fullpath:sub(invalidCharIdx, invalidCharIdx) - customError("Directory path '"..data.fullpath.."' cannot contain character '"..invalidChar.."'.") - end + checkInvalidChars(data.fullpath) if not (data.fullpath:match("\\") or data.fullpath:match("/")) then if data.fullpath == "." then data.fullpath = "" end @@ -261,9 +263,8 @@ function Directory(data) data.fullpath = _Gtme.makePathCompatibleToAllOS(data.fullpath) - local invalidChar = string.gsub(data.fullpath, "[^\33-\44\59-\64\91-\94\96\123-\125\127-\255]", "") - if #invalidChar ~= 0 then - customError("Directory path '"..data.fullpath.."' contains invalid character '"..invalidChar.."'.") + if sessionInfo().system == "windows" then + data.fullpath = replaceLatinCharacters(data.fullpath) end if data.fullpath:sub(-1) == "/" then @@ -280,7 +281,11 @@ function Directory(data) local cmd = runCommand("mktemp -d \""..data.fullpath.."\"")[1] table.insert(_Gtme.tmpdirectory__, data) - data.fullpath = cmd + if sessionInfo().system == "windows" then + data.fullpath = replaceLatinCharacters(cmd) + else + data.fullpath = cmd + end end if isFile(data.fullpath) then diff --git a/packages/base/lua/File.lua b/packages/base/lua/File.lua index fcd01e98..1450d7cf 100644 --- a/packages/base/lua/File.lua +++ b/packages/base/lua/File.lua @@ -74,6 +74,13 @@ local function parseLine(line, sep, cline) return res end +local function checkInvalidChars(filename) + local invalidChars = string.gsub(filename, "[^\34\42\47\58\60\62\63\92\124]", "") + if #invalidChars ~= 0 then + customError("File name '"..filename.."' contains invalid character '"..invalidChars.."'.") + end +end + File_ = { type_ = "File", --- Return a table with the file attributes corresponding to filepath (or nil followed by an error @@ -202,6 +209,7 @@ File_ = { local s = sessionInfo().separator local result = os.execute("rm -f \""..self.filename.."\" 2> "..directory..s.."a.txt") + directory:delete() if result ~= true then @@ -555,15 +563,10 @@ function File(data) customError("Directory '"..dir.."' does not exist.") end - local invalidCharIdx = data.filename:find("[&*<>?|\"]") - if invalidCharIdx then - customError("Filename '"..data.filename.."' cannot contain character '"..data.filename:sub(invalidCharIdx, invalidCharIdx).."'.") - end + checkInvalidChars(data:name()) - local fileName = data:name() - local invalidChar = string.gsub(fileName, "[^\33-\44\58-\64\91-\94\96\123\125\127-\255]", "") - if #invalidChar ~= 0 then - customError("File name '"..fileName.."' contains invalid character '"..invalidChar.."'.") + if sessionInfo().system == "windows" then + data.filename = replaceLatinCharacters(data.filename) end if isDirectory(data.filename) then diff --git a/packages/base/lua/Utils.lua b/packages/base/lua/Utils.lua index 55b4caec..3d4e9ecf 100644 --- a/packages/base/lua/Utils.lua +++ b/packages/base/lua/Utils.lua @@ -1609,3 +1609,64 @@ function vardump(o, indent) return recursiveVardump(o, indent, {}) end +local LatinCharacterMapper = { + [128] = "\xC0", [129] = "\xC1", [130] = "\xC2", + [131] = "\xC3", [132] = "\xC4", [133] = "\xC5", + [134] = "\xC6", [135] = "\xC7", [136] = "\xC8", + [137] = "\xC9", [138] = "\xCA", [139] = "\xCB", + [140] = "\xCC", [141] = "\xCD", [142] = "\xCE", + [143] = "\xCF", [144] = "\xD0", [145] = "\xD1", + [146] = "\xD2", [147] = "\xD3", [148] = "\xD4", + [149] = "\xD5", [150] = "\xD6", [151] = "\xD7", + [152] = "\xD8", [153] = "\xD9", [154] = "\xDA", + [155] = "\xDB", [156] = "\xDC", [157] = "\xDD", + [158] = "\xDE", [159] = "\xDF", [160] = "\xE0", + [161] = "\xE1", [162] = "\xE2", [163] = "\xE3", + [164] = "\xE4", [165] = "\xE5", [166] = "\xE6", + [167] = "\xE7", [168] = "\xE8", [169] = "\xE9", + [170] = "\xEA", [171] = "\xEB", [172] = "\xEC", + [173] = "\xED", [174] = "\xEE", [175] = "\xEF", + [176] = "\xF0", [177] = "\xF1", [178] = "\xF2", + [179] = "\xF3", [180] = "\xF4", [181] = "\xF5", + [182] = "\xF6", [183] = "\xF7", [184] = "\xF8", + [185] = "\xF9", [186] = "\xFA", [187] = "\xFB", + [188] = "\xFC", [189] = "\xFD", [190] = "\xFE", + [191] = "\xFF" +} + +local function hasLatinCharacters(str) + local latinChars = string.gsub(str, "[^\192-\255]", "") + return #latinChars > 0 +end + +--- This function converts latin characters to hexadecimal code by the characters bytes. +-- It is necessary to Windows OS works correctly with latin characters. +-- Also, it is necessary to set Lua locale as the same as the system locale. +-- See os.setlocale() for more details. +-- The list of unicode characters can be found in +-- https://en.wikipedia.org/wiki/List_of_Unicode_characters +-- @arg str A string. +-- @usage --DONTRUN +-- print(replaceLatinCharacters(águia)) +-- > \xE1guia +function replaceLatinCharacters(str) + if not hasLatinCharacters(str) then + return str + end + + local rep = "" + + for c in string.gmatch(str, ".") do + local b = string.byte(c, 1, -1) + if b ~= 195 then + if LatinCharacterMapper[b] then + rep = rep..LatinCharacterMapper[b] + else + rep = rep..c + end + end + end + + return rep +end + diff --git a/packages/base/tests/functional/alternative/Directory.lua b/packages/base/tests/functional/alternative/Directory.lua index 4d81edfb..f5f10d34 100644 --- a/packages/base/tests/functional/alternative/Directory.lua +++ b/packages/base/tests/functional/alternative/Directory.lua @@ -40,7 +40,7 @@ return{ Directory("abc\"") end - unitTest:assertError(error_func, "Directory path 'abc\"' cannot contain character '\"'.") + unitTest:assertError(error_func, "Directory path 'abc\"' contains invalid character '\"'.") error_func = function() Directory{} @@ -58,18 +58,6 @@ return{ Directory(packageInfo("base").path.."data/agents.csv") end unitTest:assertError(error_func, "'agents.csv' is a file, and not a directory.", 0, true) - - local invalidCharError = function() - Directory("C:/Usuários") - end - - unitTest:assertError(invalidCharError, "Directory path 'C:/Usuários' contains invalid character 'á'.") - - invalidCharError = function() - Directory("C:/Usuários/Ação") - end - - unitTest:assertError(invalidCharError, "Directory path 'C:/Usuários/Ação' contains invalid character 'áçã'.") end, attributes = function(unitTest) local dir = Directory("/my/path/my_dir") diff --git a/packages/base/tests/functional/alternative/File.lua b/packages/base/tests/functional/alternative/File.lua index 6557d2cc..a3cfd14a 100644 --- a/packages/base/tests/functional/alternative/File.lua +++ b/packages/base/tests/functional/alternative/File.lua @@ -50,31 +50,25 @@ return{ local path = packageInfo("base").data - local filename = path.."file*" + local filename = "file*" error_func = function() File(filename) end - unitTest:assertError(error_func, "Filename '"..filename.."' cannot contain character '*'.") + unitTest:assertError(error_func, "File name '"..filename.."' contains invalid character '*'.") - filename = path.."file\"" + filename = "file\"" error_func = function() File(filename) end - unitTest:assertError(error_func, "Filename '"..filename.."' cannot contain character '\"'.") + unitTest:assertError(error_func, "File name '"..filename.."' contains invalid character '\"'.") error_func = function() File(tostring(packageInfo("base").path)) end unitTest:assertError(error_func, "'base' is a directory, and not a file.", 0, true) - - local invalidChar = function() - File("ação.txt") - end - - unitTest:assertError(invalidChar, "File name 'ação.txt' contains invalid character 'çã'.") end, attributes = function(unitTest) local file = filePath("agents.csv", "base") diff --git a/packages/base/tests/functional/basics/Directory.lua b/packages/base/tests/functional/basics/Directory.lua index 19b1c70e..172406af 100644 --- a/packages/base/tests/functional/basics/Directory.lua +++ b/packages/base/tests/functional/basics/Directory.lua @@ -106,6 +106,22 @@ return{ unitTest:assert(blankSpaceDir:create()) unitTest:assert(blankSpaceDir:exists()) unitTest:assert(blankSpaceDir:delete()) + + local validCharacterInDirName = function() + local d = Directory("dir++") + unitTest:assert(d:create()) + unitTest:assert(d:delete()) + end + + unitTest:assert(validCharacterInDirName) + + local latinCharacter = function() + local d = Directory("Ação") + unitTest:assert(d:create()) + unitTest:assert(d:delete()) + end + + unitTest:assert(latinCharacter) end, delete = function(unitTest) local dir = Directory("test_dir_delete") diff --git a/packages/base/tests/functional/basics/File.lua b/packages/base/tests/functional/basics/File.lua index dbe8f9f5..ff308e46 100644 --- a/packages/base/tests/functional/basics/File.lua +++ b/packages/base/tests/functional/basics/File.lua @@ -26,6 +26,13 @@ return{ File = function(unitTest) local file = File("abc.txt") unitTest:assertType(file, "File") + + file = File("ação.txt") + if sessionInfo().system == "windows" then + unitTest:assert(tostring(file) ~= currentDir().."ação.txt") -- SKIP + end + local f2 = File("ação.txt") + unitTest:assertEquals(tostring(file), tostring(f2)) end, __concat = function(unitTest) local f = File("abcd1234") diff --git a/packages/gis/lua/TerraLib.lua b/packages/gis/lua/TerraLib.lua index ff496b20..4b9b74ca 100644 --- a/packages/gis/lua/TerraLib.lua +++ b/packages/gis/lua/TerraLib.lua @@ -306,6 +306,14 @@ local function addSpatialIndex(ds, dSetName) ds:execute("CREATE SPATIAL INDEX ON "..dSetName) end +local function toUtf8(str) + if sessionInfo().system == "windows" then + return binding.CharEncoding.toUTF8(str, binding.CharEncoding.getEncodingType("LATIN1")) + end + + return binding.CharEncoding.toUTF8(str) +end + local function createLayer(name, dSetName, connInfo, type, addSpatialIdx, srid, encoding) local layer local sridReal = 0 @@ -327,8 +335,9 @@ local function createLayer(name, dSetName, connInfo, type, addSpatialIdx, srid, if type == "WMS2" then local uri = binding.te.core.URI(connInfo) - local infos = binding.Expand(uri:query()) - local client = binding.te.ws.ogc.WMSClient(infos.USERDATADIR, infos.URI, infos.VERSION) + local info = binding.Expand(uri:query()) + info.USERDATADIR = binding.URIDecode(info.USERDATADIR) + local client = binding.te.ws.ogc.WMSClient(toUtf8(info.USERDATADIR), info.URI, info.VERSION) client:updateCapabilities() local capblts = client:getCapabilities() local rootLayer = capblts.m_capability.m_layer @@ -342,8 +351,8 @@ local function createLayer(name, dSetName, connInfo, type, addSpatialIdx, srid, local request = wmsLayer:createMapRequest() - if infos.FORMAT then - request.m_format = "image/"..infos.FORMAT + if info.FORMAT then + request.m_format = "image/"..info.FORMAT end local bbox = request.m_boundingBox @@ -459,7 +468,7 @@ local function saveProject(project, layers) i = i + 1 end - binding.SaveProject(file, project.author, project.title, layersVector) + binding.SaveProject(toUtf8(file), project.author, project.title, layersVector) end local function loadProject(project, file) @@ -473,7 +482,7 @@ local function loadProject(project, file) file = currentDir()..fileName..".tview" end - local projMd = binding.LoadProject(tostring(file)) + local projMd = binding.LoadProject(toUtf8(tostring(file))) project.author = projMd.author project.title = projMd.title local layers = projMd:getLayers() @@ -968,6 +977,15 @@ local function removeDataSource(project, dsId) end end +local function fixSpaceInPath(path) + return string.gsub(path, "%%20", " ") +end + +local function getFileByUri(uri) + local path = binding.URIDecode(uri:path()) + return File(fixSpaceInPath(uri:host()..path)) +end + local function removeLayer(project, layerName) do loadProject(project, project.file) @@ -979,7 +997,16 @@ local function removeLayer(project, layerName) local id = layer:getDataSourceId() local dsInfo = binding.te.da.DataSourceInfoManager.getInstance():getDsInfo(id) local dsetName = layer:getDataSetName() - local ds = makeAndOpenDataSource(dsInfo:getConnInfo(), dsInfo:getType()) + local connInfo = dsInfo:getConnInfo() + local dsType = dsInfo:getType() + + if (dsType == "OGR") or (dsType == "GDAL") then + local file = getFileByUri(connInfo) + connInfo = createFileConnInfo(tostring(file)) + connInfo = toUtf8(connInfo) + end + + local ds = makeAndOpenDataSource(connInfo, dsType) ds:dropDataSet(dsetName) removeDataSource(project, id) @@ -1099,12 +1126,8 @@ local function getLayerByDataSetName(layers, dsetName, type) return nil end -local function fixSpaceInPath(path) - return string.gsub(path, "%%20", " ") -end - local function swapFileConnInfo(connInfo, fileName) - local file = File(fixSpaceInPath(connInfo:host()..connInfo:path())) + local file = getFileByUri(connInfo) local outFile = file:path()..fileName.."."..file:extension() return createFileConnInfo(outFile) end @@ -1331,6 +1354,9 @@ local function createDataSetFromLayer(fromLayer, toSetName, toSet, attrs) if not errorMsg then local toConnInfo = createConnInfoToSave(connInfo, toSetName, fromType) + if type(toConnInfo) == "string" then + toConnInfo = toUtf8(toConnInfo) + end local toDs = makeAndOpenDataSource(toConnInfo, fromType) -- Drop if exists @@ -1357,9 +1383,10 @@ local function updateDataSet(fromLayer, toSet, attrs) local errorMsg do local dsInfo = binding.te.da.DataSourceInfoManager.getInstance():getDsInfo(fromLayer:getDataSourceId()) - local connInfo = dsInfo:getConnInfo() + local connInfo = dsInfo:getConnInfoAsString() + connInfo = binding.URIDecode(connInfo) local fromType = dsInfo:getType() - local ds = makeAndOpenDataSource(connInfo, fromType) + local ds = makeAndOpenDataSource(toUtf8(connInfo), fromType) local dsetName = fromLayer:getDataSetName() local dse = ds:getDataSet(dsetName) @@ -2034,6 +2061,11 @@ TerraLib_ = { -- layerInfo = TerraLib().getLayerInfo(proj, "SampaPg") getLayerInfo = function(project, layerName) local layer = project.layers[layerName] + + if not layer then + customError("Layer '"..layerName.."' doesn't exist.") + end + local info = {} info.name = layer:getTitle() info.srid = layer:getSRID() @@ -2055,13 +2087,9 @@ TerraLib_ = { info.database = string.gsub(connInfo:path(), "/", "") info.table = dseName info.source = "postgis" - elseif type == "OGR" then - info.file = fixSpaceInPath(connInfo:host()..connInfo:path()) - local file = File(info.file) - info.source = file:extension() - elseif type == "GDAL" then - info.file = fixSpaceInPath(connInfo:host()..connInfo:path()) - local file = File(info.file) + elseif (type == "OGR") or (type == "GDAL") then + local file = getFileByUri(connInfo) + info.file = tostring(file) info.source = file:extension() elseif type == "WFS" then info.url = connInfo:path() @@ -2341,6 +2369,7 @@ TerraLib_ = { createCellSpaceLayer(inputLayer, name, dSetName, resolution, connInfo, "OGR", mask) local encoding = binding.CharEncoding.getEncodingName(inputLayer:getEncoding()) + instance.addShpLayer(project, name, file, addSpatialIdx, srid, encoding) end, --- Add a new cellular layer to a PostgreSQL connection. @@ -2546,7 +2575,7 @@ TerraLib_ = { local outFileExt if outType == "OGR" then - outFileExt = string.lower(File(fixSpaceInPath(outConnInfo:host()..outConnInfo:path())):extension()) + outFileExt = string.lower(getFileByUri(outConnInfo):extension()) if (string.len(property) > 10) and (outFileExt == "shp") then property = getNormalizedName(property) @@ -2608,9 +2637,11 @@ TerraLib_ = { outDSetName = string.lower(outDSetName) outSpatialIdx = false -- TODO(#1678) elseif outType == "OGR" then - local file = File(fixSpaceInPath(outConnInfo:host()..outConnInfo:path())) + local file = getFileByUri(outConnInfo) local outDir = _Gtme.makePathCompatibleToAllOS(file:path()) - outConnInfo = binding.te.core.URI(createFileConnInfo(outDir..out.."."..file:extension())) + local outFileUri = createFileConnInfo(outDir..out.."."..file:extension()) + outFileUri = toUtf8(outFileUri) + outConnInfo = binding.te.core.URI(outFileUri) outSpatialIdx = true end @@ -2637,6 +2668,7 @@ TerraLib_ = { end outDs = makeAndOpenDataSource(outConnInfo, outType) + local attrsRenamed = {} if operation == "coverage" then @@ -2673,15 +2705,16 @@ TerraLib_ = { local toSetName = nil if toType == "OGR" then - local _, name = File(fixSpaceInPath(toConnInfo:host()..toConnInfo:path())):split() + local _, name = getFileByUri(toConnInfo):split() toSetName = name end outDs:close() if outFileExt == "geojson" then -- TODO(#2224) - local toFile = File(fixSpaceInPath(toConnInfo:host()..toConnInfo:path())):deleteIfExists() - os.execute("mv "..fixSpaceInPath(outConnInfo:host()..outConnInfo:path()).." "..toFile) -- SKIP + local toFile = getFileByUri(toConnInfo):deleteIfExists() + local outFile = getFileByUri(outConnInfo) + os.execute("mv "..outFile.." "..toFile) -- SKIP else overwriteLayer(project, out, to, toSetName, default) end @@ -2715,12 +2748,10 @@ TerraLib_ = { loadProject(project, project.file) local layer = project.layers[layerName] - --layer = castLayer(layer) local dseName = layer:getDataSetName() local dsInfo = binding.te.da.DataSourceInfoManager.getInstance():getDsInfo(layer:getDataSourceId()) local ds = makeAndOpenDataSource(dsInfo:getConnInfo(), dsInfo:getType()) local dse = ds:getDataSet(dseName) - --local dse = getDataSetFromLayer(project, layerName) set, err = createDataSetAdapted(dse, missing) addCache(set, project, layerName, missing) @@ -2793,16 +2824,13 @@ TerraLib_ = { end else createDataSetFromLayer(fromLayer, toSetName, toSet, attrs) - local toConnInfo = createConnInfoToSave(connInfo, toSetName, fromType) - local toLayer if dseName == "OGRGeoJSON" then toLayer = createLayer(toLayerName, dseName, toConnInfo, fromType, addSpatialIdx, fromLayer:getSRID()) else toLayer = createLayer(toLayerName, toSetName, toConnInfo, fromType, addSpatialIdx, fromLayer:getSRID()) end - project.layers[toLayerName] = toLayer saveProject(project, project.layers) end diff --git a/src/core/terrameLua.cpp b/src/core/terrameLua.cpp index f7a79364..e194817c 100644 --- a/src/core/terrameLua.cpp +++ b/src/core/terrameLua.cpp @@ -170,19 +170,19 @@ void registerClasses() int cpp_runcommand(lua_State *L) { const char* command = lua_tostring(L, -1); - + QString cmd = QString::fromLocal8Bit(command); QProcess process; - process.start(command); + process.start(cmd); process.waitForFinished(-1); // will wait forever until finished - QString out = process.readAllStandardOutput(); - QString err = process.readAllStandardError(); + QString out = QString::fromLocal8Bit(process.readAllStandardOutput()); + QString err = QString::fromLocal8Bit(process.readAllStandardError()); out.remove(QRegExp("[\\r]")); err.remove(QRegExp("[\\r]")); - lua_pushstring(L, out.toLatin1().constData()); - lua_pushstring(L, err.toLatin1().constData()); + lua_pushstring(L, out.toStdString().data()); + lua_pushstring(L, err.toStdString().data()); return 2; } @@ -219,8 +219,11 @@ int cpp_imagecompare(lua_State *L) { const char* s1 = lua_tostring(L, -1); const char* s2 = lua_tostring(L, -2); + + QString f1(QString::fromLocal8Bit(s1)); + QString f2(QString::fromLocal8Bit(s2)); - double result = comparePerPixel(s1, s2); + double result = comparePerPixel(f1, f2); lua_pushnumber(L, result); return 1; @@ -308,16 +311,24 @@ int cpp_putenv(lua_State* L) int cpp_getOsName(lua_State* L) { #ifdef _WIN64 - lua_pushstring(L, "WINDOWS"); + lua_pushstring(L, "windows"); #elif __APPLE__ - lua_pushstring(L, "MAC"); + lua_pushstring(L, "mac"); #elif __linux__ - lua_pushstring(L, "LINUX"); + lua_pushstring(L, "linux"); #endif return 1; } +int cpp_getLocale(lua_State* L) +{ + std::locale loc(""); + lua_pushstring(L, loc.name().c_str()); + + return 1; +} + extern ExecutionModes execModes; int main(int argc, char *argv[]) @@ -430,6 +441,9 @@ int main(int argc, char *argv[]) lua_pushcfunction(L, cpp_getOsName); lua_setglobal(L, "cpp_getOsName"); + lua_pushcfunction(L, cpp_getLocale); + lua_setglobal(L, "cpp_getLocale"); + // Execute the lua files if (argc < 2) { diff --git a/src/lua/terrame.lua b/src/lua/terrame.lua index 041a338a..1ee8de84 100644 --- a/src/lua/terrame.lua +++ b/src/lua/terrame.lua @@ -1136,6 +1136,13 @@ function _Gtme.execConfigure(model, packageName) end function _Gtme.execute(arguments) -- 'arguments' is a vector of strings + local osName = cpp_getOsName() + if osName == "windows" then + local locale = cpp_getLocale() + os.setlocale(locale, "collate") + os.setlocale(locale, "ctype") + end + info_ = { -- this variable is used by Utils:sessionInfo() mode = "normal", dbVersion = "1_3_1", @@ -1148,7 +1155,7 @@ function _Gtme.execute(arguments) -- 'arguments' is a vector of strings fullTraceback = false, autoclose = false, time = os.clock(), - system = string.lower(cpp_getOsName()), + system = osName, round = 1e-5 } diff --git a/src/observer/types/chartPlot/chartPlot.cpp b/src/observer/types/chartPlot/chartPlot.cpp index 6f2c35e9..d285b030 100644 --- a/src/observer/types/chartPlot/chartPlot.cpp +++ b/src/observer/types/chartPlot/chartPlot.cpp @@ -107,7 +107,7 @@ void ChartPlot::mouseDoubleClickEvent(QMouseEvent *ev) void ChartPlot::exportChart(std::string file, string extension) { QPixmap pixmap = grab(); - pixmap.save(file.c_str(), extension.c_str()); + pixmap.save(QString::fromLocal8Bit(file.c_str()), extension.c_str()); } void ChartPlot::setInternalCurves(const QList &interCurves) diff --git a/src/observer/types/observerMap.cpp b/src/observer/types/observerMap.cpp index 760ddeca..dc1b1f18 100644 --- a/src/observer/types/observerMap.cpp +++ b/src/observer/types/observerMap.cpp @@ -117,10 +117,8 @@ const TypesOfObservers ObserverMap::getType() void ObserverMap::save(string f, string e) { - //QDataStream d; - //draw(d); QPixmap pixmap = painterWidget->grab(); - pixmap.save(f.c_str(), e.c_str()); + pixmap.save(QString::fromLocal8Bit(f.c_str()), e.c_str()); } bool ObserverMap::draw(QDataStream &state) diff --git a/src/observer/types/observerScheduler.cpp b/src/observer/types/observerScheduler.cpp index 92c364c2..2ada2ef5 100644 --- a/src/observer/types/observerScheduler.cpp +++ b/src/observer/types/observerScheduler.cpp @@ -317,7 +317,7 @@ void ObserverScheduler::closeEvent(QCloseEvent *event) void ObserverScheduler::save(std::string file, std::string extension) { QPixmap pixmap = grab(); - pixmap.save(file.c_str(), extension.c_str()); + pixmap.save(QString::fromLocal8Bit(file.c_str()), extension.c_str()); } diff --git a/src/observer/types/observerTable.cpp b/src/observer/types/observerTable.cpp index 6ecfb49f..f8902d5b 100644 --- a/src/observer/types/observerTable.cpp +++ b/src/observer/types/observerTable.cpp @@ -215,6 +215,6 @@ void ObserverTable::saveAsImage(std::string file, std::string extension) { raise(); activateWindow(); - QPixmap pixmap = grab(); - pixmap.save(file.c_str(), extension.c_str()); + QPixmap pixmap = grab(); + pixmap.save(QString::fromLocal8Bit(file.c_str()), extension.c_str()); } diff --git a/src/observer/types/observerTextScreen.cpp b/src/observer/types/observerTextScreen.cpp index cc0c5d79..b6eb0fcc 100644 --- a/src/observer/types/observerTextScreen.cpp +++ b/src/observer/types/observerTextScreen.cpp @@ -220,6 +220,6 @@ void ObserverTextScreen::save(std::string file, std::string extension) void ObserverTextScreen::saveAsImage(std::string file, std::string extension) { QPixmap pixmap = textEdit->grab(); - pixmap.save(file.c_str(), extension.c_str()); + pixmap.save(QString::fromLocal8Bit(file.c_str()), extension.c_str()); } From 6724777e013fe87541163189e225859d731be0a3 Mon Sep 17 00:00:00 2001 From: Rodrigo Avancini Date: Wed, 26 Sep 2018 11:22:08 -0300 Subject: [PATCH 3/7] Some corrections. --- .../tests/functional/alternative/TerraLib.lua | 18 +++++--- .../gis/tests/functional/basic/TerraLib.lua | 41 +++++++++++-------- src/core/luaTextScreen.h | 2 +- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/packages/gis/tests/functional/alternative/TerraLib.lua b/packages/gis/tests/functional/alternative/TerraLib.lua index a197089a..aaf66222 100644 --- a/packages/gis/tests/functional/alternative/TerraLib.lua +++ b/packages/gis/tests/functional/alternative/TerraLib.lua @@ -302,18 +302,26 @@ return { proj.file:delete() end, removeLayer = function(unitTest) - local proj = Project{ - file = "test.tview", - clean = true, - inputLayer = filePath("itaituba-localities.shp", "gis") + local proj = { + file = "removelayer_alt_func.tview", + title = "TerraLib Tests", + author = "Avancini Rodrigo" } + File(proj.file):deleteIfExists() + TerraLib().createProject(proj, {}) + + local layerName = "inputLayer" + local layerFile = filePath("itaituba-localities.shp", "gis") + TerraLib().addShpLayer(proj, layerName, layerFile) + local layerNotFound = function() TerraLib().removeLayer(proj, "xxxx") end unitTest:assertError(layerNotFound, "Layer 'xxxx' not found.") - File("test.tview"):deleteIfExists() + + proj.file:delete() end, getDataSet = function(unitTest) local proj = { diff --git a/packages/gis/tests/functional/basic/TerraLib.lua b/packages/gis/tests/functional/basic/TerraLib.lua index 014766f9..0cc9e2a2 100644 --- a/packages/gis/tests/functional/basic/TerraLib.lua +++ b/packages/gis/tests/functional/basic/TerraLib.lua @@ -331,27 +331,36 @@ return { unitTest:assert(true) end, removeLayer = function(unitTest) - local proj = Project{ - file = "test.tview", - clean = true, - inputLayer = filePath("itaituba-localities.shp", "gis") + local proj = { + file = "removelayer_basic_func.tview", + title = "TerraLib Tests", + author = "Avancini Rodrigo" } - local layer = Layer{ - input = "inputLayer", - project = proj, - file = "test.shp", - name = "testLayer", - resolution = 30000, - source = "shp" - } + File(proj.file):deleteIfExists() + TerraLib().createProject(proj, {}) - unitTest:assertNotNil(proj.testLayer) + local layerName = "inputLayer" + local layerFile = filePath("itaituba-localities.shp", "gis") + TerraLib().addShpLayer(proj, layerName, layerFile) + + local clName = "testLayer" + local cellsShp = File("test.shp") + local resolution = 30000 + local mask = true + cellsShp:deleteIfExists() + TerraLib().addShpCellSpaceLayer(proj, layerName, clName, resolution, cellsShp, mask) + + local info = TerraLib().getLayerInfo(proj, clName) unitTest:assertNotNil(proj.layers.testLayer) - TerraLib().removeLayer(proj, layer.name) - unitTest:assertNil(proj.testLayer) + unitTest:assertEquals(info.name, clName) + unitTest:assertEquals(tostring(info.file), tostring(cellsShp)) + + TerraLib().removeLayer(proj, clName) + unitTest:assertNil(proj.layers.testLayer) - File("test.tview"):deleteIfExists() + + proj.file:delete() end } diff --git a/src/core/luaTextScreen.h b/src/core/luaTextScreen.h index 53c48558..d698b4ae 100644 --- a/src/core/luaTextScreen.h +++ b/src/core/luaTextScreen.h @@ -52,7 +52,7 @@ class luaTextScreen : public Reference int setObserver(lua_State* L); /// destructor - ~luaTextScreen(void); + ~luaTextScreen(void); int save(lua_State* L); From af2a9b7301886de18d48e4218aae916b7e60e078 Mon Sep 17 00:00:00 2001 From: Rodrigo Avancini Date: Wed, 26 Sep 2018 17:17:50 -0300 Subject: [PATCH 4/7] Corrections. --- packages/base/lua/Utils.lua | 8 ++++---- packages/base/tests/functional/alternative/File.lua | 2 -- packages/base/tests/functional/basics/Package.lua | 2 +- packages/gis/tests/functional/alternative/Project.lua | 2 +- src/core/terrameLua.cpp | 4 ++-- 5 files changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/base/lua/Utils.lua b/packages/base/lua/Utils.lua index 3d4e9ecf..c011a383 100644 --- a/packages/base/lua/Utils.lua +++ b/packages/base/lua/Utils.lua @@ -1642,13 +1642,13 @@ end --- This function converts latin characters to hexadecimal code by the characters bytes. -- It is necessary to Windows OS works correctly with latin characters. -- Also, it is necessary to set Lua locale as the same as the system locale. --- See os.setlocale() for more details. +-- See os.setlocale() for more details. -- The list of unicode characters can be found in --- https://en.wikipedia.org/wiki/List_of_Unicode_characters +-- https://en.wikipedia.org/wiki/List_of_Unicode_characters. -- @arg str A string. -- @usage --DONTRUN --- print(replaceLatinCharacters(águia)) --- > \xE1guia +-- print(replaceLatinCharacters(águia)) +-- > \xE1guia function replaceLatinCharacters(str) if not hasLatinCharacters(str) then return str diff --git a/packages/base/tests/functional/alternative/File.lua b/packages/base/tests/functional/alternative/File.lua index a3cfd14a..645d90f8 100644 --- a/packages/base/tests/functional/alternative/File.lua +++ b/packages/base/tests/functional/alternative/File.lua @@ -48,8 +48,6 @@ return{ unitTest:assertError(error_func, "Directory '/my/path/' does not exist.") - local path = packageInfo("base").data - local filename = "file*" error_func = function() File(filename) diff --git a/packages/base/tests/functional/basics/Package.lua b/packages/base/tests/functional/basics/Package.lua index e8c5c783..8b659f43 100644 --- a/packages/base/tests/functional/basics/Package.lua +++ b/packages/base/tests/functional/basics/Package.lua @@ -45,7 +45,7 @@ return{ unitTest:assertType(cs, "CellularSpace") -- The assert below checks the number of functions in package 'base'. - unitTest:assertEquals(getn(base), 180) + unitTest:assertEquals(getn(base), 181) end, import = function(unitTest) forEachCell = nil diff --git a/packages/gis/tests/functional/alternative/Project.lua b/packages/gis/tests/functional/alternative/Project.lua index 9253dd91..6e9a76a4 100644 --- a/packages/gis/tests/functional/alternative/Project.lua +++ b/packages/gis/tests/functional/alternative/Project.lua @@ -114,7 +114,7 @@ return{ layer = packageInfo("gis").path.."/data*/conservation*", } end - unitTest:assertError(patternFileError, "Directory path '"..packageInfo("gis").path.."/data*/".."' cannot contain character '*'.") + unitTest:assertError(patternFileError, "Directory path '"..packageInfo("gis").path.."/data*/".."' contains invalid character '*'.") local projTemporal patternFileError = function() diff --git a/src/core/terrameLua.cpp b/src/core/terrameLua.cpp index e194817c..d0908eb5 100644 --- a/src/core/terrameLua.cpp +++ b/src/core/terrameLua.cpp @@ -219,7 +219,7 @@ int cpp_imagecompare(lua_State *L) { const char* s1 = lua_tostring(L, -1); const char* s2 = lua_tostring(L, -2); - + QString f1(QString::fromLocal8Bit(s1)); QString f2(QString::fromLocal8Bit(s2)); @@ -325,7 +325,7 @@ int cpp_getLocale(lua_State* L) { std::locale loc(""); lua_pushstring(L, loc.name().c_str()); - + return 1; } From e81a3b80df9384e23882e8ee53857717896d6cd5 Mon Sep 17 00:00:00 2001 From: Rodrigo Avancini Date: Wed, 26 Sep 2018 17:18:14 -0300 Subject: [PATCH 5/7] Add test #2245. --- packages/base/tests/functional/basics/Utils.lua | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/base/tests/functional/basics/Utils.lua b/packages/base/tests/functional/basics/Utils.lua index dcd7621b..4f873199 100644 --- a/packages/base/tests/functional/basics/Utils.lua +++ b/packages/base/tests/functional/basics/Utils.lua @@ -1161,6 +1161,13 @@ return{ end) unitTest:assertEquals(count, 84) + end, + replaceLatinCharacters = function(unitTest) + local str = "action" + unitTest:assertEquals(replaceLatinCharacters(str), "action") + + str = "ação" + unitTest:assertEquals(replaceLatinCharacters(str), "a\xE7\xE3o") end } From dfda3fcec74159d03736d5164c58af1f2262f83e Mon Sep 17 00:00:00 2001 From: Rodrigo Avancini Date: Thu, 27 Sep 2018 09:23:20 -0300 Subject: [PATCH 6/7] Small corrections. --- packages/base/lua/Directory.lua | 6 +++--- packages/base/lua/File.lua | 2 +- packages/gis/tests/functional/alternative/Layer.lua | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/base/lua/Directory.lua b/packages/base/lua/Directory.lua index 7902ff74..d7315fbe 100644 --- a/packages/base/lua/Directory.lua +++ b/packages/base/lua/Directory.lua @@ -264,7 +264,7 @@ function Directory(data) data.fullpath = _Gtme.makePathCompatibleToAllOS(data.fullpath) if sessionInfo().system == "windows" then - data.fullpath = replaceLatinCharacters(data.fullpath) + data.fullpath = replaceLatinCharacters(data.fullpath) --SKIP end if data.fullpath:sub(-1) == "/" then @@ -282,9 +282,9 @@ function Directory(data) table.insert(_Gtme.tmpdirectory__, data) if sessionInfo().system == "windows" then - data.fullpath = replaceLatinCharacters(cmd) + data.fullpath = replaceLatinCharacters(cmd) --SKIP else - data.fullpath = cmd + data.fullpath = cmd --SKIP end end diff --git a/packages/base/lua/File.lua b/packages/base/lua/File.lua index 1450d7cf..c59b55f4 100644 --- a/packages/base/lua/File.lua +++ b/packages/base/lua/File.lua @@ -566,7 +566,7 @@ function File(data) checkInvalidChars(data:name()) if sessionInfo().system == "windows" then - data.filename = replaceLatinCharacters(data.filename) + data.filename = replaceLatinCharacters(data.filename) --SKIP end if isDirectory(data.filename) then diff --git a/packages/gis/tests/functional/alternative/Layer.lua b/packages/gis/tests/functional/alternative/Layer.lua index b425efaf..70c43896 100644 --- a/packages/gis/tests/functional/alternative/Layer.lua +++ b/packages/gis/tests/functional/alternative/Layer.lua @@ -458,7 +458,7 @@ return{ name = "conservation" } end - unitTest:assertError(patternFileError, "Directory path '"..packageInfo("gis").path.."/data*/".."' cannot contain character '*'.") + unitTest:assertError(patternFileError, "Directory path '"..packageInfo("gis").path.."/data*/".."' contains invalid character '*'.") patternFileError = function() Layer{ @@ -508,7 +508,7 @@ return{ times = {1961, 1974} } end - unitTest:assertError(patternFileError, "Directory path '"..packageInfo("gis").path.."/data*/".."' cannot contain character '*'.") + unitTest:assertError(patternFileError, "Directory path '"..packageInfo("gis").path.."/data*/".."' contains invalid character '*'.") local patternWarning = function() Layer{ From b9a27580f4b2c078c88dc3f8acd32e40d11f756a Mon Sep 17 00:00:00 2001 From: Rodrigo Avancini Date: Thu, 27 Sep 2018 11:04:50 -0300 Subject: [PATCH 7/7] Review request #2248. --- packages/base/lua/Utils.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/base/lua/Utils.lua b/packages/base/lua/Utils.lua index c011a383..cbf7f238 100644 --- a/packages/base/lua/Utils.lua +++ b/packages/base/lua/Utils.lua @@ -1648,7 +1648,7 @@ end -- @arg str A string. -- @usage --DONTRUN -- print(replaceLatinCharacters(águia)) --- > \xE1guia +-- -- \xE1guia function replaceLatinCharacters(str) if not hasLatinCharacters(str) then return str