Skip to content

Commit 057067a

Browse files
authored
Merge pull request #151 from SamuelMarks/msvc
Microsoft Visual Studio (Windows) support
2 parents 9738a81 + ec52b6c commit 057067a

File tree

10 files changed

+227
-44
lines changed

10 files changed

+227
-44
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ Makefile
77
# Binary
88
libunqlite.a
99

10+
# IDEs
11+
.idea
12+
.vscode
13+
14+
# Build
15+
*build*

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_LIST_DIR}/src)
1919
SET(HEADERS_UNQLITE
2020
unqlite.h
2121
)
22+
source_group("Header Files" FILES "${HEADERS_UNQLITE}")
2223

2324
SET(SOURCES_UNQLITE
2425
unqlite.c
2526
)
27+
source_group("Source Files" FILES "${SOURCES_UNQLITE}")
2628

2729
SET(UNQLITE_STATIC_LIB unqlite)
2830
ADD_LIBRARY(${UNQLITE_STATIC_LIB} ${HEADERS_UNQLITE} ${SOURCES_UNQLITE})
2931

3032
INSTALL(TARGETS ${UNQLITE_STATIC_LIB} COMPONENT devel ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}")
3133
INSTALL(FILES ${HEADERS_UNQLITE} COMPONENT devel DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
34+
35+
include(CTest)
36+
if (BUILD_TESTING)
37+
add_subdirectory("example")
38+
endif (BUILD_TESTING)

example/2.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,20 @@ static void PI_Constant(
120120
static void TIME_Constant(unqlite_value *pValue, void *pUserData /* Unused */)
121121
{
122122
struct tm *pLocal;
123+
#if defined(_MSC_VER) || defined(__MINGW32__)
124+
/* errno_t err; */
125+
__time64_t long_time;
126+
struct tm newtime;
127+
_time64( &long_time );
128+
/* err = */ _localtime64_s(&newtime, &long_time);
129+
/* if (err != 0) return UNQLITE_INVALID; */
130+
pLocal = &newtime;
131+
#else
123132
time_t tt;
124133
/* Get the current local time */
125134
time(&tt);
126135
pLocal = localtime(&tt);
136+
#endif
127137
/* Expand the current time now */
128138
unqlite_value_string_format(pValue, "%02d:%02d:%02d",
129139
pLocal->tm_hour,

example/5.c

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -153,23 +153,34 @@ int date_func(
153153
int argc, /* Total number of arguments passed to the function */
154154
unqlite_value **argv /* Array of function arguments*/
155155
){
156-
time_t tt;
157-
struct tm *pNow;
158-
/* Get the current time */
159-
time(&tt);
156+
struct tm *pNow;
157+
#if defined(_MSC_VER) || defined(__MINGW32__)
158+
errno_t err;
159+
__time64_t long_time;
160+
struct tm newtime;
161+
_time64( &long_time );
162+
err = _localtime64_s(&newtime, &long_time);
163+
if (err != 0) return UNQLITE_INVALID;
164+
pNow = &newtime;
165+
#else
166+
time_t tt;
167+
/* Get the current time */
168+
time(&tt);
160169
pNow = localtime(&tt);
161-
/*
162-
* Return the current date.
163-
*/
164-
unqlite_result_string_format(pCtx,
165-
"%04d-%02d-%02d %02d:%02d:%02d", /* printf() style format */
166-
pNow->tm_year + 1900, /* Year */
167-
pNow->tm_mday, /* Day of the month */
168-
pNow->tm_mon + 1, /* Month number */
169-
pNow->tm_hour, /* Hour */
170-
pNow->tm_min, /* Minutes */
171-
pNow->tm_sec /* Seconds */
172-
);
170+
#endif
171+
/*
172+
* Return the current date.
173+
*/
174+
unqlite_result_string_format(pCtx,
175+
"%04d-%02d-%02d %02d:%02d:%02d", /* printf() style format */
176+
pNow->tm_year + 1900, /* Year */
177+
pNow->tm_mday, /* Day of the month */
178+
pNow->tm_mon + 1, /* Month number */
179+
pNow->tm_hour, /* Hour */
180+
pNow->tm_min, /* Minutes */
181+
pNow->tm_sec /* Seconds */
182+
);
183+
173184
/* All done */
174185
return UNQLITE_OK;
175186
}
@@ -246,11 +257,21 @@ int array_time_func(unqlite_context *pCtx, int argc, unqlite_value **argv)
246257
{
247258
unqlite_value *pArray; /* Our JSON Array */
248259
unqlite_value *pValue; /* Array entries value */
249-
time_t tt;
250260
struct tm *pNow;
251261
/* Get the current time first */
252-
time(&tt);
262+
#if defined(_MSC_VER) || defined(__MINGW32__)
263+
errno_t err;
264+
__time64_t long_time;
265+
struct tm newtime;
266+
_time64( &long_time );
267+
err = _localtime64_s(&newtime, &long_time);
268+
if (err != 0) return UNQLITE_INVALID;
269+
pNow = &newtime;
270+
#else
271+
time_t tt;
272+
time(&tt);
253273
pNow = localtime(&tt);
274+
#endif
254275
/* Create a new array */
255276
pArray = unqlite_context_new_array(pCtx);
256277
/* Create a worker scalar value */
@@ -325,11 +346,21 @@ int object_date_func(unqlite_context *pCtx, int argc /* unused */, unqlite_value
325346
{
326347
unqlite_value *pObject; /* Our JSON object */
327348
unqlite_value *pValue; /* Objecr entries value */
328-
time_t tt;
329349
struct tm *pNow;
330350
/* Get the current time first */
331-
time(&tt);
332-
pNow = localtime(&tt);
351+
#if defined(_MSC_VER) || defined(__MINGW32__)
352+
errno_t err;
353+
__time64_t long_time;
354+
struct tm newtime;
355+
_time64( &long_time );
356+
err = _localtime64_s(&newtime, &long_time);
357+
if (err != 0) return UNQLITE_INVALID;
358+
pNow = &newtime;
359+
#else
360+
time_t tt;
361+
time(&tt);
362+
pNow = localtime(&tt);
363+
#endif
333364
/* Create a new JSON object */
334365
pObject = unqlite_context_new_array(pCtx);
335366
/* Create a worker scalar value */

example/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
foreach (name "1" "2" "3" "4" "5" "6" "unqlite_huge" "unqlite_mp3" "unqlite_tar")
2+
set(EXEC_NAME "${PROJECT_NAME}_test_example_${name}")
3+
set(Source_Files "${name}.c")
4+
source_group("${EXEC_NAME} Source Files" FILES "${Source_Files}")
5+
add_executable("${EXEC_NAME}" "${Source_Files}")
6+
target_link_libraries("${EXEC_NAME}" PUBLIC ${UNQLITE_STATIC_LIB})
7+
add_test(NAME "${EXEC_NAME}" COMMAND "${EXEC_NAME}")
8+
endforeach ()
9+
10+
add_subdirectory(demovfs)

example/demovfs/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
set(EXEC_NAME "${PROJECT_NAME}_test_example_unqlite_demovfs")
2+
set(Source_Files "unqlite_demovfs.c")
3+
source_group("${EXEC_NAME} Source Files" FILES "${Source_Files}")
4+
add_executable("${EXEC_NAME}" "${Source_Files}")
5+
target_link_libraries("${EXEC_NAME}" PUBLIC ${UNQLITE_STATIC_LIB})
6+
add_test(NAME "${EXEC_NAME}" COMMAND "${EXEC_NAME}")

0 commit comments

Comments
 (0)