Concord - C Discord API library
A Discord API wrapper library written in C
cog-utils.h File Reference
#include <stdio.h>
#include <stdint.h>
Include dependency graph for cog-utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

char * cog_load_whole_file_fp (FILE *fp, size_t *len)
 Load file contents into a string. More...
 
char * cog_load_whole_file (const char filename[], size_t *len)
 Load file contents into a string. More...
 
long cog_timezone (void)
 Get the difference between UTC and the latest local standard time, in seconds. More...
 
int cog_iso8601_to_unix_ms (const char str[], size_t len, uint64_t *p_value)
 Convert a iso8601 string to a unix timestamp (milliseconds) More...
 
int cog_unix_ms_to_iso8601 (char str[], size_t len, const uint64_t value)
 Convert a unix timestamp (milliseconds) to a iso8601 string. More...
 
int cog_strtou64 (char *str, size_t len, uint64_t *p_value)
 Convert a numerical string to uint64_t More...
 
int cog_u64tostr (char *str, size_t len, uint64_t *p_value)
 Convert uint64_t to a numerical string. More...
 
size_t cog_strndup (const char src[], size_t len, char **p_dest)
 Copies at most len bytes of src to *p_dest. More...
 
size_t cog_asprintf (char **strp, const char fmt[],...)
 Copies at most len bytes of src to *p_dest. More...
 
int cog_sleep_ms (const long tms)
 Sleep for amount of milliseconds. More...
 
int cog_sleep_us (const long tms)
 Sleep for amount of microseconds. More...
 
uint64_t cog_timestamp_ms (void)
 Get the current timestamp in milliseconds. More...
 
uint64_t cog_timestamp_us (void)
 Get the current timestamp in microseconds. More...
 

Function Documentation

◆ cog_load_whole_file_fp()

char * cog_load_whole_file_fp ( FILE *  fp,
size_t *  len 
)

Load file contents into a string.

Parameters
fpthe file to be read
lenoptional pointer to store the amount of bytes read
Returns
the file contents

◆ cog_load_whole_file()

char * cog_load_whole_file ( const char  filename[],
size_t *  len 
)

Load file contents into a string.

Wrapper over cog_load_whole_file_fp(), get the file by its relative-path.

Parameters
filenamethe name of the file to be read
lenoptional pointer to store the amount of bytes read
Returns
the file contents

◆ cog_timezone()

long cog_timezone ( void  )

Get the difference between UTC and the latest local standard time, in seconds.

Returns
difference between UTC and local time in seconds

◆ cog_iso8601_to_unix_ms()

int cog_iso8601_to_unix_ms ( const char  str[],
size_t  len,
uint64_t *  p_value 
)

Convert a iso8601 string to a unix timestamp (milliseconds)

Can be matched to the json_extract() and json_inject() F specifier

Parameters
strthe iso8601 string timestamp
lenthe string length
p_valuepointer to the uint64_t variable to receive the converted timestamp
Returns
1 on success, 0 on failure

◆ cog_unix_ms_to_iso8601()

int cog_unix_ms_to_iso8601 ( char  str[],
size_t  len,
const uint64_t  value 
)

Convert a unix timestamp (milliseconds) to a iso8601 string.

Parameters
timestampthe buffer to receive the converted timestamp
lenthe size of the buffer
valuethe unix timestamp to be converted to iso8601
Returns
the amount of characters (in bytes) written to the buffer

◆ cog_strtou64()

int cog_strtou64 ( char *  str,
size_t  len,
uint64_t *  p_value 
)

Convert a numerical string to uint64_t

Parameters
strthe numerical string
lenthe string length
p_valuepointer to the uint64_t variable to receive the converted value
Returns
1 on success, 0 on failure

◆ cog_u64tostr()

int cog_u64tostr ( char *  str,
size_t  len,
uint64_t *  p_value 
)

Convert uint64_t to a numerical string.

Parameters
strthe buffer to store the numerical string
lenthe size of the buffer
p_valuethe unsigned long long value
Returns
the amount of characters (in bytes) written to the buffer

◆ cog_strndup()

size_t cog_strndup ( const char  src[],
size_t  len,
char **  p_dest 
)

Copies at most len bytes of src to *p_dest.

Analogous to strndup()

Parameters
srcthe buffer to be copied
lenthe maximum amount of characters to be copied
p_desta pointer to the new src copy
Returns
length of copied string on success, 0 on failure

◆ cog_asprintf()

size_t cog_asprintf ( char **  strp,
const char  fmt[],
  ... 
)

Copies at most len bytes of src to *p_dest.

Analogous to asprintf()

Parameters
strpsource to write resulting string to
fmtprintf format string
...variadic arguments to be matched to fmt specifiers
Returns
length of copied string on success, -1 on failure

◆ cog_sleep_ms()

int cog_sleep_ms ( const long  tms)

Sleep for amount of milliseconds.

Parameters
tmsamount of milliseconds to sleep for
Returns
0 on success, -1 on error with an errno set to indicate the error

◆ cog_sleep_us()

int cog_sleep_us ( const long  tms)

Sleep for amount of microseconds.

Parameters
tmsamount of microseconds to sleep for
Returns
0 on success, -1 on error with an errno set to indicate the error

◆ cog_timestamp_ms()

uint64_t cog_timestamp_ms ( void  )

Get the current timestamp in milliseconds.

Returns
the timestamp on success, 0 on failure

◆ cog_timestamp_us()

uint64_t cog_timestamp_us ( void  )

Get the current timestamp in microseconds.

Returns
the timestamp on success, 0 on failure