2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS xml to sdb converter
4 * FILE: sdk/tools/xml2sdb/main.cpp
5 * PURPOSE: Implement platform agnostic read / write / allocation functions, parse commandline
6 * PROGRAMMERS: Mark Jansen
12 #include "sdbstringtable.h"
19 ULONG g_ShimDebugLevel
= SHIM_WARN
;
21 LPVOID WINAPI
SdbpAlloc(SIZE_T size
)
23 return ::calloc(1, size
);
26 LPVOID WINAPI
SdbpReAlloc(LPVOID mem
, SIZE_T size
, SIZE_T oldSize
)
28 LPVOID newMem
= ::realloc(mem
, size
);
29 if (newMem
&& size
> oldSize
)
31 memset((BYTE
*)newMem
+ oldSize
, 0, size
- oldSize
);
36 void WINAPI
SdbpFree(LPVOID mem
)
41 DWORD
SdbpStrlen(PCWSTR string
)
49 DWORD WINAPI
SdbpStrsize(PCWSTR string
)
51 return (SdbpStrlen(string
) + 1) * sizeof(WCHAR
);
54 PDB WINAPI
SdbpCreate(LPCWSTR path
, PATH_TYPE type
, BOOL write
)
58 std::string
pathA(path
, path
+ SdbpStrlen(path
));
60 f
= fopen(pathA
.c_str(), write
? "wb" : "rb");
64 db
= (PDB
)SdbAlloc(sizeof(DB
));
70 void WINAPI
SdbpFlush(PDB db
)
72 fwrite(db
->data
, db
->write_iter
, 1, (FILE*)db
->file
);
75 void WINAPI
SdbCloseDatabase(PDB db
)
81 fclose((FILE*)db
->file
);
82 if (db
->string_buffer
)
83 SdbCloseDatabase(db
->string_buffer
);
84 if (db
->string_lookup
)
85 SdbpTableDestroy(&db
->string_lookup
);
90 BOOL WINAPI
SdbpCheckTagType(TAG tag
, WORD type
)
92 if ((tag
& TAG_TYPE_MASK
) != type
)
97 BOOL WINAPI
SdbpReadData(PDB db
, PVOID dest
, DWORD offset
, DWORD num
)
99 DWORD size
= offset
+ num
;
101 /* Either overflow or no data to read */
109 memcpy(dest
, db
->data
+ offset
, num
);
113 TAG WINAPI
SdbGetTagFromTagID(PDB db
, TAGID tagid
)
116 if (!SdbpReadData(db
, &data
, tagid
, sizeof(data
)))
121 BOOL WINAPI
SdbpCheckTagIDType(PDB db
, TAGID tagid
, WORD type
)
123 TAG tag
= SdbGetTagFromTagID(db
, tagid
);
126 return SdbpCheckTagType(tag
, type
);
129 BOOL WINAPIV
ShimDbgPrint(SHIM_LOG_LEVEL Level
, PCSTR FunctionName
, PCSTR Format
, ...)
132 const char* LevelStr
;
134 if (Level
> g_ShimDebugLevel
)
152 printf("[%s][%-20s] ", LevelStr
, FunctionName
);
153 va_start(ArgList
, Format
);
154 vprintf(Format
, ArgList
);
160 #define TICKSPERSEC 10000000
161 #if defined(__GNUC__)
162 #define TICKSTO1970 0x019db1ded53e8000LL
164 #define TICKSTO1970 0x019db1ded53e8000i64
166 VOID NTAPI
RtlSecondsSince1970ToTime(IN ULONG SecondsSince1970
,
167 OUT PLARGE_INTEGER Time
)
169 Time
->QuadPart
= ((LONGLONG
)SecondsSince1970
* TICKSPERSEC
) + TICKSTO1970
;
176 bool xml_2_db(const char* xml
, const WCHAR
* sdb
);
178 static bool run_one(std::string
& input
, std::string
& output
)
180 sdbstring
outputW(output
.begin(), output
.end());
181 if (!xml_2_db(input
.c_str(), outputW
.c_str()))
187 static std::string
get_strarg(int argc
, char* argv
[], int& i
)
190 return std::string(argv
[i
] + 2);
193 if (i
>= argc
|| !argv
[i
])
194 return std::string();
198 static void update_loglevel(int argc
, char* argv
[], int& i
)
200 std::string value
= get_strarg(argc
, argv
, i
);
201 g_ShimDebugLevel
= strtoul(value
.c_str(), NULL
, 10);
204 // -i R:\src\apphelp\reactos\media\sdb\sysmain.xml -oR:\build\apphelp\devenv_msvc\media\sdb\ros2.sdb
205 int main(int argc
, char * argv
[])
207 std::string input
, output
;
210 for (int i
= 1; i
< argc
; ++i
)
212 if (argv
[i
][0] != '/' && argv
[i
][0] != '-')
218 input
= get_strarg(argc
, argv
, i
);
221 output
= get_strarg(argc
, argv
, i
);
224 update_loglevel(argc
, argv
, i
);
227 if (input
.empty() || output
.empty())
230 if (!run_one(input
, output
))
232 printf("Failed converting '%s' to '%s'\n", input
.c_str(), output
.c_str());