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