• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Snippet playing with cpp a bit

Holzchopf

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jul 31, 2017
Messages
500
Out of curiosity I tried to play around with cpp. I had this function ftime in BMax, that used standard c functions time, localtime and strftime to format the local time into a string - and after many tries now I got it running in Cerberus! Why? For fun, of course. And also to learn about the capabilities of CX ;)

ftime.cxs

Code:
' ftime.cxs
' compiles as STDCPP

Strict

Import "ftime.cpp"

Extern
Function malloc_:Int( size:Int )            ' allocate memory block
Function peekchar_:Int( mem:Int, pos:Int )    ' read char from memory block
Function time_:Void( time:Int )                ' get current system time
Function localtime_:Int( time:Int )            ' translate time to tm struct
Function strftime_:Void( buf:Int, size:Int, fmt$, time:Int ) ' format time into
                                            ' string in buf
Public


Function Main:Int()
    Print( ftime("%A, %Y-%m-%d %H:%M:%S") )
    Return 0
End

' returns current time in the format defined in pFString
Function ftime:String( pFString:String )
    Local time:Int = malloc_(64)            ' memory for time
    Local timestring:Int = malloc_(256)        ' buffer for string
 
    time_(time)                                ' get current system time
    Local tm:Int = localtime_(time)            ' translate to tm struct
    strftime_(timestring, 256, pFString, tm)    ' format into string
 
    ' make int array from buffered chars
    Local ret:Int[256]
    For Local i:Int = 0 Until 256
        ret[i] = peekchar_(timestring, i)
    Next
 
    ' return string from int array
    Return String.FromChars( ret )
End Function

and ftime.cpp

C++:
// wrapper function for: time_t time (time_t* timer);
// except it doesn't return anything
void time_( long long int ttime ){
    time( (time_t*)ttime );
}

// wrapper function for: struct tm * localtime (const time_t * timer);
// returns the address of the tm structure as cerberus compatible int
int localtime_( long long int ttime ){
    return (long long int)localtime( (time_t*)ttime );
}

// wrapper function for: size_t strftime (char* ptr, size_t maxsize,
//        const char* format, const struct tm* timeptr );
int strftime_( long long int buf, int size, String fmt, long long int ttime ){
    return strftime( (char*)buf,size,fmt.ToCString<char>(), (tm*)ttime );
}

// wrapper function for: void* malloc (size_t size);
// returns the address of the allocated block as a cerberus compatible int
int malloc_( int size ){
    return (long long int)malloc( size );
}

// reads a char from a block at a defined position
// returns the char as a cerberus compatible int
int peekchar_( long long int mem, int pos ){
    char* pmem = (char*)mem;
    return (int)pmem[pos];
}
 
Last edited by a moderator:
Back
Top Bottom