Merge lp:~zorba-coders/zorba/system_doc into lp:zorba/process-module

Proposed by William Candillon
Status: Superseded
Proposed branch: lp:~zorba-coders/zorba/system_doc
Merge into: lp:zorba/process-module
Diff against target: 1066 lines (+1014/-0) (has conflicts)
9 files modified
CMakeLists.txt (+29/-0)
src/CMakeLists.txt (+19/-0)
src/com/CMakeLists.txt (+14/-0)
src/com/zorba-xquery/CMakeLists.txt (+14/-0)
src/com/zorba-xquery/www/CMakeLists.txt (+14/-0)
src/com/zorba-xquery/www/modules/CMakeLists.txt (+14/-0)
src/com/zorba-xquery/www/modules/system.xq (+211/-0)
src/com/zorba-xquery/www/modules/system.xq.src/system.cpp (+606/-0)
src/com/zorba-xquery/www/modules/system.xq.src/system.h (+93/-0)
Conflict adding file CMakeLists.txt.  Moved existing file to CMakeLists.txt.moved.
Conflict adding file src.  Moved existing file to src.moved.
To merge this branch: bzr merge lp:~zorba-coders/zorba/system_doc
Reviewer Review Type Date Requested Status
Matthias Brantner Needs Information
William Candillon Pending
Review via email: mp+126739@code.launchpad.net

This proposal has been superseded by a proposal from 2012-09-27.

Commit message

Minor documentation improvement

Description of the change

Minor documentation improvement

To post a comment you must log in.
Revision history for this message
Matthias Brantner (matthias-brantner) wrote :

You were trying to merge the system into the process module.

Is that on purpose?

review: Needs Information

Unmerged revisions

35. By William Candillon

Minor documentation improvement.

34. By Dennis Knochenwefel

fixed crash with signal 8: SIGFPE (division by zero). bug #1026594. Approved: Dennis Knochenwefel, Chris Hillery

33. By Matthias Brantner

fix for bug #978722 (change annotation prefix from "ann" to "an") Approved: William Candillon, Matthias Brantner

32. By Chris Hillery

Converted all files to correct native EOLs.

31. By cezarfx

Fix bug when USER is not available as a environment var.
Fix if uname return with error.

30. By mbrantner

fixed typo in the system module

29. By mbrantner

documentation for system module (provided by William)

28. By spungi

- improved the speed of "make xqdoc" such that we can add xqdoc as a test
- fixed SF bug #3375604 "Built target cxxdocs" hangs
- make xqdoc does not search the filesystem, instead creates the documentation *only* for the modules that are compiled in the Zorba version used (based on the ZorbaManifest.xml in the build folder)
- implemented the 2 issues that Dana asked for in the email from @coders "core and non-core Zorba modules"
- fixed the link crawler issues in XQDoc reported by Daniel (except the link to xqdoc.org)

27. By mbrantner

added missing license headers

26. By spungi

XQuery version changes:
- Added XQuery version declarations for all the modules (part of the Zorba core or not).
- fixed the test "moduleDocumentation" that checks if all the modules are well documented:
  - this test used to check only the Zorba core modules: now it also checks the external modules.
  - added a new rule that makes sure all modules have a XQuery version declarations.

