compressing string using zlib

 

Hi,


iam trying to compress a string using the zlib library from zlib.net .


I discovered, that the function


compress2(Bytef * dest, uLongf * destLen, const Bytef * source, uLong sourceLen, int level)
is responsible for compressing the variable source to dest, but it is called a Bytef *. What is this for a DataType ? How can I use this function in MQL4 . Maybe a Example function, would be very helpful ... By the way, is this the optimal way to compress a String with the length of approx. 3000-5000 If there is a alternative, please provide a example code, because iam not very good in c++ or c ? Please could anybody help me ?
 

Bytef* is a pointer to a Bytef, whatever that is.

IIRC a MQL4 string is a struct{ int len, char* str}

Incompatible types. Either you will have to write a DLL to convert a passed string to the zlib, or alternatively, write the string to a file, compress the file and read the compressed file back in.

 

WHRoeder:

IIRC a MQL4 string is a struct{ int len, char* str}

Incompatible types.

Only in arrays of strings. If you pass a single string to a dll it will simply pass an ordinary pointer to the string and the string will be terminated by 0x00, very much like it is commonly done in C.


If you want to have the dll function copy data into a buffer that you provide then you have to create a string with dummy contents of the needed length and pass this string to the dll, the dll will receive a pointer to this dummy string and can write to the memory where the pointer points to.


If you have an array of strings in mql4 and pass this array to a dll function it will be a pointer to an array of the aforementioned structs.

 

i do not know how to do that. I am not eperienced in c .

Is there no possibility to use compress2 function in mq4 ?

like


#import "zlib1.dll"
    int compress2(...);
#import




int init()
{

  string compressed = compress2(... , uncompressed ) ;

}
 
 
ulukai:

i do not know how to do that. I am not eperienced in c .


You don't need to be experienced, you just need to learn the syntax and the concepts.

And btw: the function does not return string, it returns int.

The second parameter uLongf * destLen, probably the length of the destination buffer, is a little bit strange, I don't know why they want a pointer to an integer instead of just an integer, probably they smoked some funny substances when they designed it. To make this work from mql4 you must pass an array of int by reference with only one element here, this would then work like a pointer to an int, unfortunately you can't simply pass an int by reference due to a bug in mql4.

Also you must make sure the function compress2() is using the stdcall calling convention (study the documentation of zlib) and also make sure you don't miss any needed initialization of the library before you call this function. If its not stdcall you will have to write a wrapper because stdcall is the only convention you can use from mql4.

You should read the documentation about zlib and you should learn at least enough C to understand it! You don't have to be able write big programs in C but you should know what the syntax means and how the data is represented in memory and how a function call works to be able to interface with it. Learning does not cost any money nowadays, its all freely available, it only needs some dedication.


This thread now contains every mql4 specific information that is needed about how mql4 handles the data and how to pass it to functions (pointers to buffers, pointers to integers) which is not so obvious, now its up to you to do your homework and apply this. The needed knowledge about the C syntax is commonly available and public domain (you just have to absorb it) and zlib also comes with all needed documentation.

 

Read (and understand) answer 6 and 7 from the zlib FAQ:

http://www.zlib.net/DLL_FAQ.txt


zlib is CDECL and not STDCALL, you will either need to write a wrapper or find the STDCALL build of the dll that is mentioned in answer 7.

Reason: