more helper functions added to nxinstall
[public/netxms.git] / src / nxinstall / io.cpp
CommitLineData
3c050e4c
VK
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 */
29NXSL_FileClass::NXSL_FileClass() : NXSL_Class()
30{
31 _tcscpy(m_szName, _T("FILE"));
32}
33
34NXSL_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
3c050e4c
VK
44/**
45 * Open file. Returns FILE object or null.
46 * Parameters:
47 * 1) file name
48 * 2) mode (optional, default "r")
49 */
50int F_fopen(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 ((argc < 1) || (argc > 2))
56 return NXSL_ERR_INVALID_ARGUMENT_COUNT;
57
58 const TCHAR *mode;
59 if (argc == 2)
60 {
61 if (!argv[1]->isString())
62 return NXSL_ERR_NOT_STRING;
63 mode = argv[1]->getValueAsCString();
64 }
65 else
66 {
67 mode = _T("r");
68 }
69
70 FILE *file = _tfopen(argv[0]->getValueAsCString(), mode);
71 if (file != NULL)
72 {
73 *ppResult = new NXSL_Value(new NXSL_Object(&g_nxslFileClass, file));
74 }
75 else
76 {
77 *ppResult = new NXSL_Value;
78 }
79 return 0;
80}
81
82/**
83 * Close file.
84 * Parameters:
85 * 1) file object
86 */
87int F_fclose(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program)
88{
89 if (!argv[0]->isObject())
90 return NXSL_ERR_NOT_OBJECT;
91
92 NXSL_Object *object = argv[0]->getValueAsObject();
93 if (_tcscmp(object->getClass()->getName(), g_nxslFileClass.getName()))
94 return NXSL_ERR_BAD_CLASS;
95
96 FILE *file = (FILE *)object->getData();
97 fclose(file);
98 *ppResult = new NXSL_Value;
99 return 0;
100}
101
102/**
103 * Check if at EOF.
104 * Parameters:
105 * 1) file object
106 */
107int F_feof(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program)
108{
109 if (!argv[0]->isObject())
110 return NXSL_ERR_NOT_OBJECT;
111
112 NXSL_Object *object = argv[0]->getValueAsObject();
113 if (_tcscmp(object->getClass()->getName(), g_nxslFileClass.getName()))
114 return NXSL_ERR_BAD_CLASS;
115
116 FILE *file = (FILE *)object->getData();
117 *ppResult = new NXSL_Value((LONG)feof(file));
118 return 0;
119}
120
121/**
122 * Read line from file. New line character will be stripped off the line.
123 * Returns null if EOF reached or read error occured.
124 * Parameters:
125 * 1) file object
126 */
127int F_fgets(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program)
128{
129 if (!argv[0]->isObject())
130 return NXSL_ERR_NOT_OBJECT;
131
132 NXSL_Object *object = argv[0]->getValueAsObject();
133 if (_tcscmp(object->getClass()->getName(), g_nxslFileClass.getName()))
134 return NXSL_ERR_BAD_CLASS;
135
136 FILE *file = (FILE *)object->getData();
137
138 TCHAR buffer[8192] = _T("");
139 if (_fgetts(buffer, 8192, file) != NULL)
140 {
141 TCHAR *ptr = _tcschr(buffer, _T('\n'));
142 if (ptr != NULL)
143 *ptr = 0;
144 *ppResult = new NXSL_Value(buffer);
145 }
146 else
147 {
148 *ppResult = new NXSL_Value;
149 }
150 return 0;
151}
152
153/**
154 * Write line to file.
155 * Parameters:
156 * 1) file object
157 * 2) text to write
158 */
159int F_fputs(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program)
160{
161 if (!argv[0]->isObject())
162 return NXSL_ERR_NOT_OBJECT;
163
164 NXSL_Object *object = argv[0]->getValueAsObject();
165 if (_tcscmp(object->getClass()->getName(), g_nxslFileClass.getName()))
166 return NXSL_ERR_BAD_CLASS;
167
168 if (!argv[1]->isString())
169 return NXSL_ERR_NOT_STRING;
170
171 FILE *file = (FILE *)object->getData();
172 _fputts(argv[1]->getValueAsCString(), file);
173 _fputts(_T("\n"), file);
174 *ppResult = new NXSL_Value;
175 return 0;
176}