XQDoc changes:
- documented some XQDoc variables, params and return values that were missing.
- added support for XQuery version and Encoding in the Zorba XQDoc.
- added a new part called "XQuery version and Encoding for this module:" as the last lines in the module description of the XQDoc documentation.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'CMakeLists.txt'
--- CMakeLists.txt 1970-01-01 00:00:00 +0000
+++ CMakeLists.txt 2012-09-27 16:59:25 +0000
@@ -0,0 +1,29 @@
1# Copyright 2006-2010 The FLWOR Foundation.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
16
17PROJECT (zorba_system_module)
18ENABLE_TESTING ()
19INCLUDE (CTest)
20
21LIST (APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake_modules")
22
23FIND_PACKAGE (Zorba REQUIRED HINTS "${ZORBA_BUILD_DIR}")
24INCLUDE ("${Zorba_USE_FILE}")
25
26ADD_TEST_DIRECTORY("${PROJECT_SOURCE_DIR}/test")
27ADD_SUBDIRECTORY("src")
28
29DONE_DECLARING_ZORBA_URIS()
030
=== renamed file 'CMakeLists.txt' => 'CMakeLists.txt.moved'
=== added directory 'src'
=== renamed directory 'src' => 'src.moved'
=== added file 'src/CMakeLists.txt'
--- src/CMakeLists.txt 1970-01-01 00:00:00 +0000
+++ src/CMakeLists.txt 2012-09-27 16:59:25 +0000
@@ -0,0 +1,19 @@
1# Copyright 2006-2008 The FLWOR Foundation.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# all external module libraries are generated in the directory
16# of the corresponding .xq file
17MESSAGE(STATUS "Add com")
18ADD_SUBDIRECTORY(com)
19MESSAGE(STATUS "End modules")
020
=== added directory 'src/com'
=== added file 'src/com/CMakeLists.txt'
--- src/com/CMakeLists.txt 1970-01-01 00:00:00 +0000
+++ src/com/CMakeLists.txt 2012-09-27 16:59:25 +0000
@@ -0,0 +1,14 @@
1# Copyright 2006-2008 The FLWOR Foundation.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14ADD_SUBDIRECTORY(zorba-xquery)
015
=== added directory 'src/com/zorba-xquery'
=== added file 'src/com/zorba-xquery/CMakeLists.txt'
--- src/com/zorba-xquery/CMakeLists.txt 1970-01-01 00:00:00 +0000
+++ src/com/zorba-xquery/CMakeLists.txt 2012-09-27 16:59:25 +0000
@@ -0,0 +1,14 @@
1# Copyright 2006-2008 The FLWOR Foundation.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14ADD_SUBDIRECTORY(www)
015
=== added directory 'src/com/zorba-xquery/www'
=== added file 'src/com/zorba-xquery/www/CMakeLists.txt'
--- src/com/zorba-xquery/www/CMakeLists.txt 1970-01-01 00:00:00 +0000
+++ src/com/zorba-xquery/www/CMakeLists.txt 2012-09-27 16:59:25 +0000
@@ -0,0 +1,14 @@
1# Copyright 2006-2008 The FLWOR Foundation.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14ADD_SUBDIRECTORY(modules)
015
=== added directory 'src/com/zorba-xquery/www/modules'
=== added file 'src/com/zorba-xquery/www/modules/CMakeLists.txt'
--- src/com/zorba-xquery/www/modules/CMakeLists.txt 1970-01-01 00:00:00 +0000
+++ src/com/zorba-xquery/www/modules/CMakeLists.txt 2012-09-27 16:59:25 +0000
@@ -0,0 +1,14 @@
1# Copyright 2006-2008 The FLWOR Foundation.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14DECLARE_ZORBA_MODULE (URI "http://www.zorba-xquery.com/modules/system" VERSION 1.0 FILE "system.xq")
015
=== added file 'src/com/zorba-xquery/www/modules/system.xq'
--- src/com/zorba-xquery/www/modules/system.xq 1970-01-01 00:00:00 +0000
+++ src/com/zorba-xquery/www/modules/system.xq 2012-09-27 16:59:25 +0000
@@ -0,0 +1,211 @@
1xquery version "3.0";
2
3(:
4 : Copyright 2006-2009 The FLWOR Foundation.
5 :
6 : Licensed under the Apache License, Version 2.0 (the "License");
7 : you may not use this file except in compliance with the License.
8 : You may obtain a copy of the License at
9 :
10 : http://www.apache.org/licenses/LICENSE-2.0
11 :
12 : Unless required by applicable law or agreed to in writing, software
13 : distributed under the License is distributed on an "AS IS" BASIS,
14 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : See the License for the specific language governing permissions and
16 : limitations under the License.
17:)
18(:~
19 : The system module allows developers to access system properties.
20 : Part of these system properties are environment variables,
21 : local variable to the process running Zorba, and properties defined by Zorba.
22 : <br />
23 : To avoid conflicts between environment variables and properties defined by Zorba,
24 : all environment variables are prefixed with <i>env.</i>.
25 : <br />
26 : For instance, the following query: <br />
27 : <pre class="ace-static">
28 : import module namespace system = "http://www.zorba-xquery.com/modules/system";
29 :
30 : for $prop in system:properties()
31 : return concat($prop, ": ", system:property($prop), "
32 : ")
33 : </pre>
34 : <br />
35 : Will output:
36 : <pre class="ace-static">
37 : env.TERM_PROGRAM: Apple_Terminal
38 : ...
39 : </pre>
40 : In this example, it is important to notice that the environnement variable PATH
41 : with the key env.PATH.
42 :
43 : @author Markus Pilman
44 : @project Zorba/IO/System
45 :)
46module namespace system = 'http://www.zorba-xquery.com/modules/system';
47
48declare namespace an = "http://www.zorba-xquery.com/annotations";
49
50declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
51declare option ver:module-version "1.0";
52
53(:~
54 : The name of the operating system (os.name).
55 :)
56declare variable $system:os-name as xs:string := "os.name";
57
58(:~
59 : The name of the computer the process is running on (os.node.name).
60 :)
61declare variable $system:os-node-name as xs:string := "os.node.name";
62
63(:~
64 : The major version number of the Windows installation or
65 : an empty string if the process does not run on a Windows installation
66 : (os.version.major).
67 : <b>Works on Windows only.</b>
68 :)
69declare variable $system:os-version-major as xs:string := "os.version.major";
70
71(:~
72 : The minor version number of the Windows installation or
73 : an empty string if the process does not run on a Windows installation
74 : (os.version.minor).
75 : <b>Works on Windows only.</b>
76 :)
77declare variable $system:os-version-minor as xs:string := "os.version.minor";
78
79(:~
80 : The build number of the Windows installation or
81 : an empty string if the process does not run on a Windows installation
82 : (os.version.build).
83 : <b>Works on Windows only.</b>
84 :)
85declare variable $system:os-version-build as xs:string := "os.version.build";
86
87(:~
88 : The release of this UNIX installation or
89 : an empty string if the process does not run on a UNIX/Linux installation
90 : (os.version.release).
91 : <b>Works on UNIX based operating systems only.</b>
92 :)
93declare variable $system:os-version-release as xs:string := "os.version.release";
94(:~
95 : The version of this UNIX installation or
96 : an empty string if the process does not run on a UNIX/Linux installation
97 : (os.version.version).
98 : <b>Works on UNIX based operating systems only.</b>
99 :)
100declare variable $system:os-version-version as xs:string := "os.version.version";
101
102(:~
103 : The version of the Operating System.
104 :)
105declare variable $system:os-version as xs:string := "os.version";
106
107(:~
108 : The name of the processor architecture (os.arch).
109 : For example x86 or x86_64.
110 :)
111declare variable $system:os-arch as xs:string := "os.arch";
112
113(:~
114 : True if system architecture is 64bits (os.is64).
115 :)
116declare variable $system:os-is64 as xs:string := "os.is64";
117
118(:~
119 : Number of logical processors in the system (hardware.logical.cpu).
120 : This information is not available under Mac OS X.
121 :)
122declare variable $system:hardware-logical-cpu as xs:string := "hardware.logical.cpu";
123
124(:~
125 : Number of physical processors in the system (hardware.logical.cpu).
126 :)
127declare variable $system:hardware-physical-cpu as xs:string := "hardware.physical.cpu";
128
129(:~
130 : number of logical per physical processors in the system (hardware.logical.per.physical.cpu).
131 : This information is not available under Mac OS X.
132 :)
133declare variable $system:hardware-logical-per-physical-cpu as xs:string := "hardware.logical.per.physical.cpu";
134
135(:~
136 : Physical memory available (hardware.physical.memory).
137 :)
138declare variable $system:hardware-physical-memory as xs:string := "hardware.physical.memory";
139(:~
140 : Virtual memory available (hardware.virtual.memory).
141 :)
142declare variable $system:hardware-virtual-memory as xs:string := "hardware.virtual.memory";
143(:~
144 : Gets the hardware manufacturer (hardware.manufacturer).
145 :)
146declare variable $system:hardware-manufacturer as xs:string := "hardware.manufacturer";
147
148(:~
149 : The Linux distribution, Zorba is running on (linux.distributor).
150 : <b>Works on UNIX based operating systems only.</b>
151 :)
152declare variable $system:linux-distributor as xs:string := "linux.distributor";
153
154(:~
155 : The version of the Linux distribution, Zorba is running on (linux.distributor.version).
156 : <b>Works on UNIX based operating systems only.</b>
157 :)
158declare variable $system:linux-distributor-version as xs:string := "linux.distributor.version";
159
160(:~
161 : The username, with which this process was started (user.name).
162 : On Unix, this variable is only available if the USER environment
163 : variable is set (e.g. it might not be available in a cronjob).
164 :)
165declare variable $system:user-name as xs:string := "user.name";
166
167(:~
168 : The Zorba module path, that is the paths in which Zorba looks
169 : for modules (zorba.module.path).
170 :)
171declare variable $system:zorba-module-path as xs:string := "zorba.module.path";
172
173(:~
174 : Zorba version in the format Major.Minor.Patch (zorba.version).
175 :)
176declare variable $system:zorba-version as xs:string := "zorba.version";
177
178(:~
179 : Zorba major version (zorba.version.major).
180 :)
181declare variable $system:zorba-version-major as xs:string := "zorba.version.major";
182
183(:~
184 : Zorba minor version (zorba.version.minor).
185 :)
186declare variable $system:zorba-version-minor as xs:string := "zorba.version.minor";
187
188(:~
189 : Zorba patch version (zorba.version.patch).
190 :)
191declare variable $system:zorba-version-patch as xs:string := "zorba.version.patch";
192
193(:~
194 : Gets the system property indicated by the specified key.
195 :
196 : @param $key The name of the system property.
197 : @return The string value of the system property, or an empty sequence if there is no property with that key.
198 :)
199declare %an:nondeterministic function system:property($key as xs:string) as xs:string? external;
200
201(:~
202 : This function retrieves the names of the current system properties.
203 : This list includes environment variables, local variable to the process running Zorba, and properties defined by Zorba.
204 : <br />
205 : To avoid conflicts between environment variables and properties defined by Zorba,
206 : all environment variables are prefixed with <i>env.</i>.
207 :
208 : @return List of all system properties.
209 :)
210declare %an:nondeterministic function system:properties() as xs:string* external;
211
0212
=== added directory 'src/com/zorba-xquery/www/modules/system.xq.src'
=== added file 'src/com/zorba-xquery/www/modules/system.xq.src/system.cpp'
--- src/com/zorba-xquery/www/modules/system.xq.src/system.cpp 1970-01-01 00:00:00 +0000
+++ src/com/zorba-xquery/www/modules/system.xq.src/system.cpp 2012-09-27 16:59:25 +0000
@@ -0,0 +1,606 @@
1/*
2 * Copyright 2006-2008 The FLWOR Foundation.
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#include <cstdlib>
17#include <cstdio>
18#include <sstream>
19
20#ifdef WIN32
21# include <Windows.h>
22# include <malloc.h>
23# include <stdio.h>
24# include <tchar.h>
25# include <winreg.h>
26#else
27#include <sys/utsname.h>
28# ifndef __APPLE__
29# include <sys/sysinfo.h>
30# else
31# include <sys/param.h>
32# include <sys/sysctl.h>
33# endif
34#endif
35
36#include <zorba/zorba_string.h>
37#include <zorba/singleton_item_sequence.h>
38#include <zorba/vector_item_sequence.h>
39#include <zorba/empty_sequence.h>
40#include <zorba/item_factory.h>
41
42
43#ifdef LINUX
44#include <iostream>
45#include <string>
46#include <fstream>
47#include <unistd.h>
48extern char** environ;
49#elif defined APPLE
50# include <crt_externs.h>
51#endif
52
53#include "system.h"
54
55
56namespace zorba { namespace system {
57
58 const String SystemModule::SYSTEM_MODULE_NAMESPACE = "http://www.zorba-xquery.com/modules/system";
59
60#ifdef WIN32
61 typedef BOOL (WINAPI *LPFN_GLPI)(
62 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION,
63 PDWORD);
64
65 // Helper function to count set bits in the processor mask.
66 DWORD CountSetBits(ULONG_PTR bitMask)
67 {
68 DWORD LSHIFT = sizeof(ULONG_PTR)*8 - 1;
69 DWORD bitSetCount = 0;
70 ULONG_PTR bitTest = (ULONG_PTR)1 << LSHIFT;
71 DWORD i;
72 for (i = 0; i <= LSHIFT; ++i)
73 {
74 bitSetCount += ((bitMask & bitTest)?1:0);
75 bitTest/=2;
76 }
77
78 return bitSetCount;
79 }
80
81 DWORD numaNodeCount = 0;
82 DWORD processorPackageCount = 0;
83 DWORD logicalProcessorCount = 0;
84 DWORD processorCoreCount = 0;
85 DWORD processorL1CacheCount = 0;
86 DWORD processorL2CacheCount = 0;
87 DWORD processorL3CacheCount = 0;
88 static void countProcessors() {
89 LPFN_GLPI glpi;
90 BOOL done = FALSE;
91 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
92 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
93 DWORD returnLength = 0;
94 DWORD byteOffset = 0;
95 PCACHE_DESCRIPTOR Cache;
96
97 glpi = (LPFN_GLPI) GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformation");
98 if (NULL == glpi) {
99 // GetLogicalProcessorInformation is not supported.
100 return;
101 }
102
103 while (!done)
104 {
105 DWORD rc = glpi(buffer, &returnLength);
106 if (FALSE == rc)
107 {
108 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
109 {
110 if (buffer)
111 free(buffer);
112 buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);
113 if (NULL == buffer)
114 {
115 // Error: Allocation failure
116 return;
117 }
118 } else {
119 // Error %d, GetLastError()
120 return;
121 }
122 } else {
123 done = TRUE;
124 }
125 }
126 ptr = buffer;
127 while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength)
128 {
129 switch (ptr->Relationship)
130 {
131 case RelationNumaNode:
132 // Non-NUMA systems report a single record of this type.
133 numaNodeCount++;
134 break;
135 case RelationProcessorCore:
136 processorCoreCount++;
137 // A hyperthreaded core supplies more than one logical processor.
138 logicalProcessorCount += CountSetBits(ptr->ProcessorMask);
139 break;
140
141 case RelationCache:
142 // Cache data is in ptr->Cache, one CACHE_DESCRIPTOR structure for each cache.
143 Cache = &ptr->Cache;
144 if (Cache->Level == 1)
145 {
146 processorL1CacheCount++;
147 }
148 else if (Cache->Level == 2)
149 {
150 processorL2CacheCount++;
151 }
152 else if (Cache->Level == 3)
153 {
154 processorL3CacheCount++;
155 }
156 break;
157 case RelationProcessorPackage:
158 // Logical processors share a physical package.
159 processorPackageCount++;
160 break;
161 default:
162 // Error: Unsupported LOGICAL_PROCESSOR_RELATIONSHIP value.
163 break;
164 }
165 byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
166 ptr++;
167 }
168 free(buffer);
169 return;
170 }
171
172#endif
173
174#ifdef LINUX
175 static void trim(std::string& str, char delim)
176 {
177 std::string::size_type pos = str.find_last_not_of(delim);
178 if(pos != std::string::npos) {
179 str.erase(pos + 1);
180 pos = str.find_first_not_of(delim);
181 if(pos != std::string::npos) str.erase(0, pos);
182 }
183 else str.erase(str.begin(), str.end());
184 }
185
186
187 int logical=0;
188 int cores=0;
189 int physical=0;
190
191
192 static void countProcessors() {
193 logical=0;
194 cores=1; // per default: single core processor
195 physical=0;
196
197 std::ifstream in("/proc/cpuinfo");
198 if(in) {
199 std::string name;
200 std::string value;
201
202 while(in) {
203 getline(in, name, ':');
204 trim (name, ' ');
205 trim (name, '\t');
206 trim (name, '\n');
207 getline(in, value);
208 trim (value, ' ');
209 trim (value, '\t');
210 if (name == "processor") {
211 logical++;
212 }
213
214 if (name == "cpu cores") {
215 cores = atoi(value.c_str());
216 }
217 }
218 physical = logical/cores;
219 in.close();
220 }
221 }
222
223
224
225 static std::pair<std::string, std::string> getDistribution() {
226 std::pair<std::string, std::string> lRes;
227 FILE *pipe;
228 const char* command = "lsb_release -r -i";
229 pipe = (FILE*) popen(command, "r");
230
231 char line[1024];
232 while (fgets(line, sizeof(line), pipe)) {
233 std::stringstream s(line);
234 std::string name, value;
235 getline(s, name, ':');
236 trim(name, ' ');
237 trim(name, '\t');
238 getline(s, value, ':');
239 trim(value, ' ');
240 trim(value, '\t');
241 trim(value, '\n');
242 if (name == "Distributor ID") {
243 lRes.first = value;
244 } else {
245 lRes.second = value;
246 }
247 }
248 return lRes;
249 }
250#endif
251
252 SystemModule::SystemModule()
253 : thePropertyFunction(0), thePropertiesFunction(0)
254 {
255 }
256
257 ExternalFunction* SystemModule::getExternalFunction(const String& localName) {
258 if (localName == "properties") {
259 if (!thePropertiesFunction)
260 thePropertiesFunction = new PropertiesFunction(this);
261 return thePropertiesFunction;
262 } else if (localName == "property") {
263 if (!thePropertyFunction)
264 thePropertyFunction = new PropertyFunction(this);
265 return thePropertyFunction;
266 }
267 return 0;
268 }
269
270 void SystemModule::destroy() {
271 delete this;
272 }
273
274 SystemModule::~SystemModule() {
275 delete thePropertyFunction;
276 delete thePropertiesFunction;
277 }
278
279
280 SystemFunction::SystemFunction(const ExternalModule* aModule)
281 : theModule(aModule), theFactory(Zorba::getInstance(0)->getItemFactory())
282 {
283#ifdef WIN32
284
285 {
286 DWORD nodeNameLength = MAX_COMPUTERNAME_LENGTH + 1;
287 TCHAR nodeName[MAX_COMPUTERNAME_LENGTH + 1];
288 char nodeNameC[MAX_COMPUTERNAME_LENGTH + 1];
289 GetComputerName(nodeName, &nodeNameLength);
290 for (DWORD i = 0; i < nodeNameLength; ++i) {
291 nodeNameC[i] = static_cast<char>(nodeName[i]);
292 }
293 nodeNameC[nodeNameLength] = NULL; // Terminate string
294 theProperties.insert(std::make_pair("os.node.name", nodeNameC));
295 }
296
297 {
298 DWORD dwVersion = 0;
299 DWORD dwMajorVersion = 0;
300 DWORD dwMinorVersion = 0;
301 DWORD dwBuild = 0;
302
303 dwVersion = GetVersion();
304
305 // Get the Windows version.
306 dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
307 dwMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
308
309 // Get the build number.
310 if (dwVersion < 0x80000000)
311 dwBuild = (DWORD)(HIWORD(dwVersion));
312
313 std::string major;
314 std::string minor;
315 std::string build;
316 {
317 std::stringstream sMajor;
318 sMajor << dwMajorVersion;
319 std::stringstream sMinor;
320 sMinor << dwMinorVersion;
321 std::stringstream sBuild;
322 sBuild << dwBuild;
323
324 major = sMajor.str();
325 minor = sMinor.str();
326 build = sBuild.str();
327 }
328 theProperties.insert(std::make_pair("os.version.major", major));
329 theProperties.insert(std::make_pair("os.version.minor", minor));
330 theProperties.insert(std::make_pair("os.version.build", build));
331 theProperties.insert(std::make_pair("os.version", major + "." + minor + "." + build));
332 // http://msdn.microsoft.com/en-us/library/ms724832(v=VS.85).aspx
333 std::string operativeSystem;
334 theProperties.insert(std::make_pair("os.name", "Windows"));
335 {
336 countProcessors();
337 std::stringstream logicalProcessors;
338 logicalProcessors << processorPackageCount;
339 std::stringstream physicalProcessors;
340 physicalProcessors << logicalProcessorCount;
341 std::stringstream logicalPerPhysicalProcessors;
342 logicalPerPhysicalProcessors << (logicalProcessorCount / processorPackageCount );
343 theProperties.insert(std::make_pair("hardware.physical.cpu", logicalProcessors.str() ));
344 theProperties.insert(std::make_pair("hardware.logical.cpu", physicalProcessors.str() ));
345 theProperties.insert(std::make_pair("hardware.logical.per.physical.cpu", logicalPerPhysicalProcessors.str() ));
346 }
347 {
348 MEMORYSTATUSEX statex;
349 statex.dwLength = sizeof (statex);
350 GlobalMemoryStatusEx (&statex);
351 std::stringstream virtualMemory;
352 virtualMemory << statex.ullTotalVirtual;
353 std::stringstream physicalMemory;
354 physicalMemory << statex.ullTotalPhys;
355 theProperties.insert(std::make_pair("hardware.virtual.memory", virtualMemory.str() ));
356 theProperties.insert(std::make_pair("hardware.physical.memory", physicalMemory.str() ));
357 }
358
359 }
360 {
361 DWORD userNameLength = 1023;
362 TCHAR userName[1024];
363 char userNameC[1024];
364 GetUserName(userName, &userNameLength);
365 for (DWORD i = 0; i < userNameLength; ++i) {
366 userNameC[i] = static_cast<char>(userName[i]);
367 }
368 theProperties.insert(std::make_pair("user.name", userNameC));
369 }
370 {
371 SYSTEM_INFO info;
372 GetSystemInfo(&info);
373 if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
374 theProperties.insert(std::make_pair("os.arch", "x86_64"));
375 theProperties.insert(std::make_pair("os.is64", "true"));
376 } else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64) {
377 theProperties.insert(std::make_pair("os.arch", "ia64"));
378 theProperties.insert(std::make_pair("os.is64", "true"));
379 } else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL) {
380 theProperties.insert(std::make_pair("os.arch", "i386"));
381 theProperties.insert(std::make_pair("os.is64", "false"));
382 }
383 }
384
385 {
386 HKEY keyHandle;
387 TCHAR value [1024];
388 char valueC [1024];
389 DWORD size = 0;
390 DWORD Type;
391 if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\BIOS", 0, KEY_QUERY_VALUE, &keyHandle) == ERROR_SUCCESS)
392 {
393 RegQueryValueEx( keyHandle, L"SystemManufacturer", NULL, &Type, (LPBYTE)value, &size);
394 for (DWORD i = 0; i < size; ++i) {
395 valueC[i] = static_cast<char>(value[i]);
396 }
397 if (size > 0)
398 theProperties.insert(std::make_pair("hardware.manufacturer", valueC));
399 }
400 RegCloseKey(keyHandle);
401 }
402
403
404#else
405 struct utsname osname;
406 if (uname(&osname) == 0)
407 {
408 theProperties.insert(std::make_pair("os.name", osname.sysname));
409 theProperties.insert(std::make_pair("os.node.name", osname.nodename));
410 theProperties.insert(std::make_pair("os.version.release", osname.release));
411 theProperties.insert(std::make_pair("os.version.version", osname.version));
412 theProperties.insert(std::make_pair("os.version", osname.release));
413 theProperties.insert(std::make_pair("os.arch", osname.machine));
414 }
415 char* lUser = getenv("USER");
416 if (lUser)
417 {
418 theProperties.insert(std::make_pair("user.name", lUser));
419 }
420 theProperties.insert(std::make_pair("os.is64", "false"));
421 {
422#ifdef __APPLE__
423 int mib[2];
424 size_t len = 4;
425 uint32_t res = 0;
426
427 mib[0] = CTL_HW;
428 mib[1] = HW_NCPU;
429 sysctl(mib, 2, &res, &len, NULL, NULL);
430 std::stringstream lStream;
431 lStream << res;
432 theProperties.insert(std::make_pair("hardware.physical.cpu", lStream.str()));
433#else
434 countProcessors();
435 std::stringstream logicalProcessor;
436 std::stringstream physicalProcessor;
437 std::stringstream logicalPerPhysicalProcessors;
438 logicalProcessor << logical;
439 physicalProcessor << physical;
440 logicalPerPhysicalProcessors << cores;
441 theProperties.insert(std::make_pair("hardware.logical.per.physical.cpu", logicalPerPhysicalProcessors.str() ));
442 theProperties.insert(std::make_pair("hardware.physical.cpu", physicalProcessor.str() ));
443 theProperties.insert(std::make_pair("hardware.logical.cpu", logicalProcessor.str() ));
444#endif
445 }
446 {
447# ifdef LINUX
448 struct sysinfo sys_info;
449 if(sysinfo(&sys_info) == 0) {
450 std::stringstream memory;
451 memory << sys_info.totalram;
452 std::stringstream swap;
453 swap << sys_info.totalswap;
454 theProperties.insert(std::make_pair("hardware.virtual.memory", swap.str() ));
455 theProperties.insert(std::make_pair("hardware.physical.memory", memory.str() ));
456 }
457# elif defined __APPLE__
458 int mib[2];
459 size_t len = 8;
460 uint64_t res = 0;
461
462 mib[0] = CTL_HW;
463 mib[1] = HW_MEMSIZE;
464 sysctl(mib, 2, &res, &len, NULL, NULL);
465 std::stringstream lStream;
466 lStream << res;
467 theProperties.insert(std::make_pair("hardware.physical.memory", lStream.str()));
468# endif
469 }
470
471#endif
472#ifdef LINUX
473 theProperties.insert(std::make_pair("linux.distributor", ""));
474 theProperties.insert(std::make_pair("linux.distributor.version", ""));
475#endif
476 theProperties.insert(std::make_pair("zorba.version", Zorba::version().getVersion()));
477 theProperties.insert(std::make_pair("zorba.version.major", intToString(Zorba::version().getMajorVersion())));
478 theProperties.insert(std::make_pair("zorba.version.minor", intToString(Zorba::version().getMinorVersion())));
479 theProperties.insert(std::make_pair("zorba.version.patch", intToString(Zorba::version().getPatchVersion())));
480 }
481
482 String SystemFunction::intToString(int v) {
483 std::stringstream ss;
484 ss << v;
485 return ss.str();
486 }
487
488 bool SystemFunction::getEnv(const String& name, String& value) const
489 {
490 char* v = getenv(name.c_str());
491 if (v == NULL) return false;
492 value = v;
493 return true;
494 }
495
496 void SystemFunction::getEnvNames(std::vector<Item>& names) const
497 {
498#ifdef WIN32
499 // put in the environment variables
500 TCHAR *l_EnvStr;
501 l_EnvStr = GetEnvironmentStrings();
502
503 LPTSTR l_str = l_EnvStr;
504
505 int count = 0;
506 while (true)
507 {
508 if (*l_str == 0) break;
509 while (*l_str != 0) l_str++;
510 l_str++;
511 count++;
512 }
513
514 for (int i = 0; i < count; i++)
515 {
516 char lStr[1024];
517 memset(lStr, 0, 1024);
518 for (int i =0; i<1023 && l_EnvStr[i]; ++i) {
519 lStr[i] = (char) l_EnvStr[i];
520 }
521 std::string e(lStr);
522 std::string name("env.");
523 name += e.substr(0, e.find('='));
524 String value = e.substr(e.find('=') + 1);
525 if (name != "env.")
526 names.push_back(theFactory->createString(name));
527 while(*l_EnvStr != '\0')
528 l_EnvStr++;
529 l_EnvStr++;
530 }
531 //FreeEnvironmentStrings(l_EnvStr);
532#else
533# ifdef APPLE
534 char** environ = *_NSGetEnviron();
535# endif // APPLE
536 for (int i = 0; environ[i] != NULL; ++i) {
537 std::string e(environ[i]);
538 String name("env.");
539 name += e.substr(0, e.find('='));
540 names.push_back(theFactory->createString(name));
541 }
542#endif
543 }
544
545 ItemSequence_t PropertiesFunction::evaluate(
546 const ExternalFunction::Arguments_t& args) const {
547 std::vector<Item> lRes;
548 getEnvNames(lRes);
549 for (std::map<String, String>::const_iterator i = theProperties.begin();
550 i != theProperties.end(); ++i) {
551 Item lItem = theFactory->createString(i->first.c_str());
552 lRes.push_back(lItem);
553 }
554 // insert the zorba module path
555 lRes.push_back(theFactory->createString("zorba.module.path"));
556 return ItemSequence_t(new VectorItemSequence(lRes));
557 }
558
559 ItemSequence_t PropertyFunction::evaluate(
560 const ExternalFunction::Arguments_t& args,
561 const StaticContext* sctx,
562 const DynamicContext* dctx) const {
563 Item item;
564 Iterator_t arg0_iter = args[0]->getIterator();
565 arg0_iter->open();
566 arg0_iter->next(item);
567 arg0_iter->close();
568 String envS = item.getStringValue();
569 String lRes;
570 if (envS == "zorba.module.path") {
571 std::vector<String> lModulePaths;
572 sctx->getFullModulePaths(lModulePaths);
573 if (lModulePaths.size() == 0)
574 return ItemSequence_t(new SingletonItemSequence(theFactory->createString("")));
575 lRes = lModulePaths[0];
576 for (std::vector<String>::iterator i = lModulePaths.begin() + 1; i != lModulePaths.end(); ++i) {
577#ifdef WIN32
578 lRes += ";";
579#else
580 lRes += ":";
581#endif
582 lRes += *i;
583 }
584 } else if (envS.substr(0,4) == "env.") {
585 //Sleep(5000);
586 if (!getEnv(envS.substr(4), lRes)) {
587 return ItemSequence_t(new EmptySequence());
588 }
589#ifdef LINUX
590 } else if (envS == "linux.distributor") {
591 lRes = getDistribution().first;
592 } else if (envS == "linux.distributor.version") {
593 lRes = getDistribution().second;
594#endif
595 } else {
596 std::map<String, String>::const_iterator i;
597 if ((i = theProperties.find(envS.c_str())) != theProperties.end()) {
598 lRes = i->second;
599 } else {
600 return ItemSequence_t(new EmptySequence());
601 }
602 }
603 return ItemSequence_t(new SingletonItemSequence(theFactory->createString(lRes)));
604 }
605}} // namespace zorba, system
606
0607
=== added file 'src/com/zorba-xquery/www/modules/system.xq.src/system.h'
--- src/com/zorba-xquery/www/modules/system.xq.src/system.h 1970-01-01 00:00:00 +0000
+++ src/com/zorba-xquery/www/modules/system.xq.src/system.h 2012-09-27 16:59:25 +0000
@@ -0,0 +1,93 @@
1/*
2 * Copyright 2006-2008 The FLWOR Foundation.
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#ifndef __COM_ZORBA_WWW_MODULES_SYSTEM_H__
18#define __COM_ZORBA_WWW_MODULES_SYSTEM_H__
19#include <vector>
20#include <map>
21
22#include <zorba/zorba.h>
23#include <zorba/external_module.h>
24#include <zorba/function.h>
25
26namespace zorba { namespace system {
27 class SystemModule : public ExternalModule {
28 private:
29 ExternalFunction* thePropertyFunction;
30 ExternalFunction* thePropertiesFunction;
31 const static String SYSTEM_MODULE_NAMESPACE;
32 public:
33 SystemModule();
34 virtual ~SystemModule();
35 public:
36 virtual String getURI() const { return SYSTEM_MODULE_NAMESPACE; }
37
38 virtual ExternalFunction* getExternalFunction(const String& localName);
39
40 virtual void destroy();
41 };
42
43 class SystemFunction {
44 protected:
45 const ExternalModule* theModule;
46 ItemFactory* theFactory;
47 std::map<String, String> theProperties;
48 public:
49 SystemFunction(const ExternalModule* aModule);
50 protected:
51 String getURI() const { return theModule->getURI(); }
52 bool getEnv(const String& name, String& value) const;
53 void getEnvNames(std::vector<Item>& names) const;
54 String intToString(int v);
55 };
56
57 class PropertiesFunction : public NonContextualExternalFunction, public SystemFunction {
58 public:
59 PropertiesFunction(const ExternalModule* mod) : SystemFunction(mod) {}
60
61 virtual String getLocalName() const { return "properties"; }
62
63 virtual ItemSequence_t
64 evaluate(const ExternalFunction::Arguments_t& args) const;
65 virtual String getURI() const { return SystemFunction::getURI(); }
66 };
67
68 class PropertyFunction : public ContextualExternalFunction, public SystemFunction {
69 public:
70 PropertyFunction(const ExternalModule* mod) : SystemFunction(mod) {}
71
72 virtual String getLocalName() const { return "property"; }
73
74 virtual ItemSequence_t
75 evaluate(const ExternalFunction::Arguments_t& args,
76 const StaticContext* sctx,
77 const DynamicContext* dctx) const;
78 virtual String getURI() const { return SystemFunction::getURI(); }
79 };
80
81} } // namespace zorba, namespace system
82
83#ifdef WIN32
84# define DLL_EXPORT __declspec(dllexport)
85#else
86# define DLL_EXPORT __attribute__ ((visibility("default")))
87#endif
88
89extern "C" DLL_EXPORT zorba::ExternalModule* createModule() {
90 return new zorba::system::SystemModule();
91}
92
93#endif // __COM_ZORBA_WWW_MODULES_SYSTEM_H__

Subscribers

People subscribed via source and target branches

to all changes: