libulz - a collection of useful functions and data structures in C

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
Locked
j8w344c6
Posts: 184
Joined: Oct 25, 2021 10:18

libulz - a collection of useful functions and data structures in C

Post by j8w344c6 »

Home page: https://github.com/rofl0r/libulz

Example:

format.bi

Code: Select all

#pragma once

#include once "unistd.bi"
#include once "crt/stddef.bi"
#include once "crt/stdint.bi"
#include once "crt/stdarg.bi"

extern "C"

declare sub ulz_printf(byval fmt as const zstring ptr, ...)
declare sub ulz_fprintf(byval fd as long, byval fmt as const zstring ptr, ...)
declare sub ulz_dprintf alias "ulz_fprintf"(byval fd as long, byval fmt as const zstring ptr, ...)
declare function ulz_snprintf(byval dest as zstring ptr, byval destsize as uinteger, byval fmt as const zstring ptr, ...) as integer
declare function ulz_vsnprintf(byval dest as zstring ptr, byval destsize as uinteger, byval format_ as const zstring ptr, byval ap as va_list) as integer

end extern
strlib.bi

Generated with: fbfrog strlib.h -rename_ str -rename_ len -rename_ hex -rename_ out -rename_ base

Code: Select all

#pragma once

#include once "crt/stddef.bi"
#include once "crt/stdint.bi"
#include once "crt/time.bi"
#include once "unistd.bi"
#include once "crt/string.bi"

extern "C"

type numberToString_flags as long
enum
	NTS_NONE = 0
	NTS_PAD = 1
	NTS_SIGNED_TYPE = 2
	NTS_LOWERCASE_CHARS = 4
end enum

declare function containsChar(byval str_ as zstring ptr, byval what as byte) as long
declare function hexval(byval i as zstring ptr) as long
declare sub raw2hex(byval dst as zstring ptr, byval src as const ubyte ptr, byval len_ as uinteger)
declare function hex2raw(byval hex_ as const zstring ptr, byval out_ as ubyte ptr) as uinteger
declare function isAlpha(byval i as zstring ptr) as long
declare function isNumber(byval i as zstring ptr) as long
declare function isLetter(byval i as zstring ptr) as long
declare function isUpper(byval i as zstring ptr) as long
declare function isLower(byval i as zstring ptr) as long
declare sub makelower(byval i as zstring ptr)
declare sub makeupper(byval i as zstring ptr)
declare function itoa(byval number as long, byval buffer as zstring ptr) as zstring ptr
declare function utoa(byval number as ulong, byval buffer as zstring ptr) as zstring ptr
declare function numberToString(byval number as ulongint, byval base_ as ulong, byval buffer as zstring ptr, byval maxlen as uinteger, byval flags as long) as zstring ptr
declare function intToString(byval number as long, byval buffer as zstring ptr) as zstring ptr
declare function uintToString(byval number as ulong, byval buffer as zstring ptr) as zstring ptr
declare function int64ToString(byval number as longint, byval buffer as zstring ptr) as zstring ptr
declare function uint64ToString(byval number as ulongint, byval buffer as zstring ptr) as zstring ptr
declare function strtoint(byval str_ as zstring ptr, byval len_ as uinteger) as long
declare function strtoint64(byval str_ as zstring ptr, byval len_ as uinteger) as longint
declare function strdup_n(byval str_ as zstring ptr, byval len_ as uinteger) as zstring ptr
declare function strstr_uc(byval haystack as zstring ptr, byval needle as zstring ptr, byval needlesize as uinteger) as zstring ptr
declare function strstar(byval haystack as const zstring ptr, byval needle as const zstring ptr, byval needlesize as uinteger) as zstring ptr
declare function findword(byval buf as zstring ptr, byval word_uc as zstring ptr, byval word_len as uinteger) as zstring ptr
declare function ipv4fromstring(byval ipstring as zstring ptr, byval fourbytesptr as ubyte ptr) as long
declare sub stringfromipv4(byval ip_buf_4_bytes as ubyte ptr, byval outbuf_16_bytes as zstring ptr)
declare function isnumericipv4(byval ipstring as const zstring ptr) as long

#define mem_equal(A, B, C) (memcmp((A), (B), (C)) = 0)
#define str_equal(A, B) (strcmp((A), (B)) = 0)
#define strlitlen(A) (sizeof(A) - 1)

extern conv_cypher as const zstring * ...
extern conv_cypher_len as const uinteger
extern base64_tbl as const zstring * 64

#define BASE64ENC_BYTES(N) (((N + 2) / 3) * 4)
#define BASE64ENC_BUFSIZE(N) (1 + BASE64ENC_BYTES(N))
#define BASE64DEC_BYTES(N) ((N / 4) * 3)
#define BASE64DEC_BUFSIZE(N) (1 + BASE64DEC_BYTES(N))

declare sub base64enc(byval dst as zstring ptr, byval src as const ubyte ptr, byval count as uinteger)
declare function base64dec(byval dst as ubyte ptr, byval src as const zstring ptr, byval dst_len as uinteger) as uinteger
declare function base64enc_str(byval dst as zstring ptr, byval src as const ubyte ptr, byval dst_len as uinteger) as long
declare sub rc4(byval dst as ubyte ptr, byval src as const ubyte ptr, byval len_ as uinteger, byval key as const ubyte ptr, byval keylen as uinteger)

end extern
hex.bas (translated from examples/hex.c)

Code: Select all

#pragma once

#include once "unistd.bi"
#include once "crt/stddef.bi"
#include once "format.bi"
#include once "strlib.bi"

private function usage(byval argv0 as zstring ptr) as long
    ulz_dprintf(2, !"%s - dumps input from stdin as hex stream\n", argv0)
    return 1
end function

private function main(byval argc as long, byval argv as zstring ptr ptr) as long
    dim buf(0 to 1023) as ubyte
    dim out as zstring * 2048 + 1
    if argc > 1 then
        return usage(argv[0])
    end if
    while 1
        dim n as integer = read(0, buf, sizeof(buf))
        if n <= 0 then
            exit while
        end if
        raw2hex(out, buf, n)
        ulz_printf("%s", out)
    wend
    ulz_printf(!"\n")
    return 0
end function

main()
Edit: library currently fails to build with this error so the code above is not tested:

cc -Os -std=c99 -I./include -c -o src/filelib/mktempdir.o src/filelib/mktempdir.c
In file included from src/filelib/../../include/stringptr.h:11,
from src/filelib/../../include/filelib.h:7,
from src/filelib/mktempdir.c:2:
C:/Toolchain/msys64/ucrt64/x86_64-w64-mingw32/include/stdlib.h:708:17: error: conflicting types for 'itoa'; have 'char *(int, char *, int)'
708 | char *__cdecl itoa(int _Val,char *_DstBuf,int _Radix) __MINGW_ATTRIB_DEPRECATED_MSVC2005;
| ^~~~
In file included from src/filelib/mktempdir.c:1:
src/filelib/../../include/strlib.h:50:7: note: previous declaration of 'itoa' with type 'char *(int, char *)'
50 | char *itoa(int number, char* buffer);
| ^~~~
make: *** [Makefile:49: src/filelib/mktempdir.o] Error 1
miilvyxg
Posts: 193
Joined: Dec 07, 2021 6:51

Re: libulz - a collection of useful functions and data structures in C

Post by miilvyxg »

What's the point of sharing something completely untested and even doesn't build?
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: libulz - a collection of useful functions and data structures in C

Post by deltarho[1859] »

The primary function of a peer suuport commnity is in posting code which does not work.

The secondary function of a peer suuport commnity is in posting code which does work.

Image
Locked