project_files/hwc/rtl/sysutils.c
author koda
Tue, 13 Nov 2012 23:39:49 +0100
branchwebgl
changeset 8047 25a4daa6473c
parent 7983 02f36c3e7f6c
child 10015 4feced261c68
permissions -rw-r--r--
cleanup headers, remove -I . from rtl lib to save a few warnings
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8047
25a4daa6473c cleanup headers, remove -I . from rtl lib to save a few warnings
koda
parents: 7983
diff changeset
     1
#include "SysUtils.h"
25a4daa6473c cleanup headers, remove -I . from rtl lib to save a few warnings
koda
parents: 7983
diff changeset
     2
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     3
#include <time.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     4
#include <stdio.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     5
#include <stdlib.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     6
#include <string.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     7
8047
25a4daa6473c cleanup headers, remove -I . from rtl lib to save a few warnings
koda
parents: 7983
diff changeset
     8
#include "system.h"
25a4daa6473c cleanup headers, remove -I . from rtl lib to save a few warnings
koda
parents: 7983
diff changeset
     9
#include "misc.h"
25a4daa6473c cleanup headers, remove -I . from rtl lib to save a few warnings
koda
parents: 7983
diff changeset
    10
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    11
TDateTime fpcrtl_date()
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    12
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    13
    const int num_days_between_1900_1980 = 29220;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    14
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    15
    struct tm ref_date;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    16
    struct tm cur_date;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    17
    time_t local_time;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    18
    time_t ref_time, cur_time;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    19
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    20
    double timeDiff;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    21
    double day_time_frac; //fraction that represents the time in one day
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    22
    int num_seconds;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    23
    int numDays;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    24
    
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    25
    // unix epoch doesn't work, choose Jan 1st 1980 instead
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    26
    ref_date.tm_year = 80;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    27
    ref_date.tm_mon = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    28
    ref_date.tm_mday = 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    29
    ref_date.tm_hour = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    30
    ref_date.tm_min = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    31
    ref_date.tm_sec = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    32
    ref_date.tm_isdst = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    33
    ref_date.tm_wday = 0; // ignored
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    34
    ref_date.tm_yday = 0; // ignored
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    35
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    36
    local_time = time(NULL);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    37
    cur_date = *localtime(&local_time);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    38
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    39
    cur_date.tm_hour = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    40
    cur_date.tm_min = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    41
    cur_date.tm_sec = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    42
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    43
    ref_time = mktime(&ref_date);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    44
    cur_time = mktime(&cur_date);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    45
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    46
    timeDiff = difftime(cur_time, ref_time);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    47
    numDays = fpcrtl_round(timeDiff / 3600 / 24) + num_days_between_1900_1980 + 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    48
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    49
    fpcrtl_printf("[date] tim diff: %f\n", timeDiff);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    50
    fpcrtl_printf("[date] num days between 1980 and today:  %d\n", fpcrtl_round(timeDiff/3600/24));
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    51
    fpcrtl_printf("[date] current date: %s\n", asctime(&cur_date));
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    52
    fpcrtl_printf("[date] reference date: %s\n", asctime(&ref_date));
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    53
    fpcrtl_printf("[date] num days: %d\n", numDays);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    54
    
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    55
    return numDays;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    56
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    57
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    58
TDateTime fpcrtl_time()
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    59
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    60
    struct tm cur_date;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    61
    time_t local_time;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    62
    time_t cur_time;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    63
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    64
    double day_time_frac; //fraction that represents the time in one day
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    65
    int num_seconds;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    66
    
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    67
    local_time = time(NULL);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    68
    cur_date = *localtime(&local_time);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    69
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    70
    num_seconds = cur_date.tm_hour * 3600 + cur_date.tm_min * 60 + cur_date.tm_sec;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    71
    day_time_frac = num_seconds / 3600.0 / 24.0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    72
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    73
    fpcrtl_printf("%f\n", day_time_frac);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    74
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    75
    return day_time_frac;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    76
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    77
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    78
TDateTime fpcrtl_now()
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    79
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    80
    return fpcrtl_date() + fpcrtl_time();
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    81
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    82
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    83
/*
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    84
 * XXX: dummy
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    85
 */
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    86
string255 fpcrtl_formatDateTime(string255 FormatStr, TDateTime DateTime)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    87
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    88
    string255 result = STRINIT("2012 01-01");
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    89
    return result;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    90
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    91
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    92
string255 fpcrtl_trim(string255 s)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    93
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    94
    int left, right;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    95
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    96
    if(s.len == 0){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    97
        return s;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    98
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    99
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   100
    for(left = 0; left < s.len; left++)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   101
    {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   102
        if(s.str[left] != ' '){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   103
            break;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   104
        }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   105
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   106
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   107
    for(right = s.len - 1; right >= 0; right--)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   108
    {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   109
        if(s.str[right] != ' '){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   110
            break;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   111
        }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   112
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   113
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   114
    if(left > right){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   115
        s.len = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   116
        s.str[0] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   117
        return s;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   118
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   119
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   120
    s.len = right - left + 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   121
    memmove(s.str, s.str + left, s.len);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   122
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   123
    s.str[s.len] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   124
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   125
    return s;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   126
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   127
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   128
Integer fpcrtl_strToInt(string255 s)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   129
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   130
    s.str[s.len] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   131
    return atoi(s.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   132
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   133
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   134
//function ExtractFileName(const FileName: string): string;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   135
//var
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   136
//  i : longint;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   137
//  EndSep : Set of Char;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   138
//begin
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   139
//  I := Length(FileName);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   140
//  EndSep:=AllowDirectorySeparators+AllowDriveSeparators;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   141
//  while (I > 0) and not (FileName[I] in EndSep) do
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   142
//    Dec(I);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   143
//  Result := Copy(FileName, I + 1, MaxInt);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   144
//end;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   145
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   146
string255 fpcrtl_extractFileName(string255 f)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   147
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   148
    const char sep[] = {'\\', '/', ':'};
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   149
    LongInt i,j;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   150
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   151
    i = f.len - 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   152
    while(i >= 0){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   153
        for(j = 0; j < sizeof(sep); j++){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   154
            if(f.str[i] == sep[j]){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   155
                goto FPCRTL_EXTRACTFILENAME_END;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   156
            }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   157
        }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   158
        i--;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   159
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   160
FPCRTL_EXTRACTFILENAME_END:
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   161
    return fpcrtl_copy(f, i + 2, 256);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   162
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   163
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   164
string255 fpcrtl_strPas(PChar p)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   165
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   166
    string255 s;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   167
    int l = strlen(p);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   168
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   169
    if(l > 255){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   170
        printf("strPas: source string length > 255\n");
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   171
        assert(0);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   172
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   173
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   174
    s.len = l;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   175
    strcpy(s.str, p);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   176
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   177
    return s;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   178
}