Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to accomodate build on Windows #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(SGP4)

set (CMAKE_CXX_STANDARD 11)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_EXTENSIONS OFF)


if(DEFINED MSVC_VERSION)
if (MSVC_VERSION GREATER_EQUAL "1900")
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("/std:c++latest" _cpp_latest_flag_supported)
if (_cpp_latest_flag_supported)
add_compile_options("/std:c++latest")
endif()
endif()
endif()


set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wextra")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wnon-virtual-dtor")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-long-long")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wcast-align")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsign-conversion")

include_directories(libsgp4)

Expand All @@ -19,4 +27,4 @@ add_subdirectory(sattrack)
add_subdirectory(runtest)
add_subdirectory(passpredict)

file(COPY SGP4-VER.TLE DESTINATION ${PROJECT_BINARY_DIR})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/SGP4-VER.TLE DESTINATION ${PROJECT_BINARY_DIR})
7 changes: 3 additions & 4 deletions libsgp4/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ set(SRCS
TleException.h
Tle.h
Util.h
Vector.h
)
Vector.h)

add_library(sgp4 STATIC ${SRCS} ${INCS})
add_library(sgp4s SHARED ${SRCS} ${INCS})
install( TARGETS sgp4s LIBRARY DESTINATION lib )
install( FILES ${INCS} DESTINATION include/SGP4 )
install(TARGETS sgp4s DESTINATION lib)
install( FILES ${INCS} DESTINATION include/SGP4)
33 changes: 13 additions & 20 deletions libsgp4/DateTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <algorithm>
#include "TimeSpan.h"
#include "Util.h"

Expand Down Expand Up @@ -141,27 +142,19 @@ class DateTime
static DateTime Now(bool microseconds = false)
{
DateTime dt;
struct timespec ts;

if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
{
if (microseconds)
{
dt = DateTime(UnixEpoch
+ ts.tv_sec * TicksPerSecond
+ ts.tv_nsec / 1000LL * TicksPerMicrosecond);
}
else
{
dt = DateTime(UnixEpoch
+ ts.tv_sec * TicksPerSecond);
}
}
Util::UnixTimestamp ts = Util::CurrentTime();

if (microseconds)
{
dt = DateTime(UnixEpoch
+ ts.seconds * TicksPerSecond
+ ts.nanoseconds / 1000LL * TicksPerMicrosecond);
}
else
{
throw 1;
}

{
dt = DateTime(UnixEpoch
+ ts.nanoseconds * TicksPerSecond);
}
return dt;
}

Expand Down
93 changes: 60 additions & 33 deletions libsgp4/Util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,68 @@
* limitations under the License.
*/

#include <Util.h>

#include "Util.h"

#include <algorithm>
#include <locale>
#include <functional>
#ifdef _WIN32

namespace Util
{
namespace
{
struct IsDigit: std::unary_function<char, bool>
{
bool operator()(char c) const
{
return std::isdigit(c, std::locale::classic()) == 0;
}
};
}

void TrimLeft(std::string& s)
{
s.erase(s.begin(),
std::find_if(s.begin(), s.end(), std::not1(IsDigit())));
}

void TrimRight(std::string& s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(IsDigit())).base(),
s.end());
}

void Trim(std::string& s)
{
TrimLeft(s);
TrimRight(s);
}

std::atomic<WindowsUtcTimeSource*> WindowsUtcTimeSource::m_instance = std::atomic<WindowsUtcTimeSource*>(nullptr);
std::mutex WindowsUtcTimeSource::m_mutex;

WindowsUtcTimeSource* WindowsUtcTimeSource::Instance()
{
WindowsUtcTimeSource* tmp = m_instance.load(std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_acquire);
if (tmp == nullptr)
{
std::lock_guard<std::mutex> lock(m_mutex);
if (tmp == nullptr)
{
tmp = new WindowsUtcTimeSource();
std::atomic_thread_fence(std::memory_order_release);
m_instance.store(tmp, std::memory_order_relaxed);
}

}
return tmp;
}

WindowsUtcTimeSource::WindowsUtcTimeSource()
{
// we check to see if GetSystemTimePreciseAsFileTime exists. If it does, use that. Otherwise, use the non-precise version
HINSTANCE hDLL = LoadLibrary("Kernel32.dll");
m_getUtcTimeFunc = (FuncT)GetProcAddress((HMODULE)hDLL, "GetSystemTimePreciseAsFileTime");

if (m_getUtcTimeFunc != nullptr)
{
m_isPrecise = true;
}
else
{
m_isPrecise = false;
m_getUtcTimeFunc = (FuncT)GetSystemTimeAsFileTime;
}
};

UnixTimestamp WindowsUtcTimeSource::GetUtcTime()
{
UnixTimestamp stamp;
int ticks_per_second = 10000000; // there are 1e7 100ns ticks in a second
int nanoseconds_per_tick = 100;
ULONGLONG seconds_from_windows_to_unix_epoch = 11644473600; // seconds from 1601-01-01T00:00:00Z to 1970-01-01T00:00:00Z
FILETIME ft;
m_getUtcTimeFunc(&ft);
ULARGE_INTEGER ui;
ui.LowPart = ft.dwLowDateTime;
ui.HighPart = ft.dwHighDateTime;
int64_t diff = ui.QuadPart - seconds_from_windows_to_unix_epoch * ticks_per_second;
stamp.seconds = diff / ticks_per_second;
stamp.nanoseconds = (diff - stamp.seconds * ticks_per_second) * nanoseconds_per_tick;
return stamp;
}

}

#endif
Loading