Rollback from r3608 to r3606
[public/netxms.git] / src / libnxsl / main.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** NetXMS Scripting Language Interpreter
4 ** Copyright (C) 2005, 2006, 2007, 2008 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: main.cpp
21 **
22 **/
23
24 #include "libnxsl.h"
25
26
27 //
28 // For unknown reasons, min() becames undefined on Linux, despite the fact
29 // that it is defined in nms_common.h
30 //
31
32 #ifndef min
33 #define min(a, b) ((a) < (b) ? (a) : (b))
34 #endif
35
36
37 //
38 // Interface to compiler
39 //
40
41 NXSL_Program LIBNXSL_EXPORTABLE *NXSLCompile(TCHAR *pszSource,
42 TCHAR *pszError, int nBufSize)
43 {
44 NXSL_Compiler compiler;
45 NXSL_Program *pResult;
46
47 pResult = compiler.Compile(pszSource);
48 if (pResult == NULL)
49 {
50 if (pszError != NULL)
51 nx_strncpy(pszError, compiler.GetErrorText(), nBufSize);
52 }
53 return pResult;
54 }
55
56
57 //
58 // Load file into memory
59 //
60
61 TCHAR LIBNXSL_EXPORTABLE *NXSLLoadFile(TCHAR *pszFileName, DWORD *pdwFileSize)
62 {
63 int fd, iBufPos, iNumBytes, iBytesRead;
64 TCHAR *pBuffer = NULL;
65 struct stat fs;
66
67 fd = _topen(pszFileName, O_RDONLY | O_BINARY);
68 if (fd != -1)
69 {
70 if (fstat(fd, &fs) != -1)
71 {
72 pBuffer = (TCHAR *)malloc(fs.st_size + 1);
73 if (pBuffer != NULL)
74 {
75 *pdwFileSize = fs.st_size;
76 for(iBufPos = 0; iBufPos < fs.st_size; iBufPos += iBytesRead)
77 {
78 iNumBytes = min(16384, fs.st_size - iBufPos);
79 if ((iBytesRead = read(fd, &pBuffer[iBufPos], iNumBytes)) < 0)
80 {
81 free(pBuffer);
82 pBuffer = NULL;
83 break;
84 }
85 }
86 if (pBuffer != NULL)
87 {
88 for(iBufPos = 0; iBufPos < fs.st_size; iBufPos++)
89 if (pBuffer[iBufPos] == 0)
90 pBuffer[iBufPos] = ' ';
91 pBuffer[fs.st_size] = 0;
92 }
93 }
94 }
95 close(fd);
96 }
97 return pBuffer;
98 }
99
100
101 //
102 // DLL entry point
103 //
104
105 #ifdef _WIN32
106
107 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
108 {
109 if (dwReason == DLL_PROCESS_ATTACH)
110 DisableThreadLibraryCalls(hInstance);
111 return TRUE;
112 }
113
114 #endif /* _WIN32 */