d3fee09973b125fbb800927268e996dd06c052a1
[public/netxms.git] / src / nxinstall / io.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** NXSL-based installer tool collection
4 ** Copyright (C) 2005-2011 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: io.cpp
21 **
22 **/
23
24 #include "nxinstall.h"
25
26 /**
27 * Implementation of "FILE" class
28 */
29 NXSL_FileClass::NXSL_FileClass() : NXSL_Class()
30 {
31 _tcscpy(m_szName, _T("FILE"));
32 }
33
34 NXSL_Value *NXSL_FileClass::getAttr(NXSL_Object *pObject, const TCHAR *pszAttr)
35 {
36 NXSL_Value *value = NULL;
37 if (!_tcscmp(pszAttr, _T("eof")))
38 {
39 value = new NXSL_Value((LONG)feof((FILE *)pObject->getData()));
40 }
41 return value;
42 }
43
44 /**
45 * Check file access.
46 * Parameters:
47 * 1) file name
48 * 2) desired access
49 */
50 int F_access(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program)
51 {
52 if (!argv[0]->isString())
53 return NXSL_ERR_NOT_STRING;
54
55 if (!argv[1]->isInteger())
56 return NXSL_ERR_NOT_INTEGER;
57
58 *ppResult = new NXSL_Value((LONG)((_taccess(argv[0]->getValueAsCString(), argv[1]->getValueAsInt32()) == 0) ? 1 : 0));
59 return 0;
60 }
61
62 /**
63 * Open file. Returns FILE object or null.
64 * Parameters:
65 * 1) file name
66 * 2) mode (optional, default "r")
67 */
68 int F_fopen(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program)
69 {
70 if (!argv[0]->isString())
71 return NXSL_ERR_NOT_STRING;
72
73 if ((argc < 1) || (argc > 2))
74 return NXSL_ERR_INVALID_ARGUMENT_COUNT;
75
76 const TCHAR *mode;
77 if (argc == 2)
78 {
79 if (!argv[1]->isString())
80 return NXSL_ERR_NOT_STRING;
81 mode = argv[1]->getValueAsCString();
82 }
83 else
84 {
85 mode = _T("r");
86 }
87
88 FILE *file = _tfopen(argv[0]->getValueAsCString(), mode);
89 if (file != NULL)
90 {
91 *ppResult = new NXSL_Value(new NXSL_Object(&g_nxslFileClass, file));
92 }
93 else
94 {
95 *ppResult = new NXSL_Value;
96 }
97 return 0;
98 }
99
100 /**
101 * Close file.
102 * Parameters:
103 * 1) file object
104 */
105 int F_fclose(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program)
106 {
107 if (!argv[0]->isObject())
108 return NXSL_ERR_NOT_OBJECT;
109
110 NXSL_Object *object = argv[0]->getValueAsObject();
111 if (_tcscmp(object->getClass()->getName(), g_nxslFileClass.getName()))
112 return NXSL_ERR_BAD_CLASS;
113
114 FILE *file = (FILE *)object->getData();
115 fclose(file);
116 *ppResult = new NXSL_Value;
117 return 0;
118 }
119
120 /**
121 * Check if at EOF.
122 * Parameters:
123 * 1) file object
124 */
125 int F_feof(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program)
126 {
127 if (!argv[0]->isObject())
128 return NXSL_ERR_NOT_OBJECT;
129
130 NXSL_Object *object = argv[0]->getValueAsObject();
131 if (_tcscmp(object->getClass()->getName(), g_nxslFileClass.getName()))
132 return NXSL_ERR_BAD_CLASS;
133
134 FILE *file = (FILE *)object->getData();
135 *ppResult = new NXSL_Value((LONG)feof(file));
136 return 0;
137 }
138
139 /**
140 * Read line from file. New line character will be stripped off the line.
141 * Returns null if EOF reached or read error occured.
142 * Parameters:
143 * 1) file object
144 */
145 int F_fgets(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program)
146 {
147 if (!argv[0]->isObject())
148 return NXSL_ERR_NOT_OBJECT;
149
150 NXSL_Object *object = argv[0]->getValueAsObject();
151 if (_tcscmp(object->getClass()->getName(), g_nxslFileClass.getName()))
152 return NXSL_ERR_BAD_CLASS;
153
154 FILE *file = (FILE *)object->getData();
155
156 TCHAR buffer[8192] = _T("");
157 if (_fgetts(buffer, 8192, file) != NULL)
158 {
159 TCHAR *ptr = _tcschr(buffer, _T('\n'));
160 if (ptr != NULL)
161 *ptr = 0;
162 *ppResult = new NXSL_Value(buffer);
163 }
164 else
165 {
166 *ppResult = new NXSL_Value;
167 }
168 return 0;
169 }
170
171 /**
172 * Write line to file.
173 * Parameters:
174 * 1) file object
175 * 2) text to write
176 */
177 int F_fputs(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program)
178 {
179 if (!argv[0]->isObject())
180 return NXSL_ERR_NOT_OBJECT;
181
182 NXSL_Object *object = argv[0]->getValueAsObject();
183 if (_tcscmp(object->getClass()->getName(), g_nxslFileClass.getName()))
184 return NXSL_ERR_BAD_CLASS;
185
186 if (!argv[1]->isString())
187 return NXSL_ERR_NOT_STRING;
188
189 FILE *file = (FILE *)object->getData();
190 _fputts(argv[1]->getValueAsCString(), file);
191 _fputts(_T("\n"), file);
192 *ppResult = new NXSL_Value;
193 return 0;
194 }