-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.cpp
86 lines (71 loc) · 1.6 KB
/
import.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// import.cpp
// FireFly server import/export module
#include "import.h"
#include "database.h"
#include "string.h"
#include "array.h"
#include "object.h"
#include "any_impl.h"
#include "parse.h"
// Public interface
static char buf[4096+1];
RESULT import_t::Import( const char* szFilename )
{
// Read the import file into a buffer.
int fd = OPEN( szFilename, O_RDONLY | O_BINARY );
if (fd <= 0) {
printf("import: failed to open import file.\n");
return E_INTERNAL;
}
stringp source = new string_t();
for (;;) {
int n = READ( fd, buf, sizeof(buf)-1 );
if (n < 1) break;
buf[n] = '\0';
source->AppendCStr(buf);
}
CLOSE(fd);
// Parse the file and import everything.
any_t mResult;
RESULT nResult = Parse( source, true, mResult );
if (ISERROR(nResult)) {
printf( "import: error parsing import file: %d\n", (int)nResult );
} else {
// Display any errors that were returned.
if (mResult.IsString()) {
puts(stringp(mResult)->CString());
}
}
// Clean up.
source->Release();
printf("import: done.\n");
return E_SUCCESS;
}
RESULT import_t::Export( const char* szFilename )
{
m_pFile = fopen( szFilename, "wt" );
if (m_pFile) {
printf("export: exporting database...\n");
g_oDatabase.RootObject()->Export();
fclose(m_pFile);
m_pFile = NULL;
printf("export: done.\n");
}
else
{
printf("export: failed to open output file.\n");
}
return E_SUCCESS;
}
void import_t::WriteExport(stringp sOutput)
{
if (m_pFile)
{
fwrite( sOutput->CString(), 1, sOutput->Length(), m_pFile );
}
}
void import_t::WriteExportBreak()
{
fwrite( "\n", 1, 1, m_pFile );
printf("."); // show progress.
}