project_files/hwc/rtl/misc.c
author koda
Tue, 04 Jun 2013 23:48:44 +0200
branchwebgl
changeset 9133 a269fe7090a0
parent 7983 02f36c3e7f6c
child 10015 4feced261c68
permissions -rw-r--r--
actually everything is CLANG'd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     1
#include "misc.h"
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     2
#include <stdio.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     3
#include <stdarg.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     4
#include <string.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     5
#include <assert.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     6
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     7
char strbuf[512];
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     8
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     9
void fpcrtl_assert(int i)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    10
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    11
    if(!i){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    12
        assert(0);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    13
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    14
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    15
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    16
// EFFECTS: return the nearest integer of the given number
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    17
int fpcrtl_round(double number)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    18
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    19
    return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    20
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    21
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    22
void fpcrtl_printf(const char* format, ...)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    23
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    24
#ifdef FPCRTL_DEBUG
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    25
    va_list args;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    26
    va_start (args, format);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    27
    vprintf (format, args);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    28
    va_end (args);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    29
#endif
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    30
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    31
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    32
//
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    33
//void fpcrtl_check_string(string255 str)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    34
//{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    35
//#ifdef FPCRTL_DEBUG
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    36
//    int len = strlen(str.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    37
//    if(len != str.len){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    38
//        printf("String %s internal inconsistency error. Length should be %d but actually is %d.\n", str.str, len, str.len);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    39
//    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    40
//    //assert(len == str.len);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    41
//#endif
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    42
//}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    43
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    44
string255 fpcrtl_strconcat(string255 str1, string255 str2)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    45
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    46
    //printf("str1 = %d, %d\n", str1.len, strlen(str1.str));
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    47
    //printf("str2 = %d, %d\n", str2.len, strlen(str2.str));
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    48
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    49
#ifdef FPCRTL_DEBUG
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    50
    if(str1.len + (int)(str2.len) > 255){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    51
        printf("String overflow\n");
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    52
        printf("str1(%d): %s\nstr2(%d): %s\n", str1.len, str1.str, str2.len, str2.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    53
        printf("String will be truncated.\n");
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    54
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    55
        strbuf[0] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    56
        strcpy(strbuf, str1.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    57
        strcat(strbuf, str2.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    58
        memcpy(str1.str, strbuf, 255);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    59
        str1.str[254] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    60
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    61
        return str1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    62
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    63
#endif
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    64
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    65
    memcpy(&(str1.str[str1.len]), str2.str, str2.len);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    66
    str1.str[str1.len + str2.len] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    67
    str1.len += str2.len;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    68
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    69
    return str1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    70
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    71
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    72
string255 fpcrtl_strappend(string255 s, char c)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    73
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    74
    s.str[s.len] = c;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    75
    s.str[s.len + 1] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    76
    s.len ++;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    77
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    78
    return s;
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
string255 fpcrtl_strprepend(char c, string255 s)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    82
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    83
    FIX_STRING(s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    84
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    85
    memmove(s.str + 1, s.str, s.len + 1); // also move '/0'
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    86
    s.str[0] = c;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    87
    s.len++;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    88
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    89
    return s;
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_chrconcat(char a, char b)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    93
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    94
    string255 result;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    95
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    96
    result.len = 2;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    97
    result.str[0] = a;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    98
    result.str[1] = b;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    99
    result.str[2] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   100
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   101
    return result;
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
bool fpcrtl_strcompare(string255 str1, string255 str2)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   105
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   106
    //printf("str1 = %d, %d\n", str1.len, strlen(str1.str));
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   107
    //printf("str2 = %d, %d\n", str2.len, strlen(str2.str));
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   108
    FIX_STRING(str1);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   109
    FIX_STRING(str2);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   110
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   111
    if(strcmp(str1.str, str2.str) == 0){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   112
        return true;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   113
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   114
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   115
    return false;
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
bool fpcrtl_strcomparec(string255 a, char b)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   119
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   120
    FIX_STRING(a);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   121
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   122
    if(a.len == 1 && a.str[0] == b){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   123
        return true;
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
    return false;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   127
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   128
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   129
bool fpcrtl_strncompare(string255 a, string255 b)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   130
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   131
    return !fpcrtl_strcompare(a, b);
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
//char* fpcrtl_pchar(string255 s)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   135
//{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   136
//    return s.str;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   137
//}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   138
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   139
string255 fpcrtl_pchar2str(char *s)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   140
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   141
    string255 result;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   142
    int t = strlen(s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   143
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   144
    if(t > 255){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   145
        printf("pchar2str, length > 255\n");
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   146
        assert(0);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   147
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   148
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   149
    result.len = t;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   150
    memcpy(result.str, s, t);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   151
    result.str[t] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   152
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   153
    return result;
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
string255 fpcrtl_make_string(const char* s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   157
    string255 result;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   158
    strcpy(result.str, s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   159
    result.len = strlen(s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   160
    return result;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   161
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   162
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   163
#ifdef EMSCRIPTEN
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   164
GLenum glewInit()
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   165
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   166
    return GLEW_OK;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   167
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   168
#endif