Here's a simple little program I wrote, which uses the ZLIB library to do compression for us:
CompSize = compressBound(FileSize)
Result
= compress2
(return$
, CompSize
, text$
, FileSize
, 9)
Simple enough. Pass FUNCTION deflate a string, it passes you back a compressed string...
Here's my problem -- the c-output that we get from this code:
qbs* FUNC_DEFLATE(qbs*_FUNC_DEFLATE_STRING_TEXT){
qbs *tqbs;
ptrszint tmp_long;
int32 tmp_fileno;
uint32 qbs_tmp_base=qbs_tmp_list_nexti;
uint8 *tmp_mem_static_pointer=mem_static_pointer;
uint32 tmp_cmem_sp=cmem_sp;
qbs *_FUNC_DEFLATE_STRING_DEFLATE=NULL;
if (!_FUNC_DEFLATE_STRING_DEFLATE)_FUNC_DEFLATE_STRING_DEFLATE=qbs_new(0,0);
qbs*oldstr1=NULL;
if(_FUNC_DEFLATE_STRING_TEXT->tmp||_FUNC_DEFLATE_STRING_TEXT->fixed||_FUNC_DEFLATE_STRING_TEXT->readonly){
oldstr1=_FUNC_DEFLATE_STRING_TEXT;
if (oldstr1->cmem_descriptor){
_FUNC_DEFLATE_STRING_TEXT=qbs_new_cmem(oldstr1->len,0);
}else{
_FUNC_DEFLATE_STRING_TEXT=qbs_new(oldstr1->len,0);
}
memcpy(_FUNC_DEFLATE_STRING_TEXT->chr,oldstr1->chr,oldstr1->len);
}
int32 *_FUNC_DEFLATE_LONG_FILESIZE=NULL;
if(_FUNC_DEFLATE_LONG_FILESIZE==NULL){
_FUNC_DEFLATE_LONG_FILESIZE=(int32*)mem_static_malloc(4);
*_FUNC_DEFLATE_LONG_FILESIZE=0;
}
int32 *_FUNC_DEFLATE_LONG_COMPSIZE=NULL;
if(_FUNC_DEFLATE_LONG_COMPSIZE==NULL){
_FUNC_DEFLATE_LONG_COMPSIZE=(int32*)mem_static_malloc(4);
*_FUNC_DEFLATE_LONG_COMPSIZE=0;
}
qbs *_FUNC_DEFLATE_STRING_RETURN=NULL;
if (!_FUNC_DEFLATE_STRING_RETURN)_FUNC_DEFLATE_STRING_RETURN=qbs_new(0,0);
byte_element_struct *byte_element_2=NULL;
if (!byte_element_2){
if ((mem_static_pointer+=12)<mem_static_limit) byte_element_2=(byte_element_struct*)(mem_static_pointer-12); else byte_element_2=(byte_element_struct*)mem_static_malloc(12);
}
float *_FUNC_DEFLATE_SINGLE_RESULT=NULL;
if(_FUNC_DEFLATE_SINGLE_RESULT==NULL){
_FUNC_DEFLATE_SINGLE_RESULT=(float*)mem_static_malloc(4);
*_FUNC_DEFLATE_SINGLE_RESULT=0;
}
mem_lock *sf_mem_lock;
new_mem_lock();
sf_mem_lock=mem_lock_tmp;
sf_mem_lock->type=3;
if (new_error) goto exit_subfunc;
*_FUNC_DEFLATE_LONG_FILESIZE=_FUNC_DEFLATE_STRING_TEXT->len;
qbs_cleanup(qbs_tmp_base,0);
*_FUNC_DEFLATE_LONG_COMPSIZE=( int32 )compressBound(*_FUNC_DEFLATE_LONG_FILESIZE);
qbs_set(_FUNC_DEFLATE_STRING_RETURN,func_space(*_FUNC_DEFLATE_LONG_COMPSIZE));
qbs_cleanup(qbs_tmp_base,0);
*_FUNC_DEFLATE_SINGLE_RESULT=( int32 )compress2((char*)(_FUNC_DEFLATE_STRING_RETURN)->chr,_FUNC_DEFLATE_LONG_COMPSIZE,(char*)(_FUNC_DEFLATE_STRING_TEXT)->chr,*_FUNC_DEFLATE_LONG_FILESIZE, 9 );
qbs_cleanup(qbs_tmp_base,0);
qbs_set(_FUNC_DEFLATE_STRING_DEFLATE,qbs_left(_FUNC_DEFLATE_STRING_RETURN,*_FUNC_DEFLATE_LONG_COMPSIZE));
qbs_cleanup(qbs_tmp_base,0);
exit_subfunc:;
free_mem_lock(sf_mem_lock);
if(oldstr1){
if(oldstr1->fixed)qbs_set(oldstr1,_FUNC_DEFLATE_STRING_TEXT);
qbs_free(_FUNC_DEFLATE_STRING_TEXT);
}
qbs_free(_FUNC_DEFLATE_STRING_RETURN);
if ((tmp_mem_static_pointer>=mem_static)&&(tmp_mem_static_pointer<=mem_static_limit)) mem_static_pointer=tmp_mem_static_pointer; else mem_static_pointer=mem_static;
cmem_sp=tmp_cmem_sp;
qbs_maketmp(_FUNC_DEFLATE_STRING_DEFLATE);return _FUNC_DEFLATE_STRING_DEFLATE;
}
Our 6 lines of code end up turning into 66 lines of mind-boggling gibberish!!
So my question here is: Does anybody know a nice, clean way to turn that small function into C-code?
QB64 has always boggled my poor brain with how it handles strings. We don't use strings like they have in C... We have our own custom little type called *qbs (quick basic string), complete with data structures and all sorts of gunk which I get lost in. Converting QB64 numbers to C numbers doesn't give me any issue, but sorting out how the hell to make a QB64 string work with the things I might want it to in C just leaves me frustrated and scratching my head, as I drool incessantly while trying out one failure after another, looking for the right thing to stick and work!
And why do I need such a SUB, you ask?
I've been playing around with my Repo, and I've now got ZLIB merged into QB64 with it an available library which we can pull from. You don't need any sort of dll file in your folder for it to work anymore (like with the SaveImage library) -- it's already in QB64 and ready for us to make use of it. (Only my personal version at the moment, as I'm still testing things out.)
Once I can get a simple little function wrote up to _DEFLATE and _ENFLATE, I can add those to QB64 itself and we'll have compression tools which we can use forever more with the language.
What compress2 is actually looking for is:
int compress2(Bytef * dest, uLongf * destLen, const Bytef * source, uLong sourceLen, int level);
Which is the following 5 things:
Offset to the compressed data (or string in this case)
offset to the length of the compressed data
Offset to the raw data which we want to compress
the length of the raw data which we're compressing
And the value for the compression level (9 is max compression)
Any ideas on how to turn this into working C, which I can plug into libqb.cpp for use:
CompSize = compressBound(FileSize)
Result
= compress2
(return$
, CompSize
, text$
, FileSize
, 9)
There's got to be something better than the auto-translated gook which QB64 kicks out for it!
Any help would be appreciated.