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

corec: remove more unused date/path functions #197

Merged
merged 4 commits into from
Jan 1, 2025
Merged
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
1 change: 0 additions & 1 deletion corec/corec/helpers/date/date.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ typedef struct datepack_t
} datepack_t;

DATE_DLL datetime_t GetTimeDate(void); // UTC in s (reference is 1st January 2001 00:00:00.000 UTC, use a helper to get the localized string value)
DATE_DLL datetime_t TimePackToRel(const datepack_t *tp, bool_t FromLocal);
DATE_DLL bool_t GetDatePacked(datetime_t t, datepack_t *tp, bool_t ToLocal);

#ifdef __cplusplus
Expand Down
43 changes: 0 additions & 43 deletions corec/corec/helpers/date/date_libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,54 +27,11 @@ datetime_t LinuxToDateTime(time_t t)
return (datetime_t)t;
}

static time_t DateTimeTZOffset(time_t t)
{
time_t offset = 0;
struct tm *tmp = NULL;
tmp = localtime(&t);
if (tmp) {
offset = mktime(tmp);
tmp = gmtime(&t);
if (tmp) {
offset -= mktime(tmp);
} else {
offset = 0;
}
}
return offset;
}

datetime_t GetTimeDate(void)
{
return LinuxToDateTime(time(NULL));
}

datetime_t TimePackToRel(const datepack_t *tp, bool_t Local)
{
struct tm date;
time_t ot = 0;

if (!tp)
return INVALID_DATETIME_T;

date.tm_sec = (int)tp->Second;
date.tm_min = (int)tp->Minute;
date.tm_hour = (int)tp->Hour;
date.tm_mday = (int)tp->Day;
date.tm_mon = (int)tp->Month - 1;
date.tm_year = (int)tp->Year - 1900;
date.tm_isdst = -1; // use auto

ot = mktime(&date);
if (ot == (time_t) -1)
return INVALID_DATETIME_T;

if (!Local)
ot += DateTimeTZOffset(ot);

return LinuxToDateTime(ot);
}

