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