Telodendria/src/include/Util.h

122 lines
3.8 KiB
C
Raw Normal View History

/*
* Copyright (C) 2022 Jordan Bancino <@jordan:bancino.net>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
2022-07-28 16:00:52 +00:00
* included in all copies or portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2022-07-29 19:36:21 +00:00
/*
* Util.h: Some misc. helper functions that provide functionality that
* doesn't need its own full API. The functions here are entirely
* stand-alone, and generally don't depend on any of the other APIs
* defined by Telodendria.
*/
#ifndef TELODENDRIA_UTIL_H
#define TELODENDRIA_UTIL_H
#include <stdio.h>
#include <stddef.h>
#include <sys/types.h>
2022-07-29 19:36:21 +00:00
/*
* Get the current type in milliseconds since the Unix epoch. This uses
* POSIX gettimeofday(2) and time_t, and converts it to a single number,
* which is returned.
*
* A note on the 2038 problem: that's a long ways away, screw future
* me!
*
* Kidding. As long as (sizeof(long) == 8), that is, as long as the
* long datatype is 64 bits, which is is on all modern 64-bit Unix-like
* operating systems, then everything should be fine. Expect
* Telodendria on 32-bit machines to break in 2038. I didn't want to
* try to hack together some system to store larger numbers than the
* architecture supports. We can always re-evaluate things over the
* next decade.
*
* Return: A long representing the current time in milliseconds since
* the beginning of the Unix epoch, just as the Matrix spec requires.
*/
extern unsigned long
UtilServerTs(void);
2022-11-15 18:20:05 +00:00
extern unsigned long
UtilLastModified(char *);
extern int
UtilMkdir(const char *, const mode_t);
2022-07-29 19:36:21 +00:00
/*
* Encode a single UTF-8 codepoint as a string buffer containing
* between 1 and 4 bytes. The string buffer is allocated on the heap,
* so it should be freed when it is no longer needed.
*
* Params:
*
* (unsigned long) The UTF-8 codepoint to encode as a byte buffer.
*
* Return: a null-terminated byte buffer representing the UTF-8
* codepoint.
*/
extern char *
2022-07-29 19:36:21 +00:00
UtilUtf8Encode(unsigned long);
/*
* Duplicate a null-terminated string, and return a new string on the
* heap.
*
* Params:
* (char *) The string to duplicate. It can be located anywhere on
* the heap or the stack.
*
* Return: A pointer to a null-terminated string on the heap. You must
* free() it when you're done with it. This may also return NULL if the
* call to malloc() fails.
*/
extern char *
UtilStringDuplicate(char *);
2022-11-15 18:20:05 +00:00
extern char *
UtilStringConcat(char *, char *);
2022-08-24 23:30:20 +00:00
/*
* Sleep for the given number of milliseconds. This is a simple wrapper
* for nanosleep() that makes its usage much easier.
*
* Params:
* (long) The number of milliseconds to sleep for.
*
* Return: The result of nanosleep().
*/
extern int
UtilSleepMillis(long);
extern size_t
UtilStringToBytes(char *);
extern ssize_t
UtilGetDelim(char **, size_t *, int, FILE *, int *);
extern ssize_t
UtilGetLine(char **, size_t *, FILE *, int *);
2022-07-29 19:36:21 +00:00
#endif /* TELODENDRIA_UTIL_H */