bool_t GetDatePacked(datetime_t t, datepack_t *tp, bool_t Local)
{
time_t ot;
Expand Down
81 changes: 4 additions & 77 deletions corec/corec/helpers/date/date_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ static FILETIME fTimeCache[MAX_CACHED_YEAR - MIN_CACHED_YEAR + 2][2];
#define TIME_ZONE_ID_INVALID ((DWORD)0xFFFFFFFF)
#endif

#ifndef TARGET_WIN2K
static bool_t GetIsDst(datetime_t t); // may not be correct on all platforms
#endif

systick_t GetTimeTick(void)
{
Expand Down Expand Up @@ -135,58 +137,6 @@ datetime_t GetTimeDate()
return INVALID_DATETIME_T;
}

datetime_t TimePackToRel(const datepack_t *tp, bool_t Local)
{
SYSTEMTIME TimeStruct = {0};
FILETIME fTime;
datetime_t t;

if (!tp)
return INVALID_DATETIME_T;

TimeStruct.wMilliseconds = (WORD)0;
TimeStruct.wSecond = (WORD)tp->Second;
TimeStruct.wMinute = (WORD)tp->Minute;
TimeStruct.wHour = (WORD)tp->Hour;
TimeStruct.wDay = (WORD)tp->Day;
TimeStruct.wMonth = (WORD)tp->Month;
TimeStruct.wYear = (WORD)tp->Year;
TimeStruct.wDayOfWeek = (WORD)(tp->WeekDay ? tp->WeekDay-1:0);

#ifdef TARGET_WIN2K
if (Local)
{
SYSTEMTIME sysTimeStruct;
TIME_ZONE_INFORMATION timeZoneInfo;
GetTimeZoneInformation(&timeZoneInfo);
if (!TzSpecificLocalTimeToSystemTime(&timeZoneInfo, &TimeStruct, &sysTimeStruct))
return INVALID_DATETIME_T;
if (!SystemTimeToFileTime( &sysTimeStruct, &fTime ))
return INVALID_DATETIME_T;
}
else
#endif
if (!SystemTimeToFileTime( &TimeStruct, &fTime ))
return INVALID_DATETIME_T;

t = FileTimeToRel(&fTime);

#ifndef TARGET_WIN2K
if (Local) {
GetFixedTZ();
t += fix_tz.Bias * 60;
if (GetIsDst(t)) // test with UTC time without daylight (not perfect at the edges)
t += fix_tz.DaylightBias * 60;
else
t += fix_tz.StandardBias * 60;
if (t==INVALID_DATETIME_T)
++t;
}
#endif

return t;
}

bool_t GetDatePacked(datetime_t t, datepack_t *tp, bool_t Local)
{
SYSTEMTIME TimeStruct = {0};
Expand Down Expand Up @@ -233,32 +183,9 @@ bool_t GetDatePacked(datetime_t t, datepack_t *tp, bool_t Local)
return 1;
}

#ifndef TARGET_WIN2K
bool_t GetIsDst(datetime_t t)
{
#ifdef TARGET_WIN2K
FILETIME fTime = {0};
TIME_ZONE_INFORMATION timeZoneInfo;
SYSTEMTIME sysTimeStruct = {0};
SYSTEMTIME TimeStruct = {0};
int Hour;

if (t == INVALID_DATETIME_T)
return 0;

RelToFileTime(t, &fTime);
if (!FileTimeToSystemTime(&fTime, &sysTimeStruct))
return 0;
GetTimeZoneInformation(&timeZoneInfo);
if (!SystemTimeToTzSpecificLocalTime(&timeZoneInfo, &sysTimeStruct, &TimeStruct))
return 0;
Hour = TimeStruct.wHour;
timeZoneInfo.DaylightBias = 0;
timeZoneInfo.DaylightDate.wMonth = 0;
timeZoneInfo.StandardDate.wMonth = 0;
if (!SystemTimeToTzSpecificLocalTime(&timeZoneInfo, &sysTimeStruct, &TimeStruct))
return 0;
return Hour != TimeStruct.wHour;
#else
FILETIME fTime;
FILETIME fTime1, fTime2;
FILETIME *fTimeStart, *fTimeEnd;
Expand Down Expand Up @@ -315,7 +242,7 @@ bool_t GetIsDst(datetime_t t)
return 0;

return 1;
#endif
}
#endif

#endif
6 changes: 2 additions & 4 deletions corec/corec/helpers/file/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,14 @@ FILE_DLL void RemovePathDelimiter(tchar_t* Path);
FILE_DLL void AddPathDelimiter(tchar_t* Path,size_t PathLen);
FILE_DLL const tchar_t* GetProtocol(const tchar_t* URL, tchar_t *_Protocol, int ProtoLen, bool_t* HasHost);
FILE_DLL void SplitPath(const tchar_t* Path, tchar_t* Dir, int DirLen, tchar_t* Name, int NameLen, tchar_t* Ext, int ExtLen);
FILE_DLL void SplitURL(const tchar_t* URL, tchar_t* Mime, int MimeLen, tchar_t* Host, int HostLen, int* Port, tchar_t* Path, int PathLen);
FILE_DLL bool_t SplitAddr(const tchar_t* URL, tchar_t* Peer, int PeerLen, tchar_t* Local, int LocalLen);
FILE_DLL bool_t SetFileExt(tchar_t* URL, size_t URLLen, const tchar_t* Ext);
FILE_DLL int CheckExts(const tchar_t* URL, const tchar_t* Exts);
FILE_DLL void AbsPath(tchar_t* Abs, int AbsLen, const tchar_t* Path, const tchar_t* Base);
FILE_DLL void AbsPathNormalize(tchar_t* Abs, size_t AbsLen);
FILE_DLL void ReduceLocalPath(tchar_t* Abs, size_t AbsLen);
FILE_DLL void RelPath(tchar_t* Rel, int RelLen, const tchar_t* Path, const tchar_t* Base);
FILE_DLL bool_t UpperPath(tchar_t* Path, tchar_t* Last, size_t LastLen);

int CheckExts(const tchar_t* URL, const tchar_t* Exts);

#ifdef __cplusplus
}
#endif
Expand Down
121 changes: 0 additions & 121 deletions corec/corec/helpers/file/tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,6 @@
#include "file.h"
#include <corec/str/str.h>

bool_t SetFileExt(tchar_t* URL, size_t URLLen, const tchar_t* Ext)
{
tchar_t *p,*q,*p2;
bool_t HasHost;

p = (tchar_t*) GetProtocol(URL,NULL,0,&HasHost);
q = p;

p = tcsrchr(q,'\\');
p2 = tcsrchr(q,'/');
if (!p || (p2 && p2>p))
p=p2;
if (p)
q = p+1;
else
if (HasHost) // only hostname
return 0;

if (!q[0]) // no filename at all?
return 0;

p = tcsrchr(q,'.');
if (p)
*p = 0;

tcscat_s(URL,URLLen,T("."));
tcscat_s(URL,URLLen,Ext);
return 1;
}

void AddPathDelimiter(tchar_t* Path,size_t PathLen)
{
size_t n = tcslen(Path);
Expand Down Expand Up @@ -104,97 +74,6 @@ const tchar_t* GetProtocol(const tchar_t* URL, tchar_t* Proto, int ProtoLen, boo
return s;
}

bool_t SplitAddr(const tchar_t* URL, tchar_t* Peer, int PeerLen, tchar_t* Local, int LocalLen)
{
const tchar_t* p = NULL;
const tchar_t* p2;
const tchar_t* Addr;
bool_t HasHost;
bool_t Result = 0;

Addr = GetProtocol(URL,NULL,0,&HasHost);

if (HasHost)
{
p = tcschr(Addr,'\\');
p2 = tcschr(Addr,'/');
if (!p || (p2 && p2>p))
p=p2;
}
if (!p)
p = Addr+tcslen(Addr);

p2 = tcschr(Addr,'@');
if (!p2 || p2>p)
p2 = p;
else
Result = 1;

if (Peer)
tcsncpy_s(Peer,PeerLen,URL,p2-URL);

if (Local)
{
if (p2<p)
++p2;
tcsncpy_s(Local,LocalLen,URL,Addr-URL);
tcsncat_s(Local,LocalLen,p2,p-p2);
}
return Result;
}

void SplitURL(const tchar_t* URL, tchar_t* Protocol, int ProtocolLen, tchar_t* Host, int HostLen, int* Port, tchar_t* Path, int PathLen)
{
bool_t HasHost;
URL = GetProtocol(URL,Protocol,ProtocolLen,&HasHost);

if (HasHost)
{
const tchar_t* p;
const tchar_t* p2;

p = tcschr(URL,'\\');
p2 = tcschr(URL,'/');
if (!p || (p2 && p2>p))
p=p2;
if (!p)
p = URL+tcslen(URL);

p2 = tcschr(URL,':');
if (p2 && p2<p)
{
if (Port)
stscanf(p2+1,T("%d"),Port);
}
else
p2 = p;

if (Host)
tcsncpy_s(Host,HostLen,URL,p2-URL);

URL = p;
}
else
{
if (Host && HostLen>0)
*Host = 0;
}

if (Path)
{
if (URL[0])
{
tchar_t* p;
tcscpy_s(Path,PathLen,URL);
for (p=Path;*p;++p)
if (*p == '\\')
*p = '/';
}
else
tcscpy_s(Path,PathLen,T("/"));
}
}

void SplitPath(const tchar_t* URL, tchar_t* Dir, int DirLen, tchar_t* Name, int NameLen, tchar_t* Ext, int ExtLen)
{
const tchar_t *p,*p2,*p3;
Expand Down
Loading