removed SET_CURSOR message

master
Allen Webster 2016-05-27 10:51:12 -04:00
parent 939967bc94
commit b509731104
5 changed files with 600 additions and 620 deletions

View File

@ -8,13 +8,13 @@ NOTES ON USE:
#ifdef FCPP_NO_CRT #ifdef FCPP_NO_CRT
# ifndef FCPP_NO_MALLOC # ifndef FCPP_NO_MALLOC
# define FCPP_NO_MALLOC # define FCPP_NO_MALLOC
# endif # endif
# ifndef FCPP_NO_ASSERT # ifndef FCPP_NO_ASSERT
# define FCPP_NO_ASSERT # define FCPP_NO_ASSERT
# endif # endif
# ifndef FCPP_NO_STRING # ifndef FCPP_NO_STRING
# define FCPP_NO_STRING # define FCPP_NO_STRING
# endif # endif
#endif #endif
@ -36,10 +36,10 @@ NOTES ON USE:
#ifndef FCPP_NO_MALLOC #ifndef FCPP_NO_MALLOC
# ifndef FCPP_GET_MEMORY # ifndef FCPP_GET_MEMORY
# define FCPP_GET_MEMORY malloc # define FCPP_GET_MEMORY malloc
# endif # endif
# ifndef FCPP_FREE_MEMORY # ifndef FCPP_FREE_MEMORY
# define FCPP_FREE_MEMORY free # define FCPP_FREE_MEMORY free
# endif # endif
#else #else
# ifndef FCPP_FORBID_MALLOC # ifndef FCPP_FORBID_MALLOC

View File

@ -7,6 +7,8 @@
#define UseInterfacesThatArePhasingOut 0 #define UseInterfacesThatArePhasingOut 0
#include "4coder_helper.h" #include "4coder_helper.h"
#include <assert.h>
static void static void
write_string(Application_Links *app, String string){ write_string(Application_Links *app, String string){
Buffer_Summary buffer = app->get_active_buffer(app); Buffer_Summary buffer = app->get_active_buffer(app);

View File

@ -9,7 +9,7 @@ NOTES ON USE:
- this option is unset after use so that future includes of this file - this option is unset after use so that future includes of this file
in the same unit do not continue to output implementations in the same unit do not continue to output implementations
FCPP_LINK - defines linkage of non-inline functions, defaults to static FSTRING_LINK - defines linkage of non-inline functions, defaults to static
FCPP_EXTERN changes FCPP_LINK default to extern, this option is ignored if FCPP_LINK is defined FCPP_EXTERN changes FCPP_LINK default to extern, this option is ignored if FCPP_LINK is defined
include the file "4cpp_clear_config.h" if yo want to undefine all options for some reason include the file "4cpp_clear_config.h" if yo want to undefine all options for some reason
@ -19,17 +19,18 @@ NOTES ON USE:
*/ */
// TOP // TOP
// TODO(allen):
// - comments
// - memcpy / memmove replacements (different file for optimization options?)
// - examples and docs
//
#include "4coder_config.h"
#ifndef FCPP_STRING_INC #ifndef FCPP_STRING_INC
#define FCPP_STRING_INC #define FCPP_STRING_INC
#ifndef FSTRING_LINK
# define FSTRING_LINK static
#endif
#ifndef FSTRING_INLINE
# define FSTRING_INLINE inline
#endif
#ifndef FRED_STRING_STRUCT #ifndef FRED_STRING_STRUCT
#define FRED_STRING_STRUCT #define FRED_STRING_STRUCT
struct String{ struct String{
@ -44,29 +45,29 @@ struct Offset_String{
}; };
#endif #endif
inline bool char_not_slash(char c) { return (c != '\\' && c != '/'); } FSTRING_INLINE bool char_not_slash(char c) { return (c != '\\' && c != '/'); }
inline bool char_is_slash(char c) { return (c == '\\' || c == '/'); } FSTRING_INLINE bool char_is_slash(char c) { return (c == '\\' || c == '/'); }
inline char char_to_upper(char c) { return (c >= 'a' && c <= 'z') ? c + (char)('A' - 'a') : c; } FSTRING_INLINE char char_to_upper(char c) { return (c >= 'a' && c <= 'z') ? c + (char)('A' - 'a') : c; }
inline char char_to_lower(char c) { return (c >= 'A' && c <= 'Z') ? c - (char)('A' - 'a') : c; } FSTRING_INLINE char char_to_lower(char c) { return (c >= 'A' && c <= 'Z') ? c - (char)('A' - 'a') : c; }
inline int char_to_int(char c) { return (c - '0'); } FSTRING_INLINE int char_to_int(char c) { return (c - '0'); }
inline char int_to_char(int x) { return (char)(x + '0'); } FSTRING_INLINE char int_to_char(int x) { return (char)(x + '0'); }
inline bool char_is_whitespace(char c) { return (c == ' ' || c == '\n' || c == '\r' || c == '\t'); } FSTRING_INLINE bool char_is_whitespace(char c) { return (c == ' ' || c == '\n' || c == '\r' || c == '\t'); }
inline bool char_is_white_not_r(char c) { return (c == ' ' || c == '\n' || c == '\t'); } FSTRING_INLINE bool char_is_white_not_r(char c) { return (c == ' ' || c == '\n' || c == '\t'); }
inline bool char_is_lower(char c) { return (c >= 'a' && c <= 'z'); } FSTRING_INLINE bool char_is_lower(char c) { return (c >= 'a' && c <= 'z'); }
inline bool char_is_upper(char c) { return (c >= 'A' && c <= 'Z'); } FSTRING_INLINE bool char_is_upper(char c) { return (c >= 'A' && c <= 'Z'); }
inline bool char_is_alpha(char c) { return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_'); } FSTRING_INLINE bool char_is_alpha(char c) { return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_'); }
inline bool char_is_alpha_true(char c) { return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'); } FSTRING_INLINE bool char_is_alpha_true(char c) { return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'); }
inline bool char_is_numeric(char c) { return (c >= '0' && c <= '9'); } FSTRING_INLINE bool char_is_numeric(char c) { return (c >= '0' && c <= '9'); }
inline bool char_is_alpha_numeric_true(char c) { return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9'); } FSTRING_INLINE bool char_is_alpha_numeric_true(char c) { return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9'); }
inline bool char_is_alpha_numeric(char c) { return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_'); } FSTRING_INLINE bool char_is_alpha_numeric(char c) { return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_'); }
inline bool char_is_hex(char c) { return c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f'; } FSTRING_INLINE bool char_is_hex(char c) { return c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f'; }
inline bool char_is_basic(char c) { return c >= ' ' && c <= '~'; } FSTRING_INLINE bool char_is_basic(char c) { return c >= ' ' && c <= '~'; }
inline String string_zero(); FSTRING_INLINE String string_zero();
inline String make_string(void *s, int size, int mem_size); FSTRING_INLINE String make_string(void *s, int size, int mem_size);
inline String make_string(void *s, int size); FSTRING_INLINE String make_string(void *s, int size);
#define make_lit_string(str) (make_string((char*)(str), sizeof(str)-1, sizeof(str))) #define make_lit_string(str) (make_string((char*)(str), sizeof(str)-1, sizeof(str)))
#define make_fixed_width_string(str) (make_string((char*)(str), 0, sizeof(str))) #define make_fixed_width_string(str) (make_string((char*)(str), 0, sizeof(str)))
@ -77,92 +78,92 @@ inline String make_string(void *s, int size);
#define expand_str(s) ((s).str), ((s).size) #define expand_str(s) ((s).str), ((s).size)
inline String make_string_slowly(void *s); FSTRING_INLINE String make_string_slowly(void *s);
inline char* make_c_str(String s); FSTRING_INLINE char* make_c_str(String s);
inline String substr(String str, int start); FSTRING_INLINE String substr(String str, int start);
inline String substr(String str, int start, int size); FSTRING_INLINE String substr(String str, int start, int size);
inline String substr_slowly(char *s, int start); FSTRING_INLINE String substr_slowly(char *s, int start);
inline String substr(char *s, int start, int size); FSTRING_INLINE String substr(char *s, int start, int size);
inline String tailstr(String s); FSTRING_INLINE String tailstr(String s);
FCPP_LINK int str_size(char *s); FSTRING_LINK int str_size(char *s);
FCPP_LINK bool match(char *a, char *b); FSTRING_LINK bool match(char *a, char *b);
FCPP_LINK bool match(String a, char *b); FSTRING_LINK bool match(String a, char *b);
inline bool match(char *a, String b) { return match(b,a); } FSTRING_INLINE bool match(char *a, String b) { return match(b,a); }
FCPP_LINK bool match(String a, String b); FSTRING_LINK bool match(String a, String b);
FCPP_LINK bool match_part(char *a, char *b, int *len); FSTRING_LINK bool match_part(char *a, char *b, int *len);
FCPP_LINK bool match_part(String a, char *b, int *len); FSTRING_LINK bool match_part(String a, char *b, int *len);
inline bool match_part(char *a, char *b) { int x; return match_part(a,b,&x); } FSTRING_INLINE bool match_part(char *a, char *b) { int x; return match_part(a,b,&x); }
inline bool match_part(String a, char *b) { int x; return match_part(a,b,&x); } FSTRING_INLINE bool match_part(String a, char *b) { int x; return match_part(a,b,&x); }
FCPP_LINK bool match_part(char *a, String b); FSTRING_LINK bool match_part(char *a, String b);
FCPP_LINK bool match_part(String a, String b); FSTRING_LINK bool match_part(String a, String b);
FCPP_LINK bool match_unsensitive(char *a, char *b); FSTRING_LINK bool match_unsensitive(char *a, char *b);
FCPP_LINK bool match_unsensitive(String a, char *b); FSTRING_LINK bool match_unsensitive(String a, char *b);
inline bool match_unsensitive(char *a, String b) { return match_unsensitive(b,a); } FSTRING_INLINE bool match_unsensitive(char *a, String b) { return match_unsensitive(b,a); }
FCPP_LINK bool match_unsensitive(String a, String b); FSTRING_LINK bool match_unsensitive(String a, String b);
FCPP_LINK bool match_part_unsensitive(char *a, char *b, int *len); FSTRING_LINK bool match_part_unsensitive(char *a, char *b, int *len);
FCPP_LINK bool match_part_unsensitive(String a, char *b, int *len); FSTRING_LINK bool match_part_unsensitive(String a, char *b, int *len);
inline bool match_part_unsensitive(char *a, char *b) { int x; return match_part(a,b,&x); } FSTRING_INLINE bool match_part_unsensitive(char *a, char *b) { int x; return match_part(a,b,&x); }
inline bool match_part_unsensitive(String a, char *b) { int x; return match_part(a,b,&x); } FSTRING_INLINE bool match_part_unsensitive(String a, char *b) { int x; return match_part(a,b,&x); }
FCPP_LINK bool match_part_unsensitive(char *a, String b); FSTRING_LINK bool match_part_unsensitive(char *a, String b);
FCPP_LINK bool match_part_unsensitive(String a, String b); FSTRING_LINK bool match_part_unsensitive(String a, String b);
FCPP_LINK int find(char *s, int start, char c); FSTRING_LINK int find(char *s, int start, char c);
FCPP_LINK int find(String s, int start, char c); FSTRING_LINK int find(String s, int start, char c);
FCPP_LINK int find(char *s, int start, char *c); FSTRING_LINK int find(char *s, int start, char *c);
FCPP_LINK int find(String s, int start, char *c); FSTRING_LINK int find(String s, int start, char *c);
FCPP_LINK int find_substr(char *s, int start, String seek); FSTRING_LINK int find_substr(char *s, int start, String seek);
FCPP_LINK int find_substr(String s, int start, String seek); FSTRING_LINK int find_substr(String s, int start, String seek);
FCPP_LINK int rfind_substr(String s, int start, String seek); FSTRING_LINK int rfind_substr(String s, int start, String seek);
FCPP_LINK int find_substr_unsensitive(char *s, int start, String seek); FSTRING_LINK int find_substr_unsensitive(char *s, int start, String seek);
FCPP_LINK int find_substr_unsensitive(String s, int start, String seek); FSTRING_LINK int find_substr_unsensitive(String s, int start, String seek);
inline bool has_substr(char *s, String seek) { return (s[find_substr(s, 0, seek)] != 0); } FSTRING_INLINE bool has_substr(char *s, String seek) { return (s[find_substr(s, 0, seek)] != 0); }
inline bool has_substr(String s, String seek) { return (find_substr(s, 0, seek) < s.size); } FSTRING_INLINE bool has_substr(String s, String seek) { return (find_substr(s, 0, seek) < s.size); }
inline bool has_substr_unsensitive(char *s, String seek) { return (s[find_substr_unsensitive(s, 0, seek)] != 0); } FSTRING_INLINE bool has_substr_unsensitive(char *s, String seek) { return (s[find_substr_unsensitive(s, 0, seek)] != 0); }
inline bool has_substr_unsensitive(String s, String seek) { return (find_substr_unsensitive(s, 0, seek) < s.size); } FSTRING_INLINE bool has_substr_unsensitive(String s, String seek) { return (find_substr_unsensitive(s, 0, seek) < s.size); }
FCPP_LINK int int_to_str_size(int x); FSTRING_LINK int int_to_str_size(int x);
FCPP_LINK int int_to_str(int x, char *s_out); FSTRING_LINK int int_to_str(int x, char *s_out);
FCPP_LINK bool int_to_str(int x, String *s_out); FSTRING_LINK bool int_to_str(int x, String *s_out);
FCPP_LINK bool append_int_to_str(int x, String *s_out); FSTRING_LINK bool append_int_to_str(int x, String *s_out);
FCPP_LINK int str_to_int(char *s); FSTRING_LINK int str_to_int(char *s);
FCPP_LINK int str_to_int(String s); FSTRING_LINK int str_to_int(String s);
FCPP_LINK int hexchar_to_int(char c); FSTRING_LINK int hexchar_to_int(char c);
FCPP_LINK char int_to_hexchar(int c); FSTRING_LINK char int_to_hexchar(int c);
FCPP_LINK unsigned int hexstr_to_int(String s); FSTRING_LINK unsigned int hexstr_to_int(String s);
FCPP_LINK bool color_to_hexstr(unsigned int color, String *s_out); FSTRING_LINK bool color_to_hexstr(unsigned int color, String *s_out);
FCPP_LINK bool hexstr_to_color(String s, unsigned int *color); FSTRING_LINK bool hexstr_to_color(String s, unsigned int *color);
FCPP_LINK int copy_fast_unsafe(char *dest, char *src); FSTRING_LINK int copy_fast_unsafe(char *dest, char *src);
FCPP_LINK void copy_fast_unsafe(char *dest, String src); FSTRING_LINK void copy_fast_unsafe(char *dest, String src);
FCPP_LINK bool copy_checked(String *dest, String src); FSTRING_LINK bool copy_checked(String *dest, String src);
FCPP_LINK bool copy_partial(String *dest, char *src); FSTRING_LINK bool copy_partial(String *dest, char *src);
FCPP_LINK bool copy_partial(String *dest, String src); FSTRING_LINK bool copy_partial(String *dest, String src);
inline int copy(char *dest, char *src) { return copy_fast_unsafe(dest, src); } FSTRING_INLINE int copy(char *dest, char *src) { return copy_fast_unsafe(dest, src); }
inline void copy(String *dest, String src) { copy_checked(dest, src); } FSTRING_INLINE void copy(String *dest, String src) { copy_checked(dest, src); }
inline void copy(String *dest, char *src) { copy_partial(dest, src); } FSTRING_INLINE void copy(String *dest, char *src) { copy_partial(dest, src); }
FCPP_LINK bool append_checked(String *dest, String src); FSTRING_LINK bool append_checked(String *dest, String src);
FCPP_LINK bool append_partial(String *dest, char *src); FSTRING_LINK bool append_partial(String *dest, char *src);
FCPP_LINK bool append_partial(String *dest, String src); FSTRING_LINK bool append_partial(String *dest, String src);
FCPP_LINK bool append(String *dest, char c); FSTRING_LINK bool append(String *dest, char c);
inline bool append(String *dest, String src) { return append_partial(dest, src); } FSTRING_INLINE bool append(String *dest, String src) { return append_partial(dest, src); }
inline bool append(String *dest, char *src) { return append_partial(dest, src); } FSTRING_INLINE bool append(String *dest, char *src) { return append_partial(dest, src); }
inline bool terminate_with_null(String *str){ FSTRING_INLINE bool terminate_with_null(String *str){
bool result; bool result;
if (str->size < str->memory_size){ if (str->size < str->memory_size){
str->str[str->size] = 0; str->str[str->size] = 0;
@ -175,32 +176,32 @@ inline bool terminate_with_null(String *str){
return result; return result;
} }
FCPP_LINK int compare(char *a, char *b); FSTRING_LINK int compare(char *a, char *b);
FCPP_LINK int compare(String a, char *b); FSTRING_LINK int compare(String a, char *b);
inline int compare(char *a, String b) { return -compare(b,a); } FSTRING_INLINE int compare(char *a, String b) { return -compare(b,a); }
FCPP_LINK int compare(String a, String b); FSTRING_LINK int compare(String a, String b);
FCPP_LINK int reverse_seek_slash(String str); FSTRING_LINK int reverse_seek_slash(String str);
FCPP_LINK int reverse_seek_slash(String str, int start_pos); FSTRING_LINK int reverse_seek_slash(String str, int start_pos);
inline String front_of_directory(String dir) { return substr(dir, reverse_seek_slash(dir) + 1); } FSTRING_INLINE String front_of_directory(String dir) { return substr(dir, reverse_seek_slash(dir) + 1); }
inline String path_of_directory(String dir) { return substr(dir, 0, reverse_seek_slash(dir) + 1); } FSTRING_INLINE String path_of_directory(String dir) { return substr(dir, 0, reverse_seek_slash(dir) + 1); }
inline bool get_front_of_directory(String *dest, String dir) { return append_checked(dest, front_of_directory(dir)); } FSTRING_INLINE bool get_front_of_directory(String *dest, String dir) { return append_checked(dest, front_of_directory(dir)); }
inline bool get_path_of_directory(String *dest, String dir) { return append_checked(dest, path_of_directory(dir)); } FSTRING_INLINE bool get_path_of_directory(String *dest, String dir) { return append_checked(dest, path_of_directory(dir)); }
FCPP_LINK bool set_last_folder(String *dir, char *folder_name, char slash); FSTRING_LINK bool set_last_folder(String *dir, char *folder_name, char slash);
FCPP_LINK bool set_last_folder(String *dir, String folder_name, char slash); FSTRING_LINK bool set_last_folder(String *dir, String folder_name, char slash);
FCPP_LINK String file_extension(String str); FSTRING_LINK String file_extension(String str);
FCPP_LINK String file_extension_slowly(char *str); FSTRING_LINK String file_extension_slowly(char *str);
FCPP_LINK char * file_extension_c(String str); FSTRING_LINK char * file_extension_c(String str);
FCPP_LINK bool remove_last_folder(String *str); FSTRING_LINK bool remove_last_folder(String *str);
FCPP_LINK void replace_char(String str, char replace, char with); FSTRING_LINK void replace_char(String str, char replace, char with);
FCPP_LINK void replace_char(char *str, char replace, char with); FSTRING_LINK void replace_char(char *str, char replace, char with);
inline String string_zero(){ FSTRING_INLINE String string_zero(){
String str={0}; String str={0};
return(str); return(str);
} }
inline String make_string(void *str, int size, int mem_size){ FSTRING_INLINE String make_string(void *str, int size, int mem_size){
String result; String result;
result.str = (char*)str; result.str = (char*)str;
result.size = size; result.size = size;
@ -208,7 +209,7 @@ inline String make_string(void *str, int size, int mem_size){
return result; return result;
} }
inline String FSTRING_INLINE String
make_string(void *str, int size){ make_string(void *str, int size){
String result; String result;
result.str = (char*)str; result.str = (char*)str;
@ -217,7 +218,7 @@ make_string(void *str, int size){
return result; return result;
} }
inline String FSTRING_INLINE String
make_string_slowly(void *str){ make_string_slowly(void *str){
String result; String result;
result.str = (char*)str; result.str = (char*)str;
@ -226,7 +227,7 @@ make_string_slowly(void *str){
return result; return result;
} }
inline char* FSTRING_INLINE char*
make_c_str(String str){ make_c_str(String str){
if (str.size < str.memory_size){ if (str.size < str.memory_size){
str.str[str.size] = 0; str.str[str.size] = 0;
@ -237,7 +238,7 @@ make_c_str(String str){
return (char*)str.str; return (char*)str.str;
} }
inline String FSTRING_INLINE String
substr(String str, int start){ substr(String str, int start){
String result; String result;
result.str = str.str + start; result.str = str.str + start;
@ -245,7 +246,7 @@ substr(String str, int start){
return result; return result;
} }
inline String FSTRING_INLINE String
substr(String str, int start, int size){ substr(String str, int start, int size){
String result; String result;
result.str = str.str + start; result.str = str.str + start;
@ -258,7 +259,7 @@ substr(String str, int start, int size){
return result; return result;
} }
inline String FSTRING_INLINE String
substr_slowly(char *str, int start){ substr_slowly(char *str, int start){
String result; String result;
result.str = str + start; result.str = str + start;
@ -266,7 +267,7 @@ substr_slowly(char *str, int start){
return result; return result;
} }
inline String FSTRING_INLINE String
substr(char *str, int start, int size){ substr(char *str, int start, int size){
String result; String result;
result.str = str + start; result.str = str + start;
@ -280,7 +281,7 @@ substr(char *str, int start, int size){
return result; return result;
} }
inline String FSTRING_INLINE String
tailstr(String str){ tailstr(String str){
String result; String result;
result.str = str.str + str.size; result.str = str.str + str.size;
@ -296,14 +297,14 @@ tailstr(String str){
#ifndef FCPP_DID_STRING_IMPLEMENTATION #ifndef FCPP_DID_STRING_IMPLEMENTATION
#define FCPP_DID_STRING_IMPLEMENTATION #define FCPP_DID_STRING_IMPLEMENTATION
FCPP_LINK int FSTRING_LINK int
str_size(char *str){ str_size(char *str){
int i = 0; int i = 0;
while (str[i]) ++i; while (str[i]) ++i;
return i; return i;
} }
FCPP_LINK bool FSTRING_LINK bool
match(char *a, char *b){ match(char *a, char *b){
for (int i = 0;; ++i){ for (int i = 0;; ++i){
if (a[i] != b[i]){ if (a[i] != b[i]){
@ -315,7 +316,7 @@ match(char *a, char *b){
} }
} }
FCPP_LINK bool FSTRING_LINK bool
match(String a, char *b){ match(String a, char *b){
int i = 0; int i = 0;
for (; i < a.size; ++i){ for (; i < a.size; ++i){
@ -329,7 +330,7 @@ match(String a, char *b){
return 1; return 1;
} }
FCPP_LINK bool FSTRING_LINK bool
match(String a, String b){ match(String a, String b){
if (a.size != b.size){ if (a.size != b.size){
return 0; return 0;
@ -342,7 +343,7 @@ match(String a, String b){
return 1; return 1;
} }
FCPP_LINK bool FSTRING_LINK bool
match_part(char *a, char *b, int *len){ match_part(char *a, char *b, int *len){
int i; int i;
for (i = 0; b[i] != 0; ++i){ for (i = 0; b[i] != 0; ++i){
@ -354,7 +355,7 @@ match_part(char *a, char *b, int *len){
return 1; return 1;
} }
FCPP_LINK bool FSTRING_LINK bool
match_part(String a, char *b, int *len){ match_part(String a, char *b, int *len){
int i; int i;
for (i = 0; b[i] != 0; ++i){ for (i = 0; b[i] != 0; ++i){
@ -366,7 +367,7 @@ match_part(String a, char *b, int *len){
return 1; return 1;
} }
FCPP_LINK bool FSTRING_LINK bool
match_part(char *a, String b){ match_part(char *a, String b){
for (int i = 0; i != b.size; ++i){ for (int i = 0; i != b.size; ++i){
if (a[i] != b.str[i]){ if (a[i] != b.str[i]){
@ -376,7 +377,7 @@ match_part(char *a, String b){
return 1; return 1;
} }
FCPP_LINK bool FSTRING_LINK bool
match_part(String a, String b){ match_part(String a, String b){
if (a.size < b.size){ if (a.size < b.size){
return 0; return 0;
@ -389,7 +390,7 @@ match_part(String a, String b){
return 1; return 1;
} }
FCPP_LINK bool FSTRING_LINK bool
match_unsensitive(char *a, char *b){ match_unsensitive(char *a, char *b){
for (int i = 0;; ++i){ for (int i = 0;; ++i){
if (char_to_upper(a[i]) != if (char_to_upper(a[i]) !=
@ -402,7 +403,7 @@ match_unsensitive(char *a, char *b){
} }
} }
FCPP_LINK bool FSTRING_LINK bool
match_unsensitive(String a, char *b){ match_unsensitive(String a, char *b){
int i = 0; int i = 0;
for (; i < a.size; ++i){ for (; i < a.size; ++i){
@ -417,7 +418,7 @@ match_unsensitive(String a, char *b){
return 1; return 1;
} }
FCPP_LINK bool FSTRING_LINK bool
match_unsensitive(String a, String b){ match_unsensitive(String a, String b){
if (a.size != b.size){ if (a.size != b.size){
return 0; return 0;
@ -431,7 +432,7 @@ match_unsensitive(String a, String b){
return 1; return 1;
} }
FCPP_LINK bool FSTRING_LINK bool
match_part_unsensitive(char *a, char *b, int *len){ match_part_unsensitive(char *a, char *b, int *len){
int i; int i;
for (i = 0; b[i] != 0; ++i){ for (i = 0; b[i] != 0; ++i){
@ -443,7 +444,7 @@ match_part_unsensitive(char *a, char *b, int *len){
return 1; return 1;
} }
FCPP_LINK bool FSTRING_LINK bool
match_part_unsensitive(String a, char *b, int *len){ match_part_unsensitive(String a, char *b, int *len){
int i; int i;
for (i = 0; b[i] != 0; ++i){ for (i = 0; b[i] != 0; ++i){
@ -456,7 +457,7 @@ match_part_unsensitive(String a, char *b, int *len){
return 1; return 1;
} }
FCPP_LINK bool FSTRING_LINK bool
match_part_unsensitive(char *a, String b){ match_part_unsensitive(char *a, String b){
for (int i = 0; i != b.size; ++i){ for (int i = 0; i != b.size; ++i){
if (char_to_upper(a[i]) != char_to_upper(b.str[i])){ if (char_to_upper(a[i]) != char_to_upper(b.str[i])){
@ -466,7 +467,7 @@ match_part_unsensitive(char *a, String b){
return 1; return 1;
} }
FCPP_LINK bool FSTRING_LINK bool
match_part_unsensitive(String a, String b){ match_part_unsensitive(String a, String b){
if (a.size < b.size){ if (a.size < b.size){
return 0; return 0;
@ -479,21 +480,21 @@ match_part_unsensitive(String a, String b){
return 1; return 1;
} }
FCPP_LINK int FSTRING_LINK int
find(char *str, int start, char character){ find(char *str, int start, char character){
int i = start; int i = start;
while (str[i] != character && str[i] != 0) ++i; while (str[i] != character && str[i] != 0) ++i;
return i; return i;
} }
FCPP_LINK int FSTRING_LINK int
find(String str, int start, char character){ find(String str, int start, char character){
int i = start; int i = start;
while (i < str.size && str.str[i] != character) ++i; while (i < str.size && str.str[i] != character) ++i;
return i; return i;
} }
FCPP_LINK int FSTRING_LINK int
find(char *str, int start, char *characters){ find(char *str, int start, char *characters){
int i = start, j; int i = start, j;
while (str[i] != 0){ while (str[i] != 0){
@ -507,7 +508,7 @@ find(char *str, int start, char *characters){
return i; return i;
} }
FCPP_LINK int FSTRING_LINK int
find(String str, int start, char *characters){ find(String str, int start, char *characters){
int i = start, j; int i = start, j;
while (i < str.size){ while (i < str.size){
@ -521,7 +522,7 @@ find(String str, int start, char *characters){
return i; return i;
} }
FCPP_LINK int FSTRING_LINK int
find_substr(char *str, int start, String seek){ find_substr(char *str, int start, String seek){
int i, j, k; int i, j, k;
bool hit; bool hit;
@ -546,7 +547,7 @@ find_substr(char *str, int start, String seek){
return i; return i;
} }
FCPP_LINK int FSTRING_LINK int
find_substr(String str, int start, String seek){ find_substr(String str, int start, String seek){
int stop_at, i, j, k; int stop_at, i, j, k;
bool hit; bool hit;
@ -572,7 +573,7 @@ find_substr(String str, int start, String seek){
return str.size; return str.size;
} }
FCPP_LINK int FSTRING_LINK int
rfind_substr(String str, int start, String seek){ rfind_substr(String str, int start, String seek){
int i, j, k; int i, j, k;
bool hit; bool hit;
@ -600,7 +601,7 @@ rfind_substr(String str, int start, String seek){
return -1; return -1;
} }
FCPP_LINK int FSTRING_LINK int
find_substr_unsensitive(char *str, int start, String seek){ find_substr_unsensitive(char *str, int start, String seek){
int i, j, k; int i, j, k;
bool hit; bool hit;
@ -628,7 +629,7 @@ find_substr_unsensitive(char *str, int start, String seek){
return i; return i;
} }
FCPP_LINK int FSTRING_LINK int
find_substr_unsensitive(String str, int start, String seek){ find_substr_unsensitive(String str, int start, String seek){
int i, j, k; int i, j, k;
int stop_at; int stop_at;
@ -658,7 +659,7 @@ find_substr_unsensitive(String str, int start, String seek){
return str.size; return str.size;
} }
FCPP_LINK int FSTRING_LINK int
int_to_str_size(int x){ int_to_str_size(int x){
int size; int size;
if (x < 0){ if (x < 0){
@ -675,7 +676,7 @@ int_to_str_size(int x){
return size; return size;
} }
FCPP_LINK int FSTRING_LINK int
int_to_str(int x, char *str){ int_to_str(int x, char *str){
int size, i, j; int size, i, j;
bool negative; bool negative;
@ -712,7 +713,7 @@ int_to_str(int x, char *str){
return size; return size;
} }
FCPP_LINK bool FSTRING_LINK bool
int_to_str(int x, String *dest){ int_to_str(int x, String *dest){
bool result = 1; bool result = 1;
char *str = dest->str; char *str = dest->str;
@ -758,7 +759,7 @@ int_to_str(int x, String *dest){
return result; return result;
} }
FCPP_LINK bool FSTRING_LINK bool
append_int_to_str(int x, String *dest){ append_int_to_str(int x, String *dest){
String last_part = tailstr(*dest); String last_part = tailstr(*dest);
bool result = int_to_str(x, &last_part); bool result = int_to_str(x, &last_part);
@ -768,7 +769,7 @@ append_int_to_str(int x, String *dest){
return result; return result;
} }
FCPP_LINK int FSTRING_LINK int
str_to_int(char *str){ str_to_int(char *str){
int x = 0; int x = 0;
for (; *str; ++str){ for (; *str; ++str){
@ -784,7 +785,7 @@ str_to_int(char *str){
return(x); return(x);
} }
FCPP_LINK int FSTRING_LINK int
str_to_int(String str){ str_to_int(String str){
int x, i; int x, i;
if (str.size == 0){ if (str.size == 0){
@ -800,7 +801,7 @@ str_to_int(String str){
return x; return x;
} }
FCPP_LINK int FSTRING_LINK int
hexchar_to_int(char c){ hexchar_to_int(char c){
int x; int x;
if (c >= '0' && c <= '9'){ if (c >= '0' && c <= '9'){
@ -815,12 +816,12 @@ hexchar_to_int(char c){
return x; return x;
} }
FCPP_LINK char FSTRING_LINK char
int_to_hexchar(int x){ int_to_hexchar(int x){
return (x<10)?((char)x+'0'):((char)x+'a'-10); return (x<10)?((char)x+'0'):((char)x+'a'-10);
} }
FCPP_LINK unsigned int FSTRING_LINK unsigned int
hexstr_to_int(String str){ hexstr_to_int(String str){
unsigned int x; unsigned int x;
int i; int i;
@ -837,7 +838,7 @@ hexstr_to_int(String str){
return x; return x;
} }
FCPP_LINK bool FSTRING_LINK bool
color_to_hexstr(unsigned int color, String *s){ color_to_hexstr(unsigned int color, String *s){
bool result = 0; bool result = 0;
int i; int i;
@ -864,7 +865,7 @@ color_to_hexstr(unsigned int color, String *s){
return(result); return(result);
} }
FCPP_LINK bool FSTRING_LINK bool
hexstr_to_color(String s, unsigned int *out){ hexstr_to_color(String s, unsigned int *out){
bool result = 0; bool result = 0;
unsigned int color = 0; unsigned int color = 0;
@ -882,7 +883,7 @@ hexstr_to_color(String s, unsigned int *out){
return(result); return(result);
} }
FCPP_LINK int FSTRING_LINK int
copy_fast_unsafe(char *dest, char *src){ copy_fast_unsafe(char *dest, char *src){
char *start = dest; char *start = dest;
while (*src != 0){ while (*src != 0){
@ -893,7 +894,7 @@ copy_fast_unsafe(char *dest, char *src){
return (int)(dest - start); return (int)(dest - start);
} }
FCPP_LINK void FSTRING_LINK void
copy_fast_unsafe(char *dest, String src){ copy_fast_unsafe(char *dest, String src){
int i = 0; int i = 0;
while (i != src.size){ while (i != src.size){
@ -902,7 +903,7 @@ copy_fast_unsafe(char *dest, String src){
} }
} }
FCPP_LINK bool FSTRING_LINK bool
copy_checked(String *dest, String src){ copy_checked(String *dest, String src){
char *dest_str; char *dest_str;
int i; int i;
@ -917,7 +918,7 @@ copy_checked(String *dest, String src){
return 1; return 1;
} }
FCPP_LINK bool FSTRING_LINK bool
copy_partial(String *dest, char *src){ copy_partial(String *dest, char *src){
int i = 0; int i = 0;
int memory_size = dest->memory_size; int memory_size = dest->memory_size;
@ -933,7 +934,7 @@ copy_partial(String *dest, char *src){
return 1; return 1;
} }
FCPP_LINK bool FSTRING_LINK bool
copy_partial(String *dest, String src){ copy_partial(String *dest, String src){
bool result; bool result;
int memory_size = dest->memory_size; int memory_size = dest->memory_size;
@ -955,7 +956,7 @@ copy_partial(String *dest, String src){
return result; return result;
} }
FCPP_LINK bool FSTRING_LINK bool
append_checked(String *dest, String src){ append_checked(String *dest, String src){
String end; String end;
end = tailstr(*dest); end = tailstr(*dest);
@ -966,7 +967,7 @@ append_checked(String *dest, String src){
return result; return result;
} }
FCPP_LINK bool FSTRING_LINK bool
append_partial(String *dest, char *src){ append_partial(String *dest, char *src){
String end = tailstr(*dest); String end = tailstr(*dest);
bool result = copy_partial(&end, src); bool result = copy_partial(&end, src);
@ -974,7 +975,7 @@ append_partial(String *dest, char *src){
return result; return result;
} }
FCPP_LINK bool FSTRING_LINK bool
append_partial(String *dest, String src){ append_partial(String *dest, String src){
String end = tailstr(*dest); String end = tailstr(*dest);
bool result = copy_partial(&end, src); bool result = copy_partial(&end, src);
@ -982,7 +983,7 @@ append_partial(String *dest, String src){
return result; return result;
} }
FCPP_LINK bool FSTRING_LINK bool
append(String *dest, char c){ append(String *dest, char c){
bool result = 0; bool result = 0;
if (dest->size < dest->memory_size){ if (dest->size < dest->memory_size){
@ -992,7 +993,7 @@ append(String *dest, char c){
return result; return result;
} }
FCPP_LINK int FSTRING_LINK int
compare(char *a, char *b){ compare(char *a, char *b){
int i = 0; int i = 0;
while (a[i] == b[i] && a[i] != 0){ while (a[i] == b[i] && a[i] != 0){
@ -1001,7 +1002,7 @@ compare(char *a, char *b){
return (a[i] > b[i]) - (a[i] < b[i]); return (a[i] > b[i]) - (a[i] < b[i]);
} }
FCPP_LINK int FSTRING_LINK int
compare(String a, char *b){ compare(String a, char *b){
int i = 0; int i = 0;
while (i < a.size && a.str[i] == b[i]){ while (i < a.size && a.str[i] == b[i]){
@ -1020,7 +1021,7 @@ compare(String a, char *b){
} }
} }
FCPP_LINK int FSTRING_LINK int
compare(String a, String b){ compare(String a, String b){
int i = 0; int i = 0;
while (i < a.size && i < b.size && a.str[i] == b.str[i]){ while (i < a.size && i < b.size && a.str[i] == b.str[i]){
@ -1034,7 +1035,7 @@ compare(String a, String b){
} }
} }
FCPP_LINK int FSTRING_LINK int
reverse_seek_slash(String str, int pos){ reverse_seek_slash(String str, int pos){
int i = str.size - 1 - pos; int i = str.size - 1 - pos;
while (i >= 0 && char_not_slash(str.str[i])){ while (i >= 0 && char_not_slash(str.str[i])){
@ -1043,12 +1044,12 @@ reverse_seek_slash(String str, int pos){
return i; return i;
} }
FCPP_LINK int FSTRING_LINK int
reverse_seek_slash(String str){ reverse_seek_slash(String str){
return(reverse_seek_slash(str, 0)); return(reverse_seek_slash(str, 0));
} }
FCPP_LINK bool FSTRING_LINK bool
set_last_folder(String *dir, char *folder_name, char slash){ set_last_folder(String *dir, char *folder_name, char slash){
char str[2]; char str[2];
bool result = 0; bool result = 0;
@ -1067,7 +1068,7 @@ set_last_folder(String *dir, char *folder_name, char slash){
return result; return result;
} }
FCPP_LINK bool FSTRING_LINK bool
set_last_folder(String *dir, String folder_name, char slash){ set_last_folder(String *dir, String folder_name, char slash){
char str[2]; char str[2];
bool result = 0; bool result = 0;
@ -1086,7 +1087,7 @@ set_last_folder(String *dir, String folder_name, char slash){
return result; return result;
} }
FCPP_LINK String FSTRING_LINK String
file_extension(String str){ file_extension(String str){
int i; int i;
for (i = str.size - 1; i >= 0; --i){ for (i = str.size - 1; i >= 0; --i){
@ -1096,7 +1097,7 @@ file_extension(String str){
return make_string(str.str+i, str.size-i); return make_string(str.str+i, str.size-i);
} }
FCPP_LINK String FSTRING_LINK String
file_extension_slowly(char *str){ file_extension_slowly(char *str){
int s, i; int s, i;
for (s = 0; str[s]; ++s); for (s = 0; str[s]; ++s);
@ -1107,7 +1108,7 @@ file_extension_slowly(char *str){
return make_string(str+i, s-i); return make_string(str+i, s-i);
} }
FCPP_LINK char* FSTRING_LINK char*
file_extension_c(String str){ file_extension_c(String str){
int i; int i;
for (i = str.size - 1; i >= 0; --i){ for (i = str.size - 1; i >= 0; --i){
@ -1117,7 +1118,7 @@ file_extension_c(String str){
return str.str+i; return str.str+i;
} }
FCPP_LINK bool FSTRING_LINK bool
remove_last_folder(String *str){ remove_last_folder(String *str){
bool result = 0; bool result = 0;
int end = reverse_seek_slash(*str, 1); int end = reverse_seek_slash(*str, 1);
@ -1128,7 +1129,7 @@ remove_last_folder(String *str){
return(result); return(result);
} }
FCPP_LINK void FSTRING_LINK void
replace_char(String str, char replace, char with){ replace_char(String str, char replace, char with){
char *s = str.str; char *s = str.str;
int i; int i;
@ -1138,7 +1139,7 @@ replace_char(String str, char replace, char with){
} }
} }
FCPP_LINK void FSTRING_LINK void
replace_char(char *str, char replace, char with){ replace_char(char *str, char replace, char with){
for (; *str; ++str){ for (; *str; ++str){
if (*str == replace) *str = with; if (*str == replace) *str = with;
@ -1157,7 +1158,7 @@ struct Absolutes{
int count; int count;
}; };
FCPP_LINK void FSTRING_LINK void
get_absolutes(String name, Absolutes *absolutes, bool implicit_first, bool implicit_last){ get_absolutes(String name, Absolutes *absolutes, bool implicit_first, bool implicit_last){
int count = 0; int count = 0;
int max = ArrayCount(absolutes->a) - 1; int max = ArrayCount(absolutes->a) - 1;
@ -1203,7 +1204,7 @@ get_absolutes(String name, Absolutes *absolutes, bool implicit_first, bool impli
absolutes->count = count; absolutes->count = count;
} }
FCPP_LINK bool FSTRING_LINK bool
wildcard_match(Absolutes *absolutes, char *x, int case_sensitive){ wildcard_match(Absolutes *absolutes, char *x, int case_sensitive){
bool r = 1; bool r = 1;
String *a = absolutes->a; String *a = absolutes->a;
@ -1261,7 +1262,7 @@ wildcard_match(Absolutes *absolutes, char *x, int case_sensitive){
return r; return r;
} }
FCPP_LINK bool FSTRING_LINK bool
wildcard_match(Absolutes *absolutes, String x, int case_sensitive){ wildcard_match(Absolutes *absolutes, String x, int case_sensitive){
terminate_with_null(&x); terminate_with_null(&x);
return wildcard_match(absolutes, x.str, case_sensitive); return wildcard_match(absolutes, x.str, case_sensitive);

View File

@ -1,378 +1,378 @@
/* /*
* Mr. 4th Dimention - Allen Webster * Mr. 4th Dimention - Allen Webster
* *
* 20.11.2015 * 20.11.2015
* *
* DLL loader declarations for 4coder * DLL loader declarations for 4coder
* *
*/ */
// TOP // TOP
// TODO(allen): // TODO(allen):
// Check the relocation table, if it contains anything that // Check the relocation table, if it contains anything that
// is platform specific generate an error to avoid calling // is platform specific generate an error to avoid calling
// into invalid code. // into invalid code.
i32 i32
dll_compare(char *a, char *b, i32 len){ dll_compare(char *a, char *b, i32 len){
i32 result; i32 result;
char *e; char *e;
result = 0; result = 0;
e = a + len; e = a + len;
for (;a < e && *a == *b; ++a, ++b); for (;a < e && *a == *b; ++a, ++b);
if (a < e){ if (a < e){
if (*a < *b) result = -1; if (*a < *b) result = -1;
else result = 1; else result = 1;
} }
return(result); return(result);
} }
enum DLL_Error{ enum DLL_Error{
dll_err_too_small_for_header = 1, dll_err_too_small_for_header = 1,
dll_err_wrong_MZ_signature, dll_err_wrong_MZ_signature,
dll_err_wrong_DOS_error, dll_err_wrong_DOS_error,
dll_err_wrong_PE_signature, dll_err_wrong_PE_signature,
dll_err_unrecognized_bit_signature, dll_err_unrecognized_bit_signature,
}; };
b32 b32
dll_parse_headers(Data file, DLL_Data *dll, i32 *error){ dll_parse_headers(Data file, DLL_Data *dll, i32 *error){
b32 result; b32 result;
i32 pe_offset; i32 pe_offset;
i32 read_pos; i32 read_pos;
result = 1; result = 1;
if (file.size <= sizeof(DOS_Header) + DOS_error_size){ if (file.size <= sizeof(DOS_Header) + DOS_error_size){
if (error) *error = dll_err_too_small_for_header; if (error) *error = dll_err_too_small_for_header;
result = 0; result = 0;
goto dll_parse_end; goto dll_parse_end;
} }
dll->dos_header = (DOS_Header*)file.data; dll->dos_header = (DOS_Header*)file.data;
if (dll_compare(dll->dos_header->signature, "MZ", 2) != 0){ if (dll_compare(dll->dos_header->signature, "MZ", 2) != 0){
if (error) *error = dll_err_wrong_MZ_signature; if (error) *error = dll_err_wrong_MZ_signature;
result = 0; result = 0;
goto dll_parse_end; goto dll_parse_end;
} }
if (file.size <= DOS_error_offset + DOS_error_size){ if (file.size <= DOS_error_offset + DOS_error_size){
if (error) *error = dll_err_too_small_for_header; if (error) *error = dll_err_too_small_for_header;
result = 0; result = 0;
goto dll_parse_end; goto dll_parse_end;
} }
if (dll_compare((char*)(file.data + DOS_error_offset), DOS_error_message, if (dll_compare((char*)(file.data + DOS_error_offset), DOS_error_message,
sizeof(DOS_error_message) - 1) != 0){ sizeof(DOS_error_message) - 1) != 0){
if (error) *error = dll_err_wrong_DOS_error; if (error) *error = dll_err_wrong_DOS_error;
result = 0; result = 0;
goto dll_parse_end; goto dll_parse_end;
} }
pe_offset = dll->dos_header->e_lfanew; pe_offset = dll->dos_header->e_lfanew;
read_pos = pe_offset; read_pos = pe_offset;
if (file.size <= read_pos + PE_header_size){ if (file.size <= read_pos + PE_header_size){
if (error) *error = dll_err_too_small_for_header; if (error) *error = dll_err_too_small_for_header;
result = 0; result = 0;
goto dll_parse_end; goto dll_parse_end;
} }
if (dll_compare((char*)(file.data + read_pos), if (dll_compare((char*)(file.data + read_pos),
PE_header, PE_header_size) != 0){ PE_header, PE_header_size) != 0){
if (error) *error = dll_err_wrong_PE_signature; if (error) *error = dll_err_wrong_PE_signature;
result = 0; result = 0;
goto dll_parse_end; goto dll_parse_end;
} }
read_pos += PE_header_size; read_pos += PE_header_size;
if (file.size <= read_pos + sizeof(COFF_Header)){ if (file.size <= read_pos + sizeof(COFF_Header)){
if (error) *error = dll_err_too_small_for_header; if (error) *error = dll_err_too_small_for_header;
result = 0; result = 0;
goto dll_parse_end; goto dll_parse_end;
} }
dll->coff_header = (COFF_Header*)(file.data + read_pos); dll->coff_header = (COFF_Header*)(file.data + read_pos);
read_pos += sizeof(COFF_Header); read_pos += sizeof(COFF_Header);
if (file.size <= read_pos + dll->coff_header->size_of_optional_header){ if (file.size <= read_pos + dll->coff_header->size_of_optional_header){
if (error) *error = dll_err_too_small_for_header; if (error) *error = dll_err_too_small_for_header;
result = 0; result = 0;
goto dll_parse_end; goto dll_parse_end;
} }
dll->opt_header_32 = (PE_Opt_Header_32Bit*)(file.data + read_pos); dll->opt_header_32 = (PE_Opt_Header_32Bit*)(file.data + read_pos);
dll->opt_header_64 = (PE_Opt_Header_64Bit*)(file.data + read_pos); dll->opt_header_64 = (PE_Opt_Header_64Bit*)(file.data + read_pos);
read_pos += dll->coff_header->size_of_optional_header; read_pos += dll->coff_header->size_of_optional_header;
if (dll->opt_header_32->signature != bitsig_32bit && if (dll->opt_header_32->signature != bitsig_32bit &&
dll->opt_header_32->signature != bitsig_64bit){ dll->opt_header_32->signature != bitsig_64bit){
if (error) *error = dll_err_unrecognized_bit_signature; if (error) *error = dll_err_unrecognized_bit_signature;
result = 0; result = 0;
goto dll_parse_end; goto dll_parse_end;
} }
if (dll->opt_header_32->signature == bitsig_32bit) dll->is_64bit = 0; if (dll->opt_header_32->signature == bitsig_32bit) dll->is_64bit = 0;
else dll->is_64bit = 1; else dll->is_64bit = 1;
dll->section_defs = (PE_Section_Definition*)(file.data + read_pos); dll->section_defs = (PE_Section_Definition*)(file.data + read_pos);
dll_parse_end: dll_parse_end:
return(result); return(result);
} }
i32 i32
dll_total_loaded_size(DLL_Data *dll){ dll_total_loaded_size(DLL_Data *dll){
COFF_Header *coff_header; COFF_Header *coff_header;
PE_Section_Definition *section_def; PE_Section_Definition *section_def;
i32 result, section_end, i; i32 result, section_end, i;
coff_header = dll->coff_header; coff_header = dll->coff_header;
section_def = dll->section_defs; section_def = dll->section_defs;
result = 0; result = 0;
for (i = 0; i < coff_header->number_of_sections; ++i, ++section_def){ for (i = 0; i < coff_header->number_of_sections; ++i, ++section_def){
section_end = section_def->loaded_location + section_def->loaded_size; section_end = section_def->loaded_location + section_def->loaded_size;
if (section_end > result){ if (section_end > result){
result = section_end; result = section_end;
} }
} }
return(result); return(result);
} }
b32 b32
dll_perform_reloc(DLL_Loaded *loaded){ dll_perform_reloc(DLL_Loaded *loaded){
Data img; Data img;
byte *base; byte *base;
Relocation_Block_Header *header; Relocation_Block_Header *header;
Relocation_Block_Entry *entry; Relocation_Block_Entry *entry;
Data_Directory *data_directory; Data_Directory *data_directory;
u32 cursor; u32 cursor;
u32 bytes_in_table; u32 bytes_in_table;
u32 block_end; u32 block_end;
u32 type; u32 type;
u32 offset; u32 offset;
b32 result; b32 result;
b32 highadj_stage; b32 highadj_stage;
u64 dif64; u64 dif64;
result = 1; result = 1;
img = loaded->img; img = loaded->img;
if (loaded->is_64bit){ if (loaded->is_64bit){
data_directory = loaded->opt_header_64->data_directory; data_directory = loaded->opt_header_64->data_directory;
dif64 = ((u64)img.data - (u64)loaded->opt_header_64->image_base); dif64 = ((u64)img.data - (u64)loaded->opt_header_64->image_base);
} }
else{ else{
data_directory = loaded->opt_header_32->data_directory; data_directory = loaded->opt_header_32->data_directory;
dif64 = ((u64)img.data - (u64)loaded->opt_header_32->image_base); dif64 = ((u64)img.data - (u64)loaded->opt_header_32->image_base);
} }
data_directory += image_dir_base_reloc_table; data_directory += image_dir_base_reloc_table;
base = img.data + data_directory->virtual_address; base = img.data + data_directory->virtual_address;
bytes_in_table = data_directory->size; bytes_in_table = data_directory->size;
highadj_stage = 1; highadj_stage = 1;
for (cursor = 0; cursor < bytes_in_table;){ for (cursor = 0; cursor < bytes_in_table;){
header = (Relocation_Block_Header*)(base + cursor); header = (Relocation_Block_Header*)(base + cursor);
block_end = cursor + header->block_size; block_end = cursor + header->block_size;
cursor += sizeof(Relocation_Block_Header); cursor += sizeof(Relocation_Block_Header);
for (;cursor < block_end;){ for (;cursor < block_end;){
entry = (Relocation_Block_Entry*)(base + cursor); entry = (Relocation_Block_Entry*)(base + cursor);
cursor += sizeof(Relocation_Block_Entry); cursor += sizeof(Relocation_Block_Entry);
type = (u32)(entry->entry & reloc_entry_type_mask) >> reloc_entry_type_shift; type = (u32)(entry->entry & reloc_entry_type_mask) >> reloc_entry_type_shift;
offset = (u32)(entry->entry & reloc_entry_offset_mask) + header->page_base_offset; offset = (u32)(entry->entry & reloc_entry_offset_mask) + header->page_base_offset;
switch (type){ switch (type){
case image_base_absolute: break; case image_base_absolute: break;
case image_base_high: case image_base_high:
case image_base_low: case image_base_low:
case image_base_highlow: case image_base_highlow:
case image_base_highadj: case image_base_highadj:
case image_base_arm_mov32a: case image_base_arm_mov32a:
case image_base_arm_mov32t: case image_base_arm_mov32t:
case image_base_mips_jmpaddr16: case image_base_mips_jmpaddr16:
result = 0; result = 0;
goto dll_reloc_end; goto dll_reloc_end;
case image_base_dir64: case image_base_dir64:
*(u64*)(img.data + offset) += dif64; *(u64*)(img.data + offset) += dif64;
break; break;
} }
} }
} }
dll_reloc_end: dll_reloc_end:
return(result); return(result);
} }
b32 b32
dll_load_sections(Data img, DLL_Loaded *loaded, dll_load_sections(Data img, DLL_Loaded *loaded,
Data file, DLL_Data *dll){ Data file, DLL_Data *dll){
COFF_Header *coff_header; COFF_Header *coff_header;
PE_Section_Definition *section_def; PE_Section_Definition *section_def;
u32 header_size; u32 header_size;
u32 size; u32 size;
u32 i; u32 i;
coff_header = dll->coff_header; coff_header = dll->coff_header;
section_def = dll->section_defs; section_def = dll->section_defs;
header_size = header_size =
(u32)((byte*)(section_def + coff_header->number_of_sections) - file.data); (u32)((byte*)(section_def + coff_header->number_of_sections) - file.data);
memcpy(img.data, file.data, header_size); memcpy(img.data, file.data, header_size);
memset(img.data + header_size, 0, img.size - header_size); memset(img.data + header_size, 0, img.size - header_size);
for (i = 0; i < coff_header->number_of_sections; ++i, ++section_def){ for (i = 0; i < coff_header->number_of_sections; ++i, ++section_def){
size = section_def->loaded_size; size = section_def->loaded_size;
if (size > section_def->disk_size) if (size > section_def->disk_size)
size = section_def->disk_size; size = section_def->disk_size;
memcpy(img.data + section_def->loaded_location, memcpy(img.data + section_def->loaded_location,
file.data + section_def->disk_location, file.data + section_def->disk_location,
size); size);
if (dll_compare(section_def->name, ".text", 5) == 0){ if (dll_compare(section_def->name, ".text", 5) == 0){
loaded->text_start = section_def->loaded_location; loaded->text_start = section_def->loaded_location;
loaded->text_size = section_def->loaded_size; loaded->text_size = section_def->loaded_size;
} }
} }
return(1); return(1);
} }
void void
dll_load(Data img, DLL_Loaded *loaded, Data file, DLL_Data *dll){ dll_load(Data img, DLL_Loaded *loaded, Data file, DLL_Data *dll){
Data_Directory *export_dir; Data_Directory *export_dir;
dll_load_sections(img, loaded, file, dll); dll_load_sections(img, loaded, file, dll);
loaded->img = img; loaded->img = img;
loaded->dos_header = (DOS_Header*)((byte*)img.data + ((byte*)dll->dos_header - file.data)); loaded->dos_header = (DOS_Header*)((byte*)img.data + ((byte*)dll->dos_header - file.data));
loaded->coff_header = (COFF_Header*)((byte*)img.data + ((byte*)dll->coff_header - file.data)); loaded->coff_header = (COFF_Header*)((byte*)img.data + ((byte*)dll->coff_header - file.data));
loaded->opt_header_32 = (PE_Opt_Header_32Bit*) loaded->opt_header_32 = (PE_Opt_Header_32Bit*)
((byte*)img.data + ((byte*)dll->opt_header_32 - file.data)); ((byte*)img.data + ((byte*)dll->opt_header_32 - file.data));
loaded->opt_header_64 = (PE_Opt_Header_64Bit*) loaded->opt_header_64 = (PE_Opt_Header_64Bit*)
((byte*)img.data + ((byte*)dll->opt_header_64 - file.data)); ((byte*)img.data + ((byte*)dll->opt_header_64 - file.data));
loaded->section_defs = (PE_Section_Definition*) loaded->section_defs = (PE_Section_Definition*)
((byte*)img.data + ((byte*)dll->section_defs - file.data)); ((byte*)img.data + ((byte*)dll->section_defs - file.data));
loaded->is_64bit = dll->is_64bit; loaded->is_64bit = dll->is_64bit;
if (dll->is_64bit){ if (dll->is_64bit){
export_dir = dll->opt_header_64->data_directory; export_dir = dll->opt_header_64->data_directory;
} }
else{ else{
export_dir = dll->opt_header_32->data_directory; export_dir = dll->opt_header_32->data_directory;
} }
export_dir += image_dir_entry_export; export_dir += image_dir_entry_export;
loaded->export_start = export_dir->virtual_address; loaded->export_start = export_dir->virtual_address;
dll_perform_reloc(loaded); dll_perform_reloc(loaded);
} }
void* void*
dll_load_function(DLL_Loaded *dll, char *func_name, i32 size){ dll_load_function(DLL_Loaded *dll, char *func_name, i32 size){
Data img; Data img;
DLL_Export_Directory_Table *export_dir; DLL_Export_Directory_Table *export_dir;
DLL_Export_Address *address_ptr; DLL_Export_Address *address_ptr;
DLL_Export_Name *name_ptr; DLL_Export_Name *name_ptr;
void *result; void *result;
u32 count, i; u32 count, i;
u32 result_offset; u32 result_offset;
u32 ordinal; u32 ordinal;
img = dll->img; img = dll->img;
export_dir = (DLL_Export_Directory_Table*)(img.data + dll->export_start); export_dir = (DLL_Export_Directory_Table*)(img.data + dll->export_start);
count = export_dir->number_of_name_pointers; count = export_dir->number_of_name_pointers;
name_ptr = (DLL_Export_Name*)(img.data + export_dir->name_pointer_offset); name_ptr = (DLL_Export_Name*)(img.data + export_dir->name_pointer_offset);
result = 0; result = 0;
for (i = 0; i < count; ++i, ++name_ptr){ for (i = 0; i < count; ++i, ++name_ptr){
if (dll_compare((char*)img.data + name_ptr->name_offset, if (dll_compare((char*)img.data + name_ptr->name_offset,
func_name, size) == 0){ func_name, size) == 0){
ordinal = ((u16*)(img.data + export_dir->ordinal_offset))[i]; ordinal = ((u16*)(img.data + export_dir->ordinal_offset))[i];
#if 0 #if 0
// NOTE(allen): The MS docs say to do this, but // NOTE(allen): The MS docs say to do this, but
// it appears to just be downright incorrect. // it appears to just be downright incorrect.
ordinal -= export_dir->ordinal_base; ordinal -= export_dir->ordinal_base;
#endif #endif
address_ptr = (DLL_Export_Address*)(img.data + export_dir->address_offset); address_ptr = (DLL_Export_Address*)(img.data + export_dir->address_offset);
address_ptr += ordinal; address_ptr += ordinal;
result_offset = address_ptr->export_offset; result_offset = address_ptr->export_offset;
result = (img.data + result_offset); result = (img.data + result_offset);
break; break;
} }
} }
return(result); return(result);
} }
#define MachineCase(x) case x: result = #x; *len = sizeof(#x) - 1; break #define MachineCase(x) case x: result = #x; *len = sizeof(#x) - 1; break
char* char*
dll_machine_type_str(u16 machine, i32 *len){ dll_machine_type_str(u16 machine, i32 *len){
char *result; char *result;
i32 extra; i32 extra;
if (!len) len = &extra; if (!len) len = &extra;
result = 0; result = 0;
switch (machine){ switch (machine){
MachineCase(intel_i386); MachineCase(intel_i386);
MachineCase(intel_i860); MachineCase(intel_i860);
MachineCase(mips_r3000); MachineCase(mips_r3000);
MachineCase(mips_little_endian); MachineCase(mips_little_endian);
MachineCase(mips_r10000); MachineCase(mips_r10000);
MachineCase(old_alpha_axp); MachineCase(old_alpha_axp);
MachineCase(alpha_axp); MachineCase(alpha_axp);
MachineCase(hitachi_sh3); MachineCase(hitachi_sh3);
MachineCase(hitachi_sh3_dsp); MachineCase(hitachi_sh3_dsp);
MachineCase(hitachi_sh4); MachineCase(hitachi_sh4);
MachineCase(hitachi_sh5); MachineCase(hitachi_sh5);
MachineCase(arm_little_endian); MachineCase(arm_little_endian);
MachineCase(thumb); MachineCase(thumb);
MachineCase(matsushita_am33); MachineCase(matsushita_am33);
MachineCase(power_pc_little_endian); MachineCase(power_pc_little_endian);
MachineCase(power_pc_with_floating); MachineCase(power_pc_with_floating);
MachineCase(intel_ia64); MachineCase(intel_ia64);
MachineCase(mips16); MachineCase(mips16);
MachineCase(motorola_68000_series); MachineCase(motorola_68000_series);
MachineCase(alpha_axp_64_bit); MachineCase(alpha_axp_64_bit);
MachineCase(mips_with_fpu); MachineCase(mips_with_fpu);
MachineCase(mips16_with_fpu); MachineCase(mips16_with_fpu);
MachineCase(eft_byte_code); MachineCase(eft_byte_code);
MachineCase(amd_amd64); MachineCase(amd_amd64);
MachineCase(mitsubishi_m32r_little_endian); MachineCase(mitsubishi_m32r_little_endian);
MachineCase(clr_pure_msil); MachineCase(clr_pure_msil);
} }
return(result); return(result);
} }
#undef MachineCase #undef MachineCase
// BOTTOM // BOTTOM

View File

@ -10,23 +10,15 @@
// TOP // TOP
#include "4coder_default_bindings.cpp" #include "4coder_default_bindings.cpp"
#undef exec_command
#undef exec_command_keep_stack
#undef clear_parameters
#include "4ed_meta.h" #include "4ed_meta.h"
#define FCPP_FORBID_MALLOC
#include "4cpp_types.h"
#define FCPP_STRING_IMPLEMENTATION #define FCPP_STRING_IMPLEMENTATION
#include "4coder_string.h" #include "4coder_string.h"
#include "4ed_mem.cpp" #include "4ed_mem.cpp"
#include "4ed_math.cpp" #include "4ed_math.cpp"
#include <stdlib.h>
#include "4ed_system.h" #include "4ed_system.h"
#include "4ed_rendering.h" #include "4ed_rendering.h"
#include "4ed.h" #include "4ed.h"
@ -36,21 +28,10 @@
#include "system_shared.h" #include "system_shared.h"
#if FRED_INTERNAL
struct Sys_Bubble : public Bubble{
i32 line_number;
char *file_name;
};
#endif
#define FPS 60 #define FPS 60
#define frame_useconds (1000000 / FPS) #define frame_useconds (1000000 / FPS)
#define WM_4coder_SET_CURSOR (WM_USER + 1) #define WM_4coder_ANIMATE (WM_USER + 1)
#define WM_4coder_ANIMATE (WM_USER + 2)
#define WM_4coder_EVENT_COMPLETE (WM_USER + 3)
struct Thread_Context{ struct Thread_Context{
u32 job_id; u32 job_id;
@ -123,6 +104,13 @@ struct Win32_Coroutine{
int done; int done;
}; };
#if FRED_INTERNAL
struct Sys_Bubble : public Bubble{
i32 line_number;
char *file_name;
};
#endif
struct Win32_Vars{ struct Win32_Vars{
HWND window_handle; HWND window_handle;
HDC window_hdc; HDC window_hdc;
@ -194,7 +182,7 @@ INTERNAL_system_sentinel(){
internal void internal void
INTERNAL_system_debug_message(char *message){ INTERNAL_system_debug_message(char *message){
OutputDebugString(message); OutputDebugStringA(message);
} }
#endif #endif
@ -636,10 +624,19 @@ Sys_Release_Lock_Sig(system_release_lock){
internal void internal void
Win32SetCursorFromUpdate(Application_Mouse_Cursor cursor){ Win32SetCursorFromUpdate(Application_Mouse_Cursor cursor){
SendMessage( switch (cursor){
win32vars.window_handle, case APP_MOUSE_CURSOR_ARROW:
WM_4coder_SET_CURSOR, SetCursor(win32vars.cursor_arrow); break;
cursor, 0);
case APP_MOUSE_CURSOR_IBEAM:
SetCursor(win32vars.cursor_ibeam); break;
case APP_MOUSE_CURSOR_LEFTRIGHT:
SetCursor(win32vars.cursor_leftright); break;
case APP_MOUSE_CURSOR_UPDOWN:
SetCursor(win32vars.cursor_updown); break;
}
} }
internal void internal void
@ -722,7 +719,7 @@ JobThreadProc(LPVOID lpParameter){
} }
full_job->job.callback(win32vars.system, thread, thread_memory, full_job->job.callback(win32vars.system, thread, thread_memory,
&exchange_vars.thread, full_job->job.data); &exchange_vars.thread, full_job->job.data);
PostMessage(win32vars.window_handle, WM_4coder_EVENT_COMPLETE, 0, 0); PostMessage(win32vars.window_handle, WM_4coder_ANIMATE, 0, 0);
full_job->running_thread = 0; full_job->running_thread = 0;
thread->running = 0; thread->running = 0;
} }
@ -1085,12 +1082,12 @@ Win32LoadAppCode(){
} }
#else #else
Data file = system_load_file("4ed_app.dll"); File_Data file = system_load_file("4ed_app.dll");
if (file.data){ if (file.got_file){
i32 error; i32 error;
DLL_Data dll_data; DLL_Data dll_data;
if (dll_parse_headers(file, &dll_data, &error)){ if (dll_parse_headers(file.data, &dll_data, &error)){
Data img; Data img;
img.size = dll_total_loaded_size(&dll_data); img.size = dll_total_loaded_size(&dll_data);
img.data = (byte*) img.data = (byte*)
@ -1098,7 +1095,7 @@ Win32LoadAppCode(){
MEM_COMMIT | MEM_RESERVE, MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE); PAGE_READWRITE);
dll_load(img, &win32vars.app_dll, file, &dll_data); dll_load(img, &win32vars.app_dll, file.data, &dll_data);
DWORD extra_; DWORD extra_;
VirtualProtect(img.data + win32vars.app_dll.text_start, VirtualProtect(img.data + win32vars.app_dll.text_start,
@ -1113,9 +1110,7 @@ Win32LoadAppCode(){
// TODO(allen): file loading error // TODO(allen): file loading error
} }
system_free(file.data); Win32FreeMemory(file.data.data);
DUMP((byte*)(Tbytes(3)), Kbytes(400));
} }
else{ else{
// TODO(allen): file loading error // TODO(allen): file loading error
@ -1564,23 +1559,6 @@ Win32Callback(HWND hwnd, UINT uMsg,
EndPaint(hwnd, &ps); EndPaint(hwnd, &ps);
}break; }break;
case WM_4coder_SET_CURSOR:
{
switch (wParam){
case APP_MOUSE_CURSOR_ARROW:
SetCursor(win32vars.cursor_arrow); break;
case APP_MOUSE_CURSOR_IBEAM:
SetCursor(win32vars.cursor_ibeam); break;
case APP_MOUSE_CURSOR_LEFTRIGHT:
SetCursor(win32vars.cursor_leftright); break;
case APP_MOUSE_CURSOR_UPDOWN:
SetCursor(win32vars.cursor_updown); break;
}
}break;
case WM_CLOSE: // NOTE(allen): I expect WM_CLOSE not WM_DESTROY case WM_CLOSE: // NOTE(allen): I expect WM_CLOSE not WM_DESTROY
case WM_DESTROY: case WM_DESTROY:
{ {
@ -1589,7 +1567,6 @@ Win32Callback(HWND hwnd, UINT uMsg,
}break; }break;
case WM_4coder_ANIMATE: case WM_4coder_ANIMATE:
case WM_4coder_EVENT_COMPLETE:
win32vars.got_useful_event = 1; win32vars.got_useful_event = 1;
break; break;
@ -1713,7 +1690,7 @@ UpdateStep(){
else{ else{
file->flags |= FEx_Save_Failed; file->flags |= FEx_Save_Failed;
} }
PostMessage(win32vars.window_handle, WM_4coder_EVENT_COMPLETE, 0, 0); PostMessage(win32vars.window_handle, WM_4coder_ANIMATE, 0, 0);
} }
if (file->flags & FEx_Request){ if (file->flags & FEx_Request){
@ -1728,7 +1705,7 @@ UpdateStep(){
file->data = sysfile.data.data; file->data = sysfile.data.data;
file->size = sysfile.data.size; file->size = sysfile.data.size;
} }
PostMessage(win32vars.window_handle, WM_4coder_EVENT_COMPLETE, 0, 0); PostMessage(win32vars.window_handle, WM_4coder_ANIMATE, 0, 0);
} }
} }
@ -2044,7 +2021,7 @@ int main(int argc, char **argv){
} }
// TODO(allen): not Windows XP compatible, do we care? // TODO(allen): not Windows XP compatible, do we care?
SetProcessDPIAware(); // SetProcessDPIAware();
HWND window_handle = {}; HWND window_handle = {};
window_handle = CreateWindowA( window_handle = CreateWindowA(