1
0
mirror of https://frontier.innolan.net/rainlance/amiga-tz.git synced 2025-11-22 18:22:00 +00:00
Files
amiga-tz/amiga_locale.c
2015-06-17 17:00:14 +02:00

234 lines
5.7 KiB
C

/*
* Copyright (c) 2015 Carsten Larsen
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "amiga_tz.h"
/**********************************************************************/
char ** environ = NULL;
void syslog(int pri, const char *fmt, ...) { }
void logwtmp(const char *line, const char *name, const char *host) { }
void lose(int i) { }
/**********************************************************************/
struct LocaleBase* ctypeBase = NULL;
struct Locale* ctypeLocale= NULL;
void InitCTypeLocale()
{
ctypeBase = (struct LocaleBase*)OpenLibrary("locale.library", 38L);
ctypeLocale = OpenLocale(NULL);
}
void CleanupCTypeLocale()
{
if (ctypeBase != NULL) {
CloseLocale(ctypeLocale);
ctypeLocale = NULL;
CloseLibrary((struct Library*)ctypeBase);
ctypeBase = NULL;
}
}
/**********************************************************************/
int isalnum(int c)
{
int res;
InitCTypeLocale();
res = IsAlNum(ctypeLocale, (ULONG)c);
CleanupCTypeLocale();
return res;
}
int isalpha(int c)
{
int res;
InitCTypeLocale();
res = IsAlpha(ctypeLocale, (ULONG)c);
CleanupCTypeLocale();
return res;
}
int isdigit(int c)
{
int res;
InitCTypeLocale();
res = IsDigit(ctypeLocale, (ULONG)c);
CleanupCTypeLocale();
return res;
}
int ispunct(int c)
{
int res;
InitCTypeLocale();
res = IsPunct(ctypeLocale, (ULONG)c);
CleanupCTypeLocale();
return res;
}
int isspace(int c)
{
int res;
InitCTypeLocale();
res = IsSpace(ctypeLocale, (ULONG)c);
CleanupCTypeLocale();
return res;
}
int isupper(int c)
{
int res;
InitCTypeLocale();
res = IsUpper(ctypeLocale, (ULONG)c);
CleanupCTypeLocale();
return res;
}
int iscntrl(int c)
{
int res;
InitCTypeLocale();
res = IsCntrl(ctypeLocale, (ULONG)c);
CleanupCTypeLocale();
return res;
}
/**********************************************************************/
struct Device *TimerBase = NULL;
struct timerequest *request = NULL;
int delete_timer()
{
struct MsgPort *port;
if (request == NULL)
return 1;
port = request->tr_node.io_Message.mn_ReplyPort;
if (port != 0)
DeletePort(port);
CloseDevice((struct IORequest*)request);
DeleteExtIO((struct IORequest*)request);
return 0;
}
int create_timer()
{
LONG error;
struct MsgPort *port = CreatePort(0, 0);
if (port == NULL)
return 1;
request = (struct timerequest*)CreateExtIO(port, sizeof(struct timerequest));
if (request == NULL)
{
DeletePort(port);
return 1;
}
error = OpenDevice(TIMERNAME, UNIT_MICROHZ, (struct IORequest*)request, 0L);
if (error != 0)
{
delete_timer(request);
return 1;
}
TimerBase = (struct Device*)request->tr_node.io_Device;
return 0;
}
/**********************************************************************/
tz_time_t time(tz_time_t *x)
{
struct LocaleBase* localeBase;
struct Locale* locale;
struct timeval tv;
int error;
localeBase = (struct LocaleBase*)OpenLibrary("locale.library", 38L);
if (localeBase == NULL) {
return 0;
}
locale = OpenLocale(NULL);
if (locale == NULL) {
CloseLibrary((struct Library*)localeBase);
return 0;
}
error = create_timer();
if (error != 0) {
return 0;
}
GetSysTime(&tv);
if (x) {
*x = (time_t)tv.tv_secs - locale->loc_GMTOffset * 60;
}
CloseLocale(locale);
CloseLibrary((struct Library*)localeBase);
delete_timer();
return (time_t)tv.tv_secs;
}
/**********************************************************************/
int settimeofday(const struct timeval *tv, const struct timezone *tz)
{
struct LocaleBase* localeBase;
struct Locale* locale;
int error;
localeBase = (struct LocaleBase*)OpenLibrary("locale.library", 38L);
if (localeBase == NULL) {
return 1;
}
locale = OpenLocale(NULL);
if (locale == NULL) {
CloseLibrary((struct Library*)localeBase);
return 1;
}
error = create_timer();
if (error != 0) {
return error;
}
request->tr_node.io_Command = TR_SETSYSTIME;
request->tr_time.tv_secs = (long)tv->tv_secs - AMIGAOFFSET + locale->loc_GMTOffset * 60;
request->tr_time.tv_micro = tv->tv_micro;
DoIO((struct IORequest*)request);
error = delete_timer();
CloseLocale(locale);
CloseLibrary((struct Library*)localeBase);
return error;
}
/**********************************************************************/