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
1=== added file 'CMakeLists.txt'
2--- CMakeLists.txt 1970-01-01 00:00:00 +0000
3+++ CMakeLists.txt 2012-09-27 16:59:25 +0000
4@@ -0,0 +1,29 @@
5+# Copyright 2006-2010 The FLWOR Foundation.
6+#
7+# Licensed under the Apache License, Version 2.0 (the "License");
8+# you may not use this file except in compliance with the License.
9+# You may obtain a copy of the License at
10+#
11+# http://www.apache.org/licenses/LICENSE-2.0
12+#
13+# Unless required by applicable law or agreed to in writing, software
14+# distributed under the License is distributed on an "AS IS" BASIS,
15+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+# See the License for the specific language governing permissions and
17+# limitations under the License.
18+
19+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
20+
21+PROJECT (zorba_system_module)
22+ENABLE_TESTING ()
23+INCLUDE (CTest)
24+
25+LIST (APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake_modules")
26+
27+FIND_PACKAGE (Zorba REQUIRED HINTS "${ZORBA_BUILD_DIR}")
28+INCLUDE ("${Zorba_USE_FILE}")
29+
30+ADD_TEST_DIRECTORY("${PROJECT_SOURCE_DIR}/test")
31+ADD_SUBDIRECTORY("src")
32+
33+DONE_DECLARING_ZORBA_URIS()
34
35=== renamed file 'CMakeLists.txt' => 'CMakeLists.txt.moved'
36=== added directory 'src'
37=== renamed directory 'src' => 'src.moved'
38=== added file 'src/CMakeLists.txt'
39--- src/CMakeLists.txt 1970-01-01 00:00:00 +0000
40+++ src/CMakeLists.txt 2012-09-27 16:59:25 +0000
41@@ -0,0 +1,19 @@
42+# Copyright 2006-2008 The FLWOR Foundation.
43+#
44+# Licensed under the Apache License, Version 2.0 (the "License");
45+# you may not use this file except in compliance with the License.
46+# You may obtain a copy of the License at
47+#
48+# http://www.apache.org/licenses/LICENSE-2.0
49+#
50+# Unless required by applicable law or agreed to in writing, software
51+# distributed under the License is distributed on an "AS IS" BASIS,
52+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
53+# See the License for the specific language governing permissions and
54+# limitations under the License.
55+
56+# all external module libraries are generated in the directory
57+# of the corresponding .xq file
58+MESSAGE(STATUS "Add com")
59+ADD_SUBDIRECTORY(com)
60+MESSAGE(STATUS "End modules")
61
62=== added directory 'src/com'
63=== added file 'src/com/CMakeLists.txt'
64--- src/com/CMakeLists.txt 1970-01-01 00:00:00 +0000
65+++ src/com/CMakeLists.txt 2012-09-27 16:59:25 +0000
66@@ -0,0 +1,14 @@
67+# Copyright 2006-2008 The FLWOR Foundation.
68+#
69+# Licensed under the Apache License, Version 2.0 (the "License");
70+# you may not use this file except in compliance with the License.
71+# You may obtain a copy of the License at
72+#
73+# http://www.apache.org/licenses/LICENSE-2.0
74+#
75+# Unless required by applicable law or agreed to in writing, software
76+# distributed under the License is distributed on an "AS IS" BASIS,
77+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
78+# See the License for the specific language governing permissions and
79+# limitations under the License.
80+ADD_SUBDIRECTORY(zorba-xquery)
81
82=== added directory 'src/com/zorba-xquery'
83=== added file 'src/com/zorba-xquery/CMakeLists.txt'
84--- src/com/zorba-xquery/CMakeLists.txt 1970-01-01 00:00:00 +0000
85+++ src/com/zorba-xquery/CMakeLists.txt 2012-09-27 16:59:25 +0000
86@@ -0,0 +1,14 @@
87+# Copyright 2006-2008 The FLWOR Foundation.
88+#
89+# Licensed under the Apache License, Version 2.0 (the "License");
90+# you may not use this file except in compliance with the License.
91+# You may obtain a copy of the License at
92+#
93+# http://www.apache.org/licenses/LICENSE-2.0
94+#
95+# Unless required by applicable law or agreed to in writing, software
96+# distributed under the License is distributed on an "AS IS" BASIS,
97+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
98+# See the License for the specific language governing permissions and
99+# limitations under the License.
100+ADD_SUBDIRECTORY(www)
101
102=== added directory 'src/com/zorba-xquery/www'
103=== added file 'src/com/zorba-xquery/www/CMakeLists.txt'
104--- src/com/zorba-xquery/www/CMakeLists.txt 1970-01-01 00:00:00 +0000
105+++ src/com/zorba-xquery/www/CMakeLists.txt 2012-09-27 16:59:25 +0000
106@@ -0,0 +1,14 @@
107+# Copyright 2006-2008 The FLWOR Foundation.
108+#
109+# Licensed under the Apache License, Version 2.0 (the "License");
110+# you may not use this file except in compliance with the License.
111+# You may obtain a copy of the License at
112+#
113+# http://www.apache.org/licenses/LICENSE-2.0
114+#
115+# Unless required by applicable law or agreed to in writing, software
116+# distributed under the License is distributed on an "AS IS" BASIS,
117+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
118+# See the License for the specific language governing permissions and
119+# limitations under the License.
120+ADD_SUBDIRECTORY(modules)
121
122=== added directory 'src/com/zorba-xquery/www/modules'
123=== added file 'src/com/zorba-xquery/www/modules/CMakeLists.txt'
124--- src/com/zorba-xquery/www/modules/CMakeLists.txt 1970-01-01 00:00:00 +0000
125+++ src/com/zorba-xquery/www/modules/CMakeLists.txt 2012-09-27 16:59:25 +0000
126@@ -0,0 +1,14 @@
127+# Copyright 2006-2008 The FLWOR Foundation.
128+#
129+# Licensed under the Apache License, Version 2.0 (the "License");
130+# you may not use this file except in compliance with the License.
131+# You may obtain a copy of the License at
132+#
133+# http://www.apache.org/licenses/LICENSE-2.0
134+#
135+# Unless required by applicable law or agreed to in writing, software
136+# distributed under the License is distributed on an "AS IS" BASIS,
137+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138+# See the License for the specific language governing permissions and
139+# limitations under the License.
140+DECLARE_ZORBA_MODULE (URI "http://www.zorba-xquery.com/modules/system" VERSION 1.0 FILE "system.xq")
141
142=== added file 'src/com/zorba-xquery/www/modules/system.xq'
143--- src/com/zorba-xquery/www/modules/system.xq 1970-01-01 00:00:00 +0000
144+++ src/com/zorba-xquery/www/modules/system.xq 2012-09-27 16:59:25 +0000
145@@ -0,0 +1,211 @@
146+xquery version "3.0";
147+
148+(:
149+ : Copyright 2006-2009 The FLWOR Foundation.
150+ :
151+ : Licensed under the Apache License, Version 2.0 (the "License");
152+ : you may not use this file except in compliance with the License.
153+ : You may obtain a copy of the License at
154+ :
155+ : http://www.apache.org/licenses/LICENSE-2.0
156+ :
157+ : Unless required by applicable law or agreed to in writing, software
158+ : distributed under the License is distributed on an "AS IS" BASIS,
159+ : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
160+ : See the License for the specific language governing permissions and
161+ : limitations under the License.
162+:)
163+(:~
164+ : The system module allows developers to access system properties.
165+ : Part of these system properties are environment variables,
166+ : local variable to the process running Zorba, and properties defined by Zorba.
167+ : <br />
168+ : To avoid conflicts between environment variables and properties defined by Zorba,
169+ : all environment variables are prefixed with <i>env.</i>.
170+ : <br />
171+ : For instance, the following query: <br />
172+ : <pre class="ace-static">
173+ : import module namespace system = "http://www.zorba-xquery.com/modules/system";
174+ :
175+ : for $prop in system:properties()
176+ : return concat($prop, ": ", system:property($prop), "
177+ : ")
178+ : </pre>
179+ : <br />
180+ : Will output:
181+ : <pre class="ace-static">
182+ : env.TERM_PROGRAM: Apple_Terminal
183+ : ...
184+ : </pre>
185+ : In this example, it is important to notice that the environnement variable PATH
186+ : with the key env.PATH.
187+ :
188+ : @author Markus Pilman
189+ : @project Zorba/IO/System
190+ :)
191+module namespace system = 'http://www.zorba-xquery.com/modules/system';
192+
193+declare namespace an = "http://www.zorba-xquery.com/annotations";
194+
195+declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
196+declare option ver:module-version "1.0";
197+
198+(:~
199+ : The name of the operating system (os.name).
200+ :)
201+declare variable $system:os-name as xs:string := "os.name";
202+
203+(:~
204+ : The name of the computer the process is running on (os.node.name).
205+ :)
206+declare variable $system:os-node-name as xs:string := "os.node.name";
207+
208+(:~
209+ : The major version number of the Windows installation or
210+ : an empty string if the process does not run on a Windows installation
211+ : (os.version.major).
212+ : <b>Works on Windows only.</b>
213+ :)
214+declare variable $system:os-version-major as xs:string := "os.version.major";
215+
216+(:~
217+ : The minor version number of the Windows installation or
218+ : an empty string if the process does not run on a Windows installation
219+ : (os.version.minor).
220+ : <b>Works on Windows only.</b>
221+ :)
222+declare variable $system:os-version-minor as xs:string := "os.version.minor";
223+
224+(:~
225+ : The build number of the Windows installation or
226+ : an empty string if the process does not run on a Windows installation
227+ : (os.version.build).
228+ : <b>Works on Windows only.</b>
229+ :)
230+declare variable $system:os-version-build as xs:string := "os.version.build";
231+
232+(:~
233+ : The release of this UNIX installation or
234+ : an empty string if the process does not run on a UNIX/Linux installation
235+ : (os.version.release).
236+ : <b>Works on UNIX based operating systems only.</b>
237+ :)
238+declare variable $system:os-version-release as xs:string := "os.version.release";
239+(:~
240+ : The version of this UNIX installation or
241+ : an empty string if the process does not run on a UNIX/Linux installation
242+ : (os.version.version).
243+ : <b>Works on UNIX based operating systems only.</b>
244+ :)
245+declare variable $system:os-version-version as xs:string := "os.version.version";
246+
247+(:~
248+ : The version of the Operating System.
249+ :)
250+declare variable $system:os-version as xs:string := "os.version";
251+
252+(:~
253+ : The name of the processor architecture (os.arch).
254+ : For example x86 or x86_64.
255+ :)
256+declare variable $system:os-arch as xs:string := "os.arch";
257+
258+(:~
259+ : True if system architecture is 64bits (os.is64).
260+ :)
261+declare variable $system:os-is64 as xs:string := "os.is64";
262+
263+(:~
264+ : Number of logical processors in the system (hardware.logical.cpu).
265+ : This information is not available under Mac OS X.
266+ :)
267+declare variable $system:hardware-logical-cpu as xs:string := "hardware.logical.cpu";
268+
269+(:~
270+ : Number of physical processors in the system (hardware.logical.cpu).
271+ :)
272+declare variable $system:hardware-physical-cpu as xs:string := "hardware.physical.cpu";
273+
274+(:~
275+ : number of logical per physical processors in the system (hardware.logical.per.physical.cpu).
276+ : This information is not available under Mac OS X.
277+ :)
278+declare variable $system:hardware-logical-per-physical-cpu as xs:string := "hardware.logical.per.physical.cpu";
279+
280+(:~
281+ : Physical memory available (hardware.physical.memory).
282+ :)
283+declare variable $system:hardware-physical-memory as xs:string := "hardware.physical.memory";
284+(:~
285+ : Virtual memory available (hardware.virtual.memory).
286+ :)
287+declare variable $system:hardware-virtual-memory as xs:string := "hardware.virtual.memory";
288+(:~
289+ : Gets the hardware manufacturer (hardware.manufacturer).
290+ :)
291+declare variable $system:hardware-manufacturer as xs:string := "hardware.manufacturer";
292+
293+(:~
294+ : The Linux distribution, Zorba is running on (linux.distributor).
295+ : <b>Works on UNIX based operating systems only.</b>
296+ :)
297+declare variable $system:linux-distributor as xs:string := "linux.distributor";
298+
299+(:~
300+ : The version of the Linux distribution, Zorba is running on (linux.distributor.version).
301+ : <b>Works on UNIX based operating systems only.</b>
302+ :)
303+declare variable $system:linux-distributor-version as xs:string := "linux.distributor.version";
304+
305+(:~
306+ : The username, with which this process was started (user.name).
307+ : On Unix, this variable is only available if the USER environment
308+ : variable is set (e.g. it might not be available in a cronjob).
309+ :)
310+declare variable $system:user-name as xs:string := "user.name";
311+
312+(:~
313+ : The Zorba module path, that is the paths in which Zorba looks
314+ : for modules (zorba.module.path).
315+ :)
316+declare variable $system:zorba-module-path as xs:string := "zorba.module.path";
317+
318+(:~
319+ : Zorba version in the format Major.Minor.Patch (zorba.version).
320+ :)
321+declare variable $system:zorba-version as xs:string := "zorba.version";
322+
323+(:~
324+ : Zorba major version (zorba.version.major).
325+ :)
326+declare variable $system:zorba-version-major as xs:string := "zorba.version.major";
327+
328+(:~
329+ : Zorba minor version (zorba.version.minor).
330+ :)
331+declare variable $system:zorba-version-minor as xs:string := "zorba.version.minor";
332+
333+(:~
334+ : Zorba patch version (zorba.version.patch).
335+ :)
336+declare variable $system:zorba-version-patch as xs:string := "zorba.version.patch";
337+
338+(:~
339+ : Gets the system property indicated by the specified key.
340+ :
341+ : @param $key The name of the system property.
342+ : @return The string value of the system property, or an empty sequence if there is no property with that key.
343+ :)
344+declare %an:nondeterministic function system:property($key as xs:string) as xs:string? external;
345+
346+(:~
347+ : This function retrieves the names of the current system properties.
348+ : This list includes environment variables, local variable to the process running Zorba, and properties defined by Zorba.
349+ : <br />
350+ : To avoid conflicts between environment variables and properties defined by Zorba,
351+ : all environment variables are prefixed with <i>env.</i>.
352+ :
353+ : @return List of all system properties.
354+ :)
355+declare %an:nondeterministic function system:properties() as xs:string* external;
356+
357
358=== added directory 'src/com/zorba-xquery/www/modules/system.xq.src'
359=== added file 'src/com/zorba-xquery/www/modules/system.xq.src/system.cpp'
360--- src/com/zorba-xquery/www/modules/system.xq.src/system.cpp 1970-01-01 00:00:00 +0000
361+++ src/com/zorba-xquery/www/modules/system.xq.src/system.cpp 2012-09-27 16:59:25 +0000
362@@ -0,0 +1,606 @@
363+/*
364+ * Copyright 2006-2008 The FLWOR Foundation.
365+ *
366+ * Licensed under the Apache License, Version 2.0 (the "License");
367+ * you may not use this file except in compliance with the License.
368+ * You may obtain a copy of the License at
369+ *
370+ * http://www.apache.org/licenses/LICENSE-2.0
371+ *
372+ * Unless required by applicable law or agreed to in writing, software
373+ * distributed under the License is distributed on an "AS IS" BASIS,
374+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
375+ * See the License for the specific language governing permissions and
376+ * limitations under the License.
377+ */
378+#include <cstdlib>
379+#include <cstdio>
380+#include <sstream>
381+
382+#ifdef WIN32
383+# include <Windows.h>
384+# include <malloc.h>
385+# include <stdio.h>
386+# include <tchar.h>
387+# include <winreg.h>
388+#else
389+#include <sys/utsname.h>
390+# ifndef __APPLE__
391+# include <sys/sysinfo.h>
392+# else
393+# include <sys/param.h>
394+# include <sys/sysctl.h>
395+# endif
396+#endif
397+
398+#include <zorba/zorba_string.h>
399+#include <zorba/singleton_item_sequence.h>
400+#include <zorba/vector_item_sequence.h>
401+#include <zorba/empty_sequence.h>
402+#include <zorba/item_factory.h>
403+
404+
405+#ifdef LINUX
406+#include <iostream>
407+#include <string>
408+#include <fstream>
409+#include <unistd.h>
410+extern char** environ;
411+#elif defined APPLE
412+# include <crt_externs.h>
413+#endif
414+
415+#include "system.h"
416+
417+
418+namespace zorba { namespace system {
419+
420+ const String SystemModule::SYSTEM_MODULE_NAMESPACE = "http://www.zorba-xquery.com/modules/system";
421+
422+#ifdef WIN32
423+ typedef BOOL (WINAPI *LPFN_GLPI)(
424+ PSYSTEM_LOGICAL_PROCESSOR_INFORMATION,
425+ PDWORD);
426+
427+ // Helper function to count set bits in the processor mask.
428+ DWORD CountSetBits(ULONG_PTR bitMask)
429+ {
430+ DWORD LSHIFT = sizeof(ULONG_PTR)*8 - 1;
431+ DWORD bitSetCount = 0;
432+ ULONG_PTR bitTest = (ULONG_PTR)1 << LSHIFT;
433+ DWORD i;
434+ for (i = 0; i <= LSHIFT; ++i)
435+ {
436+ bitSetCount += ((bitMask & bitTest)?1:0);
437+ bitTest/=2;
438+ }
439+
440+ return bitSetCount;
441+ }
442+
443+ DWORD numaNodeCount = 0;
444+ DWORD processorPackageCount = 0;
445+ DWORD logicalProcessorCount = 0;
446+ DWORD processorCoreCount = 0;
447+ DWORD processorL1CacheCount = 0;
448+ DWORD processorL2CacheCount = 0;
449+ DWORD processorL3CacheCount = 0;
450+ static void countProcessors() {
451+ LPFN_GLPI glpi;
452+ BOOL done = FALSE;
453+ PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
454+ PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
455+ DWORD returnLength = 0;
456+ DWORD byteOffset = 0;
457+ PCACHE_DESCRIPTOR Cache;
458+
459+ glpi = (LPFN_GLPI) GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformation");
460+ if (NULL == glpi) {
461+ // GetLogicalProcessorInformation is not supported.
462+ return;
463+ }
464+
465+ while (!done)
466+ {
467+ DWORD rc = glpi(buffer, &returnLength);
468+ if (FALSE == rc)
469+ {
470+ if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
471+ {
472+ if (buffer)
473+ free(buffer);
474+ buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);
475+ if (NULL == buffer)
476+ {
477+ // Error: Allocation failure
478+ return;
479+ }
480+ } else {
481+ // Error %d, GetLastError()
482+ return;
483+ }
484+ } else {
485+ done = TRUE;
486+ }
487+ }
488+ ptr = buffer;
489+ while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength)
490+ {
491+ switch (ptr->Relationship)
492+ {
493+ case RelationNumaNode:
494+ // Non-NUMA systems report a single record of this type.
495+ numaNodeCount++;
496+ break;
497+ case RelationProcessorCore:
498+ processorCoreCount++;
499+ // A hyperthreaded core supplies more than one logical processor.
500+ logicalProcessorCount += CountSetBits(ptr->ProcessorMask);
501+ break;
502+
503+ case RelationCache:
504+ // Cache data is in ptr->Cache, one CACHE_DESCRIPTOR structure for each cache.
505+ Cache = &ptr->Cache;
506+ if (Cache->Level == 1)
507+ {
508+ processorL1CacheCount++;
509+ }
510+ else if (Cache->Level == 2)
511+ {
512+ processorL2CacheCount++;
513+ }
514+ else if (Cache->Level == 3)
515+ {
516+ processorL3CacheCount++;
517+ }
518+ break;
519+ case RelationProcessorPackage:
520+ // Logical processors share a physical package.
521+ processorPackageCount++;
522+ break;
523+ default:
524+ // Error: Unsupported LOGICAL_PROCESSOR_RELATIONSHIP value.
525+ break;
526+ }
527+ byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
528+ ptr++;
529+ }
530+ free(buffer);
531+ return;
532+ }
533+
534+#endif
535+
536+#ifdef LINUX
537+ static void trim(std::string& str, char delim)
538+ {
539+ std::string::size_type pos = str.find_last_not_of(delim);
540+ if(pos != std::string::npos) {
541+ str.erase(pos + 1);
542+ pos = str.find_first_not_of(delim);
543+ if(pos != std::string::npos) str.erase(0, pos);
544+ }
545+ else str.erase(str.begin(), str.end());
546+ }
547+
548+
549+ int logical=0;
550+ int cores=0;
551+ int physical=0;
552+
553+
554+ static void countProcessors() {
555+ logical=0;
556+ cores=1; // per default: single core processor
557+ physical=0;
558+
559+ std::ifstream in("/proc/cpuinfo");
560+ if(in) {
561+ std::string name;
562+ std::string value;
563+
564+ while(in) {
565+ getline(in, name, ':');
566+ trim (name, ' ');
567+ trim (name, '\t');
568+ trim (name, '\n');
569+ getline(in, value);
570+ trim (value, ' ');
571+ trim (value, '\t');
572+ if (name == "processor") {
573+ logical++;
574+ }
575+
576+ if (name == "cpu cores") {
577+ cores = atoi(value.c_str());
578+ }
579+ }
580+ physical = logical/cores;
581+ in.close();
582+ }
583+ }
584+
585+
586+
587+ static std::pair<std::string, std::string> getDistribution() {
588+ std::pair<std::string, std::string> lRes;
589+ FILE *pipe;
590+ const char* command = "lsb_release -r -i";
591+ pipe = (FILE*) popen(command, "r");
592+
593+ char line[1024];
594+ while (fgets(line, sizeof(line), pipe)) {
595+ std::stringstream s(line);
596+ std::string name, value;
597+ getline(s, name, ':');
598+ trim(name, ' ');
599+ trim(name, '\t');
600+ getline(s, value, ':');
601+ trim(value, ' ');
602+ trim(value, '\t');
603+ trim(value, '\n');
604+ if (name == "Distributor ID") {
605+ lRes.first = value;
606+ } else {
607+ lRes.second = value;
608+ }
609+ }
610+ return lRes;
611+ }
612+#endif
613+
614+ SystemModule::SystemModule()
615+ : thePropertyFunction(0), thePropertiesFunction(0)
616+ {
617+ }
618+
619+ ExternalFunction* SystemModule::getExternalFunction(const String& localName) {
620+ if (localName == "properties") {
621+ if (!thePropertiesFunction)
622+ thePropertiesFunction = new PropertiesFunction(this);
623+ return thePropertiesFunction;
624+ } else if (localName == "property") {
625+ if (!thePropertyFunction)
626+ thePropertyFunction = new PropertyFunction(this);
627+ return thePropertyFunction;
628+ }
629+ return 0;
630+ }
631+
632+ void SystemModule::destroy() {
633+ delete this;
634+ }
635+
636+ SystemModule::~SystemModule() {
637+ delete thePropertyFunction;
638+ delete thePropertiesFunction;
639+ }
640+
641+
642+ SystemFunction::SystemFunction(const ExternalModule* aModule)
643+ : theModule(aModule), theFactory(Zorba::getInstance(0)->getItemFactory())
644+ {
645+#ifdef WIN32
646+
647+ {
648+ DWORD nodeNameLength = MAX_COMPUTERNAME_LENGTH + 1;
649+ TCHAR nodeName[MAX_COMPUTERNAME_LENGTH + 1];
650+ char nodeNameC[MAX_COMPUTERNAME_LENGTH + 1];
651+ GetComputerName(nodeName, &nodeNameLength);
652+ for (DWORD i = 0; i < nodeNameLength; ++i) {
653+ nodeNameC[i] = static_cast<char>(nodeName[i]);
654+ }
655+ nodeNameC[nodeNameLength] = NULL; // Terminate string
656+ theProperties.insert(std::make_pair("os.node.name", nodeNameC));
657+ }
658+
659+ {
660+ DWORD dwVersion = 0;
661+ DWORD dwMajorVersion = 0;
662+ DWORD dwMinorVersion = 0;
663+ DWORD dwBuild = 0;
664+
665+ dwVersion = GetVersion();
666+
667+ // Get the Windows version.
668+ dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
669+ dwMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
670+
671+ // Get the build number.
672+ if (dwVersion < 0x80000000)
673+ dwBuild = (DWORD)(HIWORD(dwVersion));
674+
675+ std::string major;
676+ std::string minor;
677+ std::string build;
678+ {
679+ std::stringstream sMajor;
680+ sMajor << dwMajorVersion;
681+ std::stringstream sMinor;
682+ sMinor << dwMinorVersion;
683+ std::stringstream sBuild;
684+ sBuild << dwBuild;
685+
686+ major = sMajor.str();
687+ minor = sMinor.str();
688+ build = sBuild.str();
689+ }
690+ theProperties.insert(std::make_pair("os.version.major", major));
691+ theProperties.insert(std::make_pair("os.version.minor", minor));
692+ theProperties.insert(std::make_pair("os.version.build", build));
693+ theProperties.insert(std::make_pair("os.version", major + "." + minor + "." + build));
694+ // http://msdn.microsoft.com/en-us/library/ms724832(v=VS.85).aspx
695+ std::string operativeSystem;
696+ theProperties.insert(std::make_pair("os.name", "Windows"));
697+ {
698+ countProcessors();
699+ std::stringstream logicalProcessors;
700+ logicalProcessors << processorPackageCount;
701+ std::stringstream physicalProcessors;
702+ physicalProcessors << logicalProcessorCount;
703+ std::stringstream logicalPerPhysicalProcessors;
704+ logicalPerPhysicalProcessors << (logicalProcessorCount / processorPackageCount );
705+ theProperties.insert(std::make_pair("hardware.physical.cpu", logicalProcessors.str() ));
706+ theProperties.insert(std::make_pair("hardware.logical.cpu", physicalProcessors.str() ));
707+ theProperties.insert(std::make_pair("hardware.logical.per.physical.cpu", logicalPerPhysicalProcessors.str() ));
708+ }
709+ {
710+ MEMORYSTATUSEX statex;
711+ statex.dwLength = sizeof (statex);
712+ GlobalMemoryStatusEx (&statex);
713+ std::stringstream virtualMemory;
714+ virtualMemory << statex.ullTotalVirtual;
715+ std::stringstream physicalMemory;
716+ physicalMemory << statex.ullTotalPhys;
717+ theProperties.insert(std::make_pair("hardware.virtual.memory", virtualMemory.str() ));
718+ theProperties.insert(std::make_pair("hardware.physical.memory", physicalMemory.str() ));
719+ }
720+
721+ }
722+ {
723+ DWORD userNameLength = 1023;
724+ TCHAR userName[1024];
725+ char userNameC[1024];
726+ GetUserName(userName, &userNameLength);
727+ for (DWORD i = 0; i < userNameLength; ++i) {
728+ userNameC[i] = static_cast<char>(userName[i]);
729+ }
730+ theProperties.insert(std::make_pair("user.name", userNameC));
731+ }
732+ {
733+ SYSTEM_INFO info;
734+ GetSystemInfo(&info);
735+ if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
736+ theProperties.insert(std::make_pair("os.arch", "x86_64"));
737+ theProperties.insert(std::make_pair("os.is64", "true"));
738+ } else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64) {
739+ theProperties.insert(std::make_pair("os.arch", "ia64"));
740+ theProperties.insert(std::make_pair("os.is64", "true"));
741+ } else if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL) {
742+ theProperties.insert(std::make_pair("os.arch", "i386"));
743+ theProperties.insert(std::make_pair("os.is64", "false"));
744+ }
745+ }
746+
747+ {
748+ HKEY keyHandle;
749+ TCHAR value [1024];
750+ char valueC [1024];
751+ DWORD size = 0;
752+ DWORD Type;
753+ if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\BIOS", 0, KEY_QUERY_VALUE, &keyHandle) == ERROR_SUCCESS)
754+ {
755+ RegQueryValueEx( keyHandle, L"SystemManufacturer", NULL, &Type, (LPBYTE)value, &size);
756+ for (DWORD i = 0; i < size; ++i) {
757+ valueC[i] = static_cast<char>(value[i]);
758+ }
759+ if (size > 0)
760+ theProperties.insert(std::make_pair("hardware.manufacturer", valueC));
761+ }
762+ RegCloseKey(keyHandle);
763+ }
764+
765+
766+#else
767+ struct utsname osname;
768+ if (uname(&osname) == 0)
769+ {
770+ theProperties.insert(std::make_pair("os.name", osname.sysname));
771+ theProperties.insert(std::make_pair("os.node.name", osname.nodename));
772+ theProperties.insert(std::make_pair("os.version.release", osname.release));
773+ theProperties.insert(std::make_pair("os.version.version", osname.version));
774+ theProperties.insert(std::make_pair("os.version", osname.release));
775+ theProperties.insert(std::make_pair("os.arch", osname.machine));
776+ }
777+ char* lUser = getenv("USER");
778+ if (lUser)
779+ {
780+ theProperties.insert(std::make_pair("user.name", lUser));
781+ }
782+ theProperties.insert(std::make_pair("os.is64", "false"));
783+ {
784+#ifdef __APPLE__
785+ int mib[2];
786+ size_t len = 4;
787+ uint32_t res = 0;
788+
789+ mib[0] = CTL_HW;
790+ mib[1] = HW_NCPU;
791+ sysctl(mib, 2, &res, &len, NULL, NULL);
792+ std::stringstream lStream;
793+ lStream << res;
794+ theProperties.insert(std::make_pair("hardware.physical.cpu", lStream.str()));
795+#else
796+ countProcessors();
797+ std::stringstream logicalProcessor;
798+ std::stringstream physicalProcessor;
799+ std::stringstream logicalPerPhysicalProcessors;
800+ logicalProcessor << logical;
801+ physicalProcessor << physical;
802+ logicalPerPhysicalProcessors << cores;
803+ theProperties.insert(std::make_pair("hardware.logical.per.physical.cpu", logicalPerPhysicalProcessors.str() ));
804+ theProperties.insert(std::make_pair("hardware.physical.cpu", physicalProcessor.str() ));
805+ theProperties.insert(std::make_pair("hardware.logical.cpu", logicalProcessor.str() ));
806+#endif
807+ }
808+ {
809+# ifdef LINUX
810+ struct sysinfo sys_info;
811+ if(sysinfo(&sys_info) == 0) {
812+ std::stringstream memory;
813+ memory << sys_info.totalram;
814+ std::stringstream swap;
815+ swap << sys_info.totalswap;
816+ theProperties.insert(std::make_pair("hardware.virtual.memory", swap.str() ));
817+ theProperties.insert(std::make_pair("hardware.physical.memory", memory.str() ));
818+ }
819+# elif defined __APPLE__
820+ int mib[2];
821+ size_t len = 8;
822+ uint64_t res = 0;
823+
824+ mib[0] = CTL_HW;
825+ mib[1] = HW_MEMSIZE;
826+ sysctl(mib, 2, &res, &len, NULL, NULL);
827+ std::stringstream lStream;
828+ lStream << res;
829+ theProperties.insert(std::make_pair("hardware.physical.memory", lStream.str()));
830+# endif
831+ }
832+
833+#endif
834+#ifdef LINUX
835+ theProperties.insert(std::make_pair("linux.distributor", ""));
836+ theProperties.insert(std::make_pair("linux.distributor.version", ""));
837+#endif
838+ theProperties.insert(std::make_pair("zorba.version", Zorba::version().getVersion()));
839+ theProperties.insert(std::make_pair("zorba.version.major", intToString(Zorba::version().getMajorVersion())));
840+ theProperties.insert(std::make_pair("zorba.version.minor", intToString(Zorba::version().getMinorVersion())));
841+ theProperties.insert(std::make_pair("zorba.version.patch", intToString(Zorba::version().getPatchVersion())));
842+ }
843+
844+ String SystemFunction::intToString(int v) {
845+ std::stringstream ss;
846+ ss << v;
847+ return ss.str();
848+ }
849+
850+ bool SystemFunction::getEnv(const String& name, String& value) const
851+ {
852+ char* v = getenv(name.c_str());
853+ if (v == NULL) return false;
854+ value = v;
855+ return true;
856+ }
857+
858+ void SystemFunction::getEnvNames(std::vector<Item>& names) const
859+ {
860+#ifdef WIN32
861+ // put in the environment variables
862+ TCHAR *l_EnvStr;
863+ l_EnvStr = GetEnvironmentStrings();
864+
865+ LPTSTR l_str = l_EnvStr;
866+
867+ int count = 0;
868+ while (true)
869+ {
870+ if (*l_str == 0) break;
871+ while (*l_str != 0) l_str++;
872+ l_str++;
873+ count++;
874+ }
875+
876+ for (int i = 0; i < count; i++)
877+ {
878+ char lStr[1024];
879+ memset(lStr, 0, 1024);
880+ for (int i =0; i<1023 && l_EnvStr[i]; ++i) {
881+ lStr[i] = (char) l_EnvStr[i];
882+ }
883+ std::string e(lStr);
884+ std::string name("env.");
885+ name += e.substr(0, e.find('='));
886+ String value = e.substr(e.find('=') + 1);
887+ if (name != "env.")
888+ names.push_back(theFactory->createString(name));
889+ while(*l_EnvStr != '\0')
890+ l_EnvStr++;
891+ l_EnvStr++;
892+ }
893+ //FreeEnvironmentStrings(l_EnvStr);
894+#else
895+# ifdef APPLE
896+ char** environ = *_NSGetEnviron();
897+# endif // APPLE
898+ for (int i = 0; environ[i] != NULL; ++i) {
899+ std::string e(environ[i]);
900+ String name("env.");
901+ name += e.substr(0, e.find('='));
902+ names.push_back(theFactory->createString(name));
903+ }
904+#endif
905+ }
906+
907+ ItemSequence_t PropertiesFunction::evaluate(
908+ const ExternalFunction::Arguments_t& args) const {
909+ std::vector<Item> lRes;
910+ getEnvNames(lRes);
911+ for (std::map<String, String>::const_iterator i = theProperties.begin();
912+ i != theProperties.end(); ++i) {
913+ Item lItem = theFactory->createString(i->first.c_str());
914+ lRes.push_back(lItem);
915+ }
916+ // insert the zorba module path
917+ lRes.push_back(theFactory->createString("zorba.module.path"));
918+ return ItemSequence_t(new VectorItemSequence(lRes));
919+ }
920+
921+ ItemSequence_t PropertyFunction::evaluate(
922+ const ExternalFunction::Arguments_t& args,
923+ const StaticContext* sctx,
924+ const DynamicContext* dctx) const {
925+ Item item;
926+ Iterator_t arg0_iter = args[0]->getIterator();
927+ arg0_iter->open();
928+ arg0_iter->next(item);
929+ arg0_iter->close();
930+ String envS = item.getStringValue();
931+ String lRes;
932+ if (envS == "zorba.module.path") {
933+ std::vector<String> lModulePaths;
934+ sctx->getFullModulePaths(lModulePaths);
935+ if (lModulePaths.size() == 0)
936+ return ItemSequence_t(new SingletonItemSequence(theFactory->createString("")));
937+ lRes = lModulePaths[0];
938+ for (std::vector<String>::iterator i = lModulePaths.begin() + 1; i != lModulePaths.end(); ++i) {
939+#ifdef WIN32
940+ lRes += ";";
941+#else
942+ lRes += ":";
943+#endif
944+ lRes += *i;
945+ }
946+ } else if (envS.substr(0,4) == "env.") {
947+ //Sleep(5000);
948+ if (!getEnv(envS.substr(4), lRes)) {
949+ return ItemSequence_t(new EmptySequence());
950+ }
951+#ifdef LINUX
952+ } else if (envS == "linux.distributor") {
953+ lRes = getDistribution().first;
954+ } else if (envS == "linux.distributor.version") {
955+ lRes = getDistribution().second;
956+#endif
957+ } else {
958+ std::map<String, String>::const_iterator i;
959+ if ((i = theProperties.find(envS.c_str())) != theProperties.end()) {
960+ lRes = i->second;
961+ } else {
962+ return ItemSequence_t(new EmptySequence());
963+ }
964+ }
965+ return ItemSequence_t(new SingletonItemSequence(theFactory->createString(lRes)));
966+ }
967+}} // namespace zorba, system
968+
969
970=== added file 'src/com/zorba-xquery/www/modules/system.xq.src/system.h'
971--- src/com/zorba-xquery/www/modules/system.xq.src/system.h 1970-01-01 00:00:00 +0000
972+++ src/com/zorba-xquery/www/modules/system.xq.src/system.h 2012-09-27 16:59:25 +0000
973@@ -0,0 +1,93 @@
974+/*
975+ * Copyright 2006-2008 The FLWOR Foundation.
976+ *
977+ * Licensed under the Apache License, Version 2.0 (the "License");
978+ * you may not use this file except in compliance with the License.
979+ * You may obtain a copy of the License at
980+ *
981+ * http://www.apache.org/licenses/LICENSE-2.0
982+ *
983+ * Unless required by applicable law or agreed to in writing, software
984+ * distributed under the License is distributed on an "AS IS" BASIS,
985+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
986+ * See the License for the specific language governing permissions and
987+ * limitations under the License.
988+ */
989+
990+#ifndef __COM_ZORBA_WWW_MODULES_SYSTEM_H__
991+#define __COM_ZORBA_WWW_MODULES_SYSTEM_H__
992+#include <vector>
993+#include <map>
994+
995+#include <zorba/zorba.h>
996+#include <zorba/external_module.h>
997+#include <zorba/function.h>
998+
999+namespace zorba { namespace system {
1000+ class SystemModule : public ExternalModule {
1001+ private:
1002+ ExternalFunction* thePropertyFunction;
1003+ ExternalFunction* thePropertiesFunction;
1004+ const static String SYSTEM_MODULE_NAMESPACE;
1005+ public:
1006+ SystemModule();
1007+ virtual ~SystemModule();
1008+ public:
1009+ virtual String getURI() const { return SYSTEM_MODULE_NAMESPACE; }
1010+
1011+ virtual ExternalFunction* getExternalFunction(const String& localName);
1012+
1013+ virtual void destroy();
1014+ };
1015+
1016+ class SystemFunction {
1017+ protected:
1018+ const ExternalModule* theModule;
1019+ ItemFactory* theFactory;
1020+ std::map<String, String> theProperties;
1021+ public:
1022+ SystemFunction(const ExternalModule* aModule);
1023+ protected:
1024+ String getURI() const { return theModule->getURI(); }
1025+ bool getEnv(const String& name, String& value) const;
1026+ void getEnvNames(std::vector<Item>& names) const;
1027+ String intToString(int v);
1028+ };
1029+
1030+ class PropertiesFunction : public NonContextualExternalFunction, public SystemFunction {
1031+ public:
1032+ PropertiesFunction(const ExternalModule* mod) : SystemFunction(mod) {}
1033+
1034+ virtual String getLocalName() const { return "properties"; }
1035+
1036+ virtual ItemSequence_t
1037+ evaluate(const ExternalFunction::Arguments_t& args) const;
1038+ virtual String getURI() const { return SystemFunction::getURI(); }
1039+ };
1040+
1041+ class PropertyFunction : public ContextualExternalFunction, public SystemFunction {
1042+ public:
1043+ PropertyFunction(const ExternalModule* mod) : SystemFunction(mod) {}
1044+
1045+ virtual String getLocalName() const { return "property"; }
1046+
1047+ virtual ItemSequence_t
1048+ evaluate(const ExternalFunction::Arguments_t& args,
1049+ const StaticContext* sctx,
1050+ const DynamicContext* dctx) const;
1051+ virtual String getURI() const { return SystemFunction::getURI(); }
1052+ };
1053+
1054+} } // namespace zorba, namespace system
1055+
1056+#ifdef WIN32
1057+# define DLL_EXPORT __declspec(dllexport)
1058+#else
1059+# define DLL_EXPORT __attribute__ ((visibility("default")))
1060+#endif
1061+
1062+extern "C" DLL_EXPORT zorba::ExternalModule* createModule() {
1063+ return new zorba::system::SystemModule();
1064+}
1065+
1066+#endif // __COM_ZORBA_WWW_MODULES_SYSTEM_H__

Subscribers

People subscribed via source and target branches

to all changes: