Skip to content

Commit

Permalink
Merge pull request #4 from justinfrankel/master
Browse files Browse the repository at this point in the history
Sync with justinfrankel/ninjam
  • Loading branch information
zeppelinux authored Nov 9, 2020
2 parents fbb5524 + c7dec4d commit 20ad3f0
Show file tree
Hide file tree
Showing 88 changed files with 6,770 additions and 1,879 deletions.
40 changes: 29 additions & 11 deletions WDL/assocarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,18 @@ template <class KEY, class VAL> class WDL_AssocArrayImpl
if (m_data.GetSize() > 1 && m_keycmp)
{
char *tmp=(char*)malloc(m_data.GetSize()*sizeof(KeyVal));
WDL_mergesort(m_data.Get(), m_data.GetSize(), sizeof(KeyVal),
(int(*)(const void*, const void*))m_keycmp, tmp);
free(tmp);
if (WDL_NORMALLY(tmp))
{
WDL_mergesort(m_data.Get(), m_data.GetSize(), sizeof(KeyVal),
(int(*)(const void*, const void*))m_keycmp, tmp);
free(tmp);
}
else
{
qsort(m_data.Get(), m_data.GetSize(), sizeof(KeyVal),
(int(*)(const void*, const void*))m_keycmp);
}

RemoveDuplicateKeys();
}
}
Expand Down Expand Up @@ -254,6 +263,7 @@ template <class KEY, class VAL> class WDL_AssocArrayImpl
void CopyContentsAsReference(const WDL_AssocArrayImpl &cp)
{
DeleteAll(true);
m_keycmp = cp.m_keycmp;
m_keydup = NULL; // this no longer can own any data
m_keydispose = NULL;
m_valdispose = NULL;
Expand All @@ -279,18 +289,26 @@ template <class KEY, class VAL> class WDL_AssocArrayImpl

void RemoveDuplicateKeys() // after resorting
{
// will be expensive if there are a lot of duplicates,
// in which case use m_data.DeleteBatch()
int i;
for (i=0; i < m_data.GetSize()-1; ++i)
const int sz = m_data.GetSize();

int cnt = 1;
KeyVal *rd = m_data.Get() + 1, *wr = rd;
for (int x = 1; x < sz; x ++)
{
KeyVal* kv=m_data.Get()+i;
KeyVal* nv=kv+1;
if (!m_keycmp(&kv->key, &nv->key))
if (m_keycmp(&rd->key, &wr[-1].key))
{
if (rd != wr) *wr=*rd;
wr++;
cnt++;
}
else
{
DeleteByIndex(i--);
if (m_keydispose) m_keydispose(rd->key);
if (m_valdispose) m_valdispose(rd->val);
}
rd++;
}
if (cnt < sz) m_data.Resize(cnt,false);
}
};

Expand Down
48 changes: 26 additions & 22 deletions WDL/dirscan.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
extern struct stat wdl_stat_chk;
// if this fails on linux, use CFLAGS += -D_FILE_OFFSET_BITS=64
typedef char wdl_dirscan_assert_failed_stat_not_64[sizeof(wdl_stat_chk.st_size)!=8 ? -1 : 1];
#endif

class WDL_DirScan
Expand Down Expand Up @@ -190,29 +193,30 @@ class WDL_DirScan
#ifdef _WIN32
return !!(m_fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
#else
#ifndef __APPLE__
// we could enable this on OSX, need to check to make sure realpath(x,NULL) is supported on 10.5+
char tmp[2048];
if (m_ent && m_ent->d_type == DT_LNK)
if (m_ent) switch (m_ent->d_type)
{
snprintf(tmp,sizeof(tmp),"%s/%s",m_leading_path.Get(),m_ent->d_name);
char *rp = realpath(tmp,NULL);
if (rp)
case DT_DIR: return 1;
case DT_LNK:
{
DIR *d = opendir(rp);
free(rp);
snprintf(tmp,sizeof(tmp),"%s/%s",m_leading_path.Get(),m_ent->d_name);
char *rp = realpath(tmp,NULL);
if (!rp) return 0;

struct stat sb;
int ret = !stat(rp,&sb) && (sb.st_mode & S_IFMT) == S_IFDIR;
free(rp);
return ret;
}
case DT_UNKNOWN:
{
snprintf(tmp,sizeof(tmp),"%s/%s",m_leading_path.Get(),m_ent->d_name);
DIR *d = opendir(tmp);
if (d) { closedir(d); return 1; }
return 0;
}
}
else if (m_ent && m_ent->d_type == DT_UNKNOWN)
{
snprintf(tmp,sizeof(tmp),"%s/%s",m_leading_path.Get(),m_ent->d_name);
DIR *d = opendir(tmp);
if (d) { closedir(d); return 1; }
}
#endif
return m_ent && (m_ent->d_type == DT_DIR);
return 0;
#endif
}

Expand All @@ -229,8 +233,8 @@ class WDL_DirScan
{
char tmp[2048];
snprintf(tmp,sizeof(tmp),"%s/%s",m_leading_path.Get(),GetCurrentFN());
struct stat64 st={0,};
stat64(tmp,&st);
struct stat st={0,};
stat(tmp,&st);
unsigned long long a=(unsigned long long)st.st_ctime; // seconds since january 1st, 1970
a+=11644473600ull; // 1601->1970
a*=10000000; // seconds to 1/10th microseconds (100 nanoseconds)
Expand All @@ -242,8 +246,8 @@ class WDL_DirScan
{
char tmp[2048];
snprintf(tmp,sizeof(tmp),"%s/%s",m_leading_path.Get(),GetCurrentFN());
struct stat64 st={0,};
stat64(tmp,&st);
struct stat st={0,};
stat(tmp,&st);
unsigned long long a=(unsigned long long)st.st_mtime; // seconds since january 1st, 1970
a+=11644473600ull; // 1601->1970
a*=10000000; // seconds to 1/10th microseconds (100 nanoseconds)
Expand All @@ -254,8 +258,8 @@ class WDL_DirScan
{
char tmp[2048];
snprintf(tmp,sizeof(tmp),"%s/%s",m_leading_path.Get(),GetCurrentFN());
struct stat64 st={0,};
stat64(tmp,&st);
struct stat st={0,};
stat(tmp,&st);

if (HighWord) *HighWord = (DWORD)(st.st_size>>32);
return (DWORD)(st.st_size&0xffffffff);
Expand Down
19 changes: 17 additions & 2 deletions WDL/eel2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ LFLAGS=
CXX=g++

ifdef DEBUG
CFLAGS += -D_DEBUG -O0
CFLAGS += -D_DEBUG -O0 -DWDL_CHECK_FOR_NON_UTF8_FOPEN
else
CFLAGS += -DNDEBUG -O
endif

CFLAGS += -D_FILE_OFFSET_BITS=64

OBJS=nseel-caltab.o nseel-compiler.o nseel-eval.o nseel-lextab.o nseel-ram.o nseel-yylex.o nseel-cfunc.o fft.o

OBJS2=

UNAME_S := $(shell uname -s)
ARCH := $(shell uname -m)

ifeq ($(ARCH), aarch64)
ifeq ($(shell $(CC) -dumpmachine | cut -f 1 -d -), arm)
# helper for armv7l userspace on aarch64 cpu
ARCH := armv7l
endif
endif

ifeq ($(UNAME_S),Darwin)
CFLAGS += -arch $(ARCH)
endif
Expand Down Expand Up @@ -63,7 +72,13 @@ ifndef NO_GFX
CFLAGS += -DEEL_LICE_WANT_STANDALONE

ifeq ($(UNAME_S),Darwin)
CFLAGS += -mmacosx-version-min=10.5
CLANG_VER := $(shell clang --version|head -n 1| sed 's/.*version \([0-9][0-9]*\).*/\1/' )
CLANG_GT_9 := $(shell [ $(CLANG_VER) -gt 9 ] && echo true )
ifeq ($(CLANG_GT_9),true)
CFLAGS += -mmacosx-version-min=10.7 -stdlib=libc++
else
CFLAGS += -mmacosx-version-min=10.5
endif
OBJS += swell-wnd.o swell-gdi.o swell.o swell-misc.o swell-dlg.o swell-menu.o swell-kb.o
LFLAGS += -lobjc -framework Cocoa -framework Carbon
else
Expand Down
5 changes: 5 additions & 0 deletions WDL/eel2/eel_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ void *(*NSEEL_PProc_THIS)(void *data, int data_size, struct _compileContext *ctx
void (*eel_setfp_round)();
void (*eel_setfp_trunc)();

void (*eel_enterfp)(int s[2]);
void (*eel_leavefp)(int s[2]);


eel_function_table g_eel_function_table;
#define NSEEL_ADDFUNC_DESTINATION (&g_eel_function_table)
Expand Down Expand Up @@ -94,6 +97,8 @@ class eel_string_context_state;
IMPORT_FUNC(nseel_int_register_var) \
IMPORT_FUNC(eel_setfp_round) \
IMPORT_FUNC(eel_setfp_trunc) \
IMPORT_FUNC(eel_leavefp) \
IMPORT_FUNC(eel_enterfp) \
IMPORT_FUNC(NSEEL_VM_set_var_resolver) \
IMPORT_FUNC(NSEEL_VM_alloc) /* keep NSEEL_VM_alloc last */

Expand Down
12 changes: 7 additions & 5 deletions WDL/eel2/eel_lice.h
Original file line number Diff line number Diff line change
Expand Up @@ -1025,8 +1025,8 @@ EEL_F eel_lice_state::gfx_setimgdim(int img, EEL_F *w, EEL_F *h)
int use_w = (int)*w;
int use_h = (int)*h;
if (use_w<1 || use_h < 1) use_w=use_h=0;
if (use_w > 2048) use_w=2048;
if (use_h > 2048) use_h=2048;
if (use_w > 8192) use_w=8192;
if (use_h > 8192) use_h=8192;

LICE_IBitmap *bm=NULL;
if (img >= 0 && img < m_gfx_images.GetSize())
Expand Down Expand Up @@ -2117,7 +2117,9 @@ static LRESULT WINAPI eel_lice_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPAR
extern "C"
{
void *objc_getClass(const char *p);
#ifndef _OBJC_OBJC_H_
void *sel_getUid(const char *p);
#endif
void objc_msgSend(void);
};
#endif
Expand Down Expand Up @@ -2209,9 +2211,9 @@ static EEL_F NSEEL_CGEN_CALL _gfx_init(void *opaque, INT_PTR np, EEL_F **parms)
#endif

if (sug_w < 16) sug_w=16;
else if (sug_w > 2048) sug_w=2048;
else if (sug_w > 8192) sug_w=8192;
if (sug_h < 16) sug_h=16;
else if (sug_h > 1600) sug_h=1600;
else if (sug_h > 8192) sug_h=8192;

if (!ctx->hwnd_standalone)
{
Expand Down Expand Up @@ -2927,7 +2929,7 @@ static const char *eel_lice_function_reference =
"srcx/srcy/srcw/srch specify the source rectangle (if omitted srcw/srch default to image size), destx/desty/destw/desth specify dest rectangle (if not specified, these will default to reasonable defaults -- destw/desth default to srcw/srch * scale). \0"
"gfx_blitext\tsource,coordinatelist,rotation\tDeprecated, use gfx_blit instead.\0"
"gfx_getimgdim\timage,w,h\tRetreives the dimensions of image (representing a filename: index number) into w and h. Sets these values to 0 if an image failed loading (or if the filename index is invalid).\0"
"gfx_setimgdim\timage,w,h\tResize image referenced by index 0.." EEL_LICE_DOC_MAXHANDLE ", width and height must be 0-2048. The contents of the image will be undefined after the resize.\0"
"gfx_setimgdim\timage,w,h\tResize image referenced by index 0.." EEL_LICE_DOC_MAXHANDLE ", width and height must be 0-8192. The contents of the image will be undefined after the resize.\0"
"gfx_loadimg\timage,\"filename\"\tLoad image from filename into slot 0.." EEL_LICE_DOC_MAXHANDLE " specified by image. Returns the image index if success, otherwise -1 if failure. The image will be resized to the dimensions of the image file. \0"
"gfx_gradrect\tx,y,w,h, r,g,b,a[, drdx, dgdx, dbdx, dadx, drdy, dgdy, dbdy, dady]\tFills a gradient rectangle with the color and alpha specified. drdx-dadx reflect the adjustment (per-pixel) applied for each pixel moved to the right, drdy-dady are the adjustment applied for each pixel moved toward the bottom. Normally drdx=adjustamount/w, drdy=adjustamount/h, etc.\0"
"gfx_muladdrect\tx,y,w,h,mul_r,mul_g,mul_b[,mul_a,add_r,add_g,add_b,add_a]\tMultiplies each pixel by mul_* and adds add_*, and updates in-place. Useful for changing brightness/contrast, or other effects.\0"
Expand Down
15 changes: 15 additions & 0 deletions WDL/eel2/eel_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,22 @@ int eel_format_strings(void *opaque, const char *fmt, const char *fmt_end, char
*op=0;
}
else
{
#if !defined(_WIN32) && !defined(__arm__) && !defined(__aarch64__)
// x86 and x86_64 set rounding to truncate (ugh)
// apparently on Windows it doesn't matter for sprintf(), though.
// this is safe to call on other platforms, too, just perhaps wasteful
int fpstate[2];
eel_enterfp(fpstate);
eel_setfp_round();
#endif

snprintf(op,64,fs,v);

#if !defined(_WIN32) && !defined(__arm__) && !defined(__aarch64__)
eel_leavefp(fpstate);
#endif
}
}

while (*op) op++;
Expand Down
9 changes: 5 additions & 4 deletions WDL/eel2/eelscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "../wdlstring.h"
#include "../assocarray.h"
#include "../queue.h"
#include "../win32_utf8.h"
#include "ns-eel.h"


Expand Down Expand Up @@ -94,7 +95,7 @@ class eelScriptInst {
for (x=0;x<EELSCRIPT_MAX_FILE_HANDLES && m_handles[x];x++);
if (x>= EELSCRIPT_MAX_FILE_HANDLES) return 0.0;

FILE *fp = fopen(fnstr.Get(),mode);
FILE *fp = fopenUTF8(fnstr.Get(),mode);
if (!fp) return 0.0;
m_handles[x]=fp;
return x + EELSCRIPT_FILE_HANDLE_INDEX_BASE;
Expand Down Expand Up @@ -408,7 +409,7 @@ int eelScriptInst::runcode(const char *codeptr, int showerr, const char *showerr
#else
lstrcpyn_safe(buf,"/tmp/jsfx-out",sizeof(buf));
#endif
FILE *fp = fopen(buf,"wb");
FILE *fp = fopenUTF8(buf,"wb");
if (fp)
{
fwrite(p->code,1,p->code_size,fp);
Expand Down Expand Up @@ -501,7 +502,7 @@ FILE *eelscript_resolvePath(WDL_FastString &usefn, const char *fn, const char *c
}
}

FILE *fp = fopen(usefn.Get(),"r");
FILE *fp = fopenUTF8(usefn.Get(),"r");
if (fp) return fp;
}
if (had_abs) usefn.Set(fn);
Expand Down Expand Up @@ -529,7 +530,7 @@ int eelScriptInst::loadfile(const char *fn, const char *callerfn, bool allowstdi
}
else if (!callerfn)
{
fp = fopen(fn,"r");
fp = fopenUTF8(fn,"r");
if (fp) m_loaded_fnlist.Insert(fn,true);
}
else
Expand Down
4 changes: 2 additions & 2 deletions WDL/eel2/makefile.vc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CFLAGS = /DWDL_FFT_REALSIZE=8 /DEEL_LICE_WANT_STANDALONE

CFLAGS = $(CFLAGS) -DNSEEL_LOOPFUNC_SUPPORT_MAXLEN=0

LFLAGS =
LFLAGS = shell32.lib user32.lib comdlg32.lib

!ifndef VC6
CFLAGS = $(CFLAGS) /MT
Expand All @@ -35,7 +35,7 @@ loose_eel.cpp: eel*.h ns-eel*.h
$(EEL_SOURCE): ns-eel*.h


loose_eel.exe: loose_eel.cpp $(EEL_SOURCE) $(FFT_SOURCE) $(LICE_SOURCE)
loose_eel.exe: loose_eel.cpp $(EEL_SOURCE) $(FFT_SOURCE) $(LICE_SOURCE) ..\win32_utf8.c
cl $(CFLAGS) $** /link wsock32.lib user32.lib gdi32.lib advapi32.lib $(LFLAGS) /out:$@

eel_disasm.exe: eel_disasm.cpp $(EEL_SOURCE)
Expand Down
3 changes: 2 additions & 1 deletion WDL/eel2/nseel-compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
#include "ns-eel-int.h"

#include "../denormal.h"
#include "../wdlcstring.h"

#include <string.h>
#include <math.h>
#include <stdio.h>
#include <ctype.h>

#include "../wdlcstring.h"

#if !defined(EEL_TARGET_PORTABLE) && !defined(_WIN32)
#include <sys/mman.h>
#include <stdint.h>
Expand Down
8 changes: 1 addition & 7 deletions WDL/filebrowse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,6 @@ bool WDL_ChooseDirectory(HWND parent, const char *text, const char *initialdir,
#endif
}

static WDL_STATICFUNC_UNUSED const char *stristr(const char* a, const char* b)
{
const size_t n = strlen(a), len = strlen(b);
for (size_t i = 0; i+len <= n; ++i) if (!strnicmp(a+i, b, len)) return a+i;
return NULL;
}

#ifdef _WIN32
struct WDL_FileBrowse_Dis {
Expand Down Expand Up @@ -343,7 +337,7 @@ char *WDL_ChooseFileForOpen2(HWND parent,
{
if(*p) p+=strlen(p)+1;
if(!*p) break;
if(stristr(p, defext))
if(WDL_stristr(p, defext))
{
fd.setFileTypeIndex(i+1);
break;
Expand Down
Loading

0 comments on commit 20ad3f0

Please sign in to comment.