🌐 English

    Utility API#

    These are useful functions used in Oasis.

    Header Files#

    OasisNet.h
    OasisUtil.h

    Network#

    int32_t get_netif_list ( char ifnames[][IFNAMSIZ] , int32_t ifnames_size , uint32_t flags = 0 )
    OasisNet.h
    Obtains a list of network interface names installed on the device.
    Parameters
    ifnames[][IFNAMSIZ]  OUT Returns the list of network interface names.
    ifnames_size  The maximum size of the network interface name list.
    flags  The flag value applied during searching.
    Return Value
    Returns the number of network interfaces. Returns -1 on failure.

    The flag value performs OR'ing of the following values:

    #define NETIF_LIST_F_LOOPBACK_INCLUDED          0x0001
    #define NETIF_LIST_F_MULTICAST_SUPPORTED        0x0002
    
    • NETIF_LIST_F_LOOPBACK_INCLUDED: Includes the loopback device during searching.
    • NETIF_LIST_F_MULTICAST_SUPPORTED: Includes only network interfaces that support multicast during searching.
    int32_t get_netif_list ( char ifnames[][IFNAMSIZ] , int32_t ifindices[] , int32_t ifnames_size , uint32_t flags = 0 )
    OasisNet.h
    Obtains a list of network interface names and a list of indices installed on the device.
    Parameters
    ifnames[][IFNAMSIZ]  OUT Returns the list of network interface names.
    ifindices[]  OUT Returns the list of network interface indices.
    ifnames_size  The maximum size of the network interface name and index lists.
    flags  The flag value applied during searching.
    Return Value
    Returns the number of network interfaces. Returns -1 on failure.
    int32_t get_current_mac_address ( char * ifname , size_t ifname_size , uint8_t * macaddr , int32_t * maclen )
    OasisNet.h
    Obtains the currently active network interface name and hardware address.
    Parameters
    ifname  OUT Returns the network name.
    ifname_size  The size of the network name buffer. Measured in bytes.
    macaddr  OUT Returns the hardware address.
    maclen  The size of the hardware address buffer. Measured in bytes.
    Return Value
    • 0: Success
    • -1: Failure
    int32_t get_mac_address ( int32_t s , uint8_t * macaddr , int32_t * maclen )
    OasisNet.h
    Obtains the hardware address connected to the socket.
    Parameters
    s  The socket handle number.
    macaddr  OUT Returns the hardware address.
    maclen  The size of the hardware address buffer. Measured in bytes.
    Return Value
    • 0: Success
    • -1: Failure
    int32_t get_mac_address ( const char * ifname , uint8_t * macaddr , int32_t * maclen )
    OasisNet.h
    Obtains the hardware address corresponding to the network interface name.
    Parameters
    ifname  The network interface name.
    macaddr  OUT Returns the hardware address.
    maclen  The size of the hardware address buffer. Measured in bytes.
    Return Value
    • 0: Success
    • -1: Failure
    int32_t get_current_netif_config ( const char * if_name , char * netaddr , char * netmask , char * gateway )
    OasisNet.h
    Obtains the network configuration settings of the network interface name.
    Parameters
    if_name  The network interface name.
    netaddr  OUT Returns the IPv4 address.
    netmask  OUT Returns the Netmask.
    gateway  OUT Returns the gateway address.
    Return Value
    • 0: Success
    • -1: Failure
    bool is_in_the_same_local_network ( const struct in_addr in )
    OasisNet.h
    Verifies whether the address is within the same local network.
    Parameters
    in  The IPv4 address to verify.
    Return Value
    • true: It is within the same local network.
    • false: It is within a different network.
    bool is_my_network_address ( const struct in_addr in )
    OasisNet.h
    Verifies whether the address is the IPv4 address of my device.
    Parameters
    in  The IPv4 address to verify.
    Return Value
    • true: It is my network address.
    • false: It is not my network address.
    std::string net_addr_to_string ( const struct sockaddr_in * addr )
    OasisNet.h
    Converts a network address into a string. The converted string format is <IPv4 address>/<Port Number>.
    Parameters
    addr  The address to convert.
    Return Value
    Returns the converted string address.
    int32_t get_netif_index ( int32_t s )
    OasisNet.h
    Obtains the index of the network interface connected to the socket.
    Parameters
    s  The socket handle number.
    Return Value
    Returns the network interface index number on success, and returns -1 on failure.
    int32_t get_netif_index ( struct in_addr in )
    OasisNet.h
    Obtains the index of the network interface connected to the local address.
    Parameters
    in  The local address.
    Return Value
    Returns the network interface index number on success, and returns -1 on failure.
    bool is_valid_netif ( const char * netif_name )
    OasisNet.h
    Verifies whether the network interface name is valid.
    Parameters
    netif_name  The network interface name.
    Return Value
    • true: It is valid.
    • false: It is not valid.
    bool validate_netifs ( const char * network_interfaces )
    OasisNet.h
    Verifies whether all network interface names in the list are valid.
    Parameters
    network_interfaces  The list of network interface names separated by commas (,). For example, it is formatted like "eth0,wlan0".
    Return Value
    Returns "true" if all are valid. Returns "false" if even one is invalid.
    int32_t get_netifs ( const char * network_interfaces , char ifnames[][IFNAMSIZ] , int32_t ifnames_size , uint32_t flags = 0 )
    OasisNet.h
    Obtains a list of network interface names.
    Parameters
    network_interfaces  The list of network interface names separated by commas (,). If it is not nullptr, it verifies whether the network interface names in this list are valid, and returns -1 if even one is invalid. If all are valid, it returns this list name. If it is nullptr, it finds and returns the list of network interface names existing on the device based on the flags value.
    ifnames[][IFNAMSIZ]  OUT Returns the list of network interface names.
    ifnames_size  The maximum size of the network interface name list.
    flags  The flag value applied during searching.
    Return Value
    Returns the list size on success. Returns -1 on failure.
    bool is_network_down ( const char * ifname )
    OasisNet.h
    Verifies whether the network connected to the network interface is down.
    Parameters
    ifname  The network interface name.
    Return Value
    • true: The network is down.
    • false: The network is up.
    void enable_internal_dns ( bool enable )
    OasisNet.h
    Activates the Oasis DNS client component.
    Parameters
    enable  Activates if true, and deactivates if false.
    int32_t set_dns_transport ( int32_t transport )
    OasisNet.h
    Specifies the network transport type to be used by the Oasis DNS client component.
    Parameters
    transport  A value of "0" uses TCP, and a value of "1" uses UDP.
    Return Value
    • 0: Success
    • -1: Failure
    int32_t set_dns_timeout ( int32_t timeout )
    OasisNet.h
    Configures the maximum time to wait for the Oasis DNS client component to resolve the FQDN.
    Parameters
    timeout  The maximum waiting time. Measured in milliseconds.
    Return Value
    • 0: Success
    • -1: Failure
    int32_t resolve_net_address ( const char * netaddr , uint32_t * addr4_ptr , bool use_systemd = false )
    OasisNet.h
    Converts an FQDN into an IPv4 address using the DNS service.
    Parameters
    netaddr  The FQDN. For example, it is a fully qualified name like "www.mediastek.com".
    addr4_ptr  OUT Returns the converted address string.
    use_systemd  Uses the system's DNS service if true. Uses the Oasis DNS service if false.
    Return Value
    • 0: Success
    • -1: Failure
    int32_t resolve_net_addresses ( const char * netaddr , std::vector<struct in_addr> & addrs )
    OasisNet.h
    Obtains a list of IPv4 addresses from the FQDN using the DNS service. Useful when a single FQDN has multiple IPv4 addresses.
    Parameters
    netaddr  The FQDN. For example, it is a fully qualified name like "www.mediastek.com".
    addrs  OUT Returns the converted list of IPv4 addresses.
    Return Value
    • 0: Success
    • -1: Failure
    ssize_t net_decode_url ( const char * url , int32_t len , std::string & decoded_url )
    OasisNet.h
    Decodes an encoded URL.
    Parameters
    url  The encoded URL string.
    len  The string length.
    decoded_url  OUT Returns the decoded URL string.
    Return Value
    Returns the length of the decoded URL string on success. Returns -1 on failure.
    ssize_t net_encode_url ( const char * url , std::string & encoded_url )
    OasisNet.h
    Encodes a URL.
    Parameters
    url  The URL to encode.
    encoded_url  OUT Returns the encoded URL string.
    Return Value
    Returns the length of the encoded URL string on success. Returns -1 on failure.
    const char * extension_to_mime_type ( const char * extension )
    OasisNet.h
    Converts a file extension into a MIME type. Supported extensions must be pre-registered during Oasis initialization or dynamically registered via the API. Case-insensitive.
    Parameters
    extension  The string pointer indicating the extension.
    Return Value
    Returns the corresponding MIME type for the file if the extension is supported. Returns "application/octet-stream" for an unknown extension.
    const char * get_file_mime_type ( const char * file_name )
    OasisUtil.h
    Returns the MIME type from a file name that has a typical file extension.
    Parameters
    file_name  The file name.
    Return Value
    Returns the MIME type for supported extensions, and returns "application/octet-stream" for unsupported extensions.

    Below is the MIME type table per file extension recognized by Oasis:

    Extension MIME Type
    html, htm, osp text/html
    png image/png
    gif image/gif
    bmp image/bmp
    jpeg, jpg image/jpeg
    css text/css
    js application/x-javascript
    mp4 video/mp4
    avi video/x-msvideo
    cgi text/html
    pdf application/pdf
    Others application/octet-stream

    Strings#

    std::string toHexString ( const uint8_t * data , size_t length , bool lowercase = true )
    OasisUtil.h
    Converts byte array data into a hexadecimal string. One byte is converted into two hexadecimal characters. The length of the returned string is twice the data length.
    Parameters
    data  The byte array data pointer.
    length  The array length.
    lowercase  Uses lowercase hexadecimal characters (abcdef) if "true". Uses uppercase hexadecimal characters (ABCDEF) if "false".
    Return Value
    A string composed of hexadecimal characters. For example, if the lowercase value is "true", it is converted like &lt;span class="monotext">5f1bc26509002142b6f2&lt;/span>. If the lowercase value is "false", it is converted like &lt;span class="monotext">5F1BC26509002142B6F2&lt;/span>.
    std::string toHexDumpString ( const uint8_t * data , size_t length , bool lowercase = true )
    OasisUtil.h
    Returns a Hex Dump format string where one line consists of 16 bytes, and each line is composed of hexadecimal characters and ASCII from the byte array data.
    Parameters
    data  The byte array data pointer.
    length  The array length.
    lowercase  Uses lowercase hexadecimal characters (abcdef) if "true". Uses uppercase hexadecimal characters (ABCDEF) if "false".
    Return Value
    The Hex Dump format string.

    Below is an example of toHexDumpString:

    " 00000000 33 59 c6 dd 97 07 1d d4 3b d3 5c 40 b3 b0 c9 a4 3Y......;.\@....\n 00000010 f1 0b 36 39 59 5c ed e8 dd 63 42 ce f9 d0 88 2c ..69Y...cB....,\n 00000020 29 4e 09 c0 \n"

    Printing this string to the console looks like the following:

       00000000  33 59 c6 dd 97 07 1d d4 3b d3 5c 40 b3 b0 c9 a4   3Y......;.\@....
       00000010  f1 0b 36 39 59 5c ed e8 dd 63 42 ce f9 d0 88 2c   ..69Y\...cB....,
       00000020  29 4e 09 c0                                       )N..
    
    std::string toHexAsciiString ( const uint8_t * data , size_t length )
    OasisUtil.h
    Returns a string where the byte array data is converted into respective ASCII values. Non-printable characters are converted into ".". For example, it returns in a format like &lt;span class="monotext">.p.j..#...Pp...[.av.r.H...o'.Z.D..&lt;/span>.
    Parameters
    data  The byte array data pointer.
    length  The array length.
    Return Value
    Returns the converted string.
    std::string toDotString ( const uint32_t * data , size_t length )
    OasisUtil.h
    Converts 32-bit array data into a string separated by ".". For example, it returns in a format like &lt;span class="monotext">561719494.513318045.664304121.498333846&lt;/span>.
    Parameters
    data  The 32-bit array data pointer.
    length  The array length.
    Return Value
    The converted string.
    std::string toDotString ( const uint8_t * data , size_t length )
    OasisUtil.h
    Converts byte array data into a string separated by ".". For example, it returns in a format like &lt;span class="monotext">70.230.47.188.127.96&lt;/span>.
    Parameters
    data  The byte array data pointer.
    length  The array length.
    Return Value
    The converted string.
    bool isNumber ( char * str )
    OasisUtil.h
    Verifies whether the string is composed entirely of numbers (0123456789).
    Parameters
    str  The string pointer.
    Return Value
    • true: Composed entirely of numbers.
    • false: Composed of other characters besides numbers.
    bool isHexNumber ( char * str )
    OasisUtil.h
    Verifies whether the string is composed entirely of hexadecimal characters (0123456789abcdefABCDEF).
    Parameters
    str  The string pointer.
    Return Value
    • true: Composed entirely of hexadecimal characters.
    • false: Composed of other characters besides hexadecimal characters.
    std::string format ( const char * format , ... )
    OasisUtil.h
    Returns a formatted string. The format is identical to printf.
    Parameters
    format  The format string.
    ...  The argument list.
    Return Value
    Returns the formatted string.
    std::string formatV ( const char * format , va_list args )
    OasisUtil.h
    Receives va_list as an argument and returns a formatted string. The format is identical to printf.
    Parameters
    format  The format string.
    args  The argument list.
    Return Value
    Returns the formatted string.
    void replace_all ( std::string & str , const char * from , const char * to )
    OasisUtil.h
    Changes the from string into the to string within a string.
    Parameters
    str  IN OUT The string.
    from  The string to find.
    to  The string to substitute.
    uint64_t string_to_number ( const char * string )
    OasisUtil.h
    Converts a string into a number. If the string starts with "0x" or "0X", it is considered a hexadecimal string and converted from hexadecimal to a number. Otherwise, it is considered a decimal string. If there is a hexadecimal notation such as \xNN in the middle of a decimal string, only that part is converted from hexadecimal to a number. It converts into a number only up to the part recognized as a number symbol.
    Parameters
    string  The string pointer.
    Return Value
    Returns the converted numeric value.
    ssize_t string_to_array ( const char * string , char * buffer , size_t size , uint8_t * padding_bits_ptr = nullptr )
    OasisUtil.h
    Converts a string into an array. If the string starts with "0b" or "0B", it is considered a binary number, grouped by 8 bits and converted into a single-byte value. If the last byte is not a complete 8 bits, the number of unfilled bits is returned to padding_bits_ptr. For example, if only 3 bits are filled with "110", a value of 5 is returned, and the last element value of the array becomes "192" (in binary, "1100 0000"). If the string starts with "0x" or "0X", it is considered a hexadecimal string, and two hexadecimal characters are converted and stored as a single-byte numeric value. In other cases, one character of the string is stored directly in the array byte by byte; if "\xNN" exists in the middle of this string, NN is considered a hexadecimal character and the converted numeric value is stored, if "\0" exists, "0" is stored, and if "\" exists, "\" is stored.
    Parameters
    string  The string pointer.
    buffer  The buffer pointer to convert.
    size  The buffer size. Measured in bytes. Specify at least the minimum string length.
    padding_bits_ptr  OUT Returns the number of last unfilled bits in the case of binary conversion.
    Return Value
    Returns the byte array size. Returns -22 (EINVAL) on failure.

    Base64#

    int32_t base64Decode ( uint8_t * out , const char * in , int32_t out_length )
    OasisUtil.h
    Obtains the decoded data from a BASE64 encoded string.
    Parameters
    out  OUT The buffer pointer to store the decoding result.
    in  The BASE64 encoded string.
    out_length  The length of the buffer where the decoding result will be stored. Measured in bytes. It is smaller than the encoded string length.
    Return Value
    Returns the decoded data length on success. Returns -1 on failure.
    int32_t base64Encode ( char * buf , int32_t buf_len , uint8_t * src , int32_t len , int32_t line_length = 0 )
    OasisUtil.h
    Obtains a BASE64 encoded string from data.
    Parameters
    buf  The buffer pointer to store the encoded string.
    buf_len  The buffer length to store the encoded string. Measured in bytes.
    src  The data pointer to encode.
    len  The data length to encode. Measured in bytes.
    line_length  If it is not 0, it breaks the encoded string by the line_length length line by line and returns it. Each line distinction uses "\a".
    Return Value
    Returns the encoded string length on success. If composed of multiple lines, the number of line separators "\a" is also included. For 3 lines, it is 2 greater than the actual encoded length. Returns -1 on failure.

    Below is the BASE64 encoded string when line_length is 0:

    BN+faS4nrqxihBlI7FrA/ouSVhdvYqNNWzVdZxkMPB3r24YZAjTFZLnerKU4bKTE/vrbbV1+u7i0GB/NJFvqDzZwKDik7Zxdy0gDBLSnyLKhox/+Idq21fPVohcwjCdm/E+eoT06/giCAQw2qNToSncISJni/27V1BHtBZ0=

    Below is the BASE64 encoded string when line_length is 32:

    BN+faS4nrqxihBlI7FrA/ouSVhdvYqNN<0x0a>
    WzVdZxkMPB3r24YZAjTFZLnerKU4bKTE<0x0a>
    /vrbbV1+u7i0GB/NJFvqDzZwKDik7Zxd<0x0a>
    y0gDBLSnyLKhox/+Idq21fPVohcwjCdm<0x0a>
    /E+eoT06/giCAQw2qNToSncISJni/27V<0x0a>
    1BHtBZ0=

    Random Data and UUID#

    void generateRandomBytes ( uint8_t * buf , size_t length )
    OasisUtil.h
    Obtains a random byte array.
    Parameters
    buf  The buffer pointer to store the random byte array.
    length  The buffer size. Measured in bytes.
    std::string generateUUIDString ( )
    OasisUtil.h
    Obtains a UUID string. For example, it is a string formatted like &lt;span class="monotext">1c7ffde7-ae42-1ce9-ae55-8000206779f2&lt;/span>.
    Return Value
    Returns the UUID string.

    Time#

    std::string tvToString ( struct timeval * tv )
    OasisUtil.h
    Obtains the timeval structure as a string converted to the format "yyyy/mm/dd hh:mm:ss". Based on UTC+0. For example, it is converted to a format like &lt;span class="monotext">2000/06/07 21:34:39&lt;/span>.
    Parameters
    tv  The pointer to the timeval structure to convert.
    Return Value
    Returns the converted string.
    int64_t tvSubtract ( struct timeval * x , struct timeval * y )
    OasisUtil.h
    Returns the difference between two timeval structures.
    Parameters
    x  The pointer to the timeval structure.
    y  The pointer to the timeval structure.
    Return Value
    Returns the x-y value. Measured in microseconds.
    int32_t getTimeOfDay ( struct timeval * tv )
    OasisUtil.h
    Obtains the CLOCK_MONOTONIC value. This is an incrementally increasing timer value after system booting.
    Parameters
    tv  OUT The pointer to the timeval structure to be stored.
    Return Value
    Always returns 0.
    uint64_t systemTime ( )
    OasisUtil.h
    Obtains an incrementally increasing timer value after system booting. Measured in microseconds.
    Return Value
    The timer value.
    uint64_t timeSpecToUsec ( struct timespec & ts )
    OasisUtil.h
    Obtains a value where the timespec value is converted into a microsecond value. timespec can represent nanosecond values.
    Parameters
    ts  The timespec structure reference.
    Return Value
    Returns the converted value.
    uint64_t timeSpecToUsec ( struct timespec * ts )
    OasisUtil.h
    Obtains a value where the timespec value is converted into a microsecond value. timespec can represent nanosecond values.
    Parameters
    ts  The pointer to the timespec structure.
    Return Value
    Returns the converted value.
    void parseUsec ( int64_t usec , int & h , int & m , int & s , int & u )
    OasisUtil.h
    Decomposes microseconds into hours, minutes, seconds, and microseconds.
    Parameters
    usec  The microsecond value.
    h  OUT Returns the hours. The hours value can exceed 24 (a day).
    m  OUT Returns the minutes.
    s  OUT Returns the seconds.
    u  OUT Returns the microseconds.

    Parameter Map#

    void mergeParameters ( const char * tag_string , key_value_map_t & to_parameters , key_value_map_t & from_parameters , const char * prefix_string , bool copy_prefixed_pair_only = false )
    OasisUtil.h
    Merges two key-value maps. If prefix_string is given, keys having this string as a prefix are changed to key names with this prefix removed, and then merged.
    Parameters
    tag_string  The TAG string to be used for simple reference like log output, etc.
    to_parameters  IN OUT The parameter map where the merged result is returned.
    from_parameters  The parameter map to find the key-value elements to merge.
    prefix_string  If it is not nullptr, it finds keys having the string specified in this argument as a prefix from from_parameters and uses keys with the prefix removed. For example, if prefix_string is channel1-, and a key named channel1-fps exists in from_parameters, the merged key becomes fps.
    copy_prefixed_pair_only  If "true", it merges only key-value elements whose key prefix matches such as channel1-. If "false", when prefix_string is specified, it merges key elements with this prefix removed, and other keys are merged without modification.
    void mergeParameters ( const char * tag_string , key_value_map_t & to_parameters , key_value_map_t & from_parameters , const char * prefix_string , const char * excluded_prefix_string , bool copy_prefixed_pair_only = false )
    OasisUtil.h
    Merges two key-value maps. If prefix_string is given, keys having this string as a prefix are changed to key names with this prefix removed, and then merged. If excluded_prefix_string is given, keys having this prefix are excluded from merging.
    Parameters
    tag_string  The TAG string to be used for simple reference like log output, etc.
    to_parameters  IN OUT The parameter map where the merged result is returned.
    from_parameters  The parameter map to find the key-value elements to merge.
    prefix_string  If it is not nullptr, it finds keys having the string specified in this argument as a prefix from from_parameters and uses keys with the prefix removed. For example, if prefix_string is channel1-, and a key named channel1-fps exists in from_parameters, the merged key becomes fps.
    excluded_prefix_string  If it is not nullptr, it finds keys having the string specified in this argument as a prefix from from_parameters and excludes them from merging.
    copy_prefixed_pair_only 
    void regExcludeParameters ( const char * tag_string , key_value_map_t & , key_value_map_t & from_parameters , const char * exclude_regex )
    OasisUtil.h
    Merges two key-value maps. Key names matching the regular expression string given as exclude_regex are excluded from merging.
    Parameters
    tag_string  The TAG string to be used for simple reference like log output, etc.
      IN OUT The parameter map where the merged result is returned.
    from_parameters  The parameter map to find the key-value elements to merge.
    exclude_regex  The regular expression to find key names of elements to exclude from merging.
    void dumpParameters ( const char * tag , key_value_map_t & parameters , const char * key = nullptr )
    OasisUtil.h
    Outputs a key-value map as log messages.
    Parameters
    tag  The string pointer to be used as a TAG when outputting logs.
    parameters  The key-value map to dump.
    key  If it is not nullptr, it outputs only elements having this key value.

    INI Files#

    The get_profile_section, get_profile_section_names, get_profile_string, and write_profile_string functions all manipulate the contents of files with an ini type extension.

    An ini file is composed of multiple sections, and each section consists of <Key Name>=<Value> lines. # or ; are used as comment delimiters. If <Value> contains spaces, it starts with double quotes and ends with double quotes. Section names are case-sensitive.

    ; Comment
    # Comment
    [Section1 Name]
    KeyName=Value   # Comment
    KeyName=Value   ; Comment
    

    An example is shown below:

    [general]
    key1=value1
    key2="general value2"  ; comment
    
    [section1]
    key1=section1_value1
    key2="section1 value2"  ; comment
    
    [section2]
    key1=section2_value1
    key2="section2 value2"  ; comment
    
    int32_t get_profile_section ( const char * section , char * buf , int32_t buf_size , const char * file )
    OasisUtil.h
    Finds a section in the file and obtains the "Key=Value" list. If there is a comment on the corresponding line, the comment is also included. Each line in the list ends with a null character ('\0'). An additional null character ('\0') is appended at the very end.
    Parameters
    section  The string pointer indicating the section name to find.
    buf  OUT The pointer to the buffer to store the "Key=Value" list.
    buf_size  The buffer size. Measured in bytes.
    file  The ini file path.
    Return Value
    Returns the size of the "Key=Value" list on success. Measured in bytes. Returns -1 on failure.
    :[general], __data=0xffffeafcf4c8, __size=47 of 47:
       00000000  6b 65 79 31 3d 76 61 6c 75 65 31 00 6b 65 79 32   key1=value1.key2
       00000010  3d 22 67 65 6e 65 72 61 6c 20 76 61 6c 75 65 32   ="general value2
       00000020  22 20 20 3b 20 63 6f 6d 6d 65 6e 74 00 00 00      "  ; comment...
    
    int32_t get_profile_section_names ( char * buf , int32_t buf_size , const char * file )
    OasisUtil.h
    Obtains a list of section names from the file. Each line ends with a null character ('\0'). An additional null character ('\0') is appended at the very end.
    Parameters
    buf  The buffer pointer where the section name list will be stored.
    buf_size  The buffer size. Measured in bytes.
    file  The ini file path.
    Return Value
    Returns the size of the section name list on success. Measured in bytes. Returns -1 on failure.
    • 0: Success
    • -1: Failure
    :section list, __data=0xffffeafcf4c8, __size=27 of 27:
       00000000  67 65 6e 65 72 61 6c 00 73 65 63 74 69 6f 6e 31   general.section1
       00000010  00 73 65 63 74 69 6f 6e 32 00 00                  .section2..
    
    int32_t get_profile_string ( const char * section , const char * key , const char * default_str , char * buf , int32_t buf_size , const char * file )
    OasisUtil.h
    Obtains the value of an entry having the key name key located inside section from the file.
    Parameters
    section  The string pointer indicating the section name to find.
    key  The string pointer indicating the key name inside the section to find.
    default_str  The default value to be stored in the buffer if key cannot be found. If it is nullptr, and key is not found, it returns -1. An empty string is also allowed.
    buf  OUT The pointer to the buffer where the value will be stored.
    buf_size  The buffer size. Measured in bytes.
    file  The ini file path.
    Return Value
    Returns the length of the value string on success. Returns 0 if it is an empty string. Returns -1 on failure.
    int32_t write_profile_string ( const char * section , const char * key , const char * str , const char * file )
    OasisUtil.h
    Overwrites the value of an entry having the key name key located inside section with the str string in the file. If there are whitespace characters (spaces or tabs) inside str, double quotes are appended to the front and back of the string. If str is nullptr, it deletes the key entry.
    Parameters
    section  The string pointer indicating the section name to find.
    key  The string pointer indicating the key name inside the section to find.
    str  The string pointer to overwrite with the new value.
    file  The ini file path.
    Return Value
    • 0: Success
    • -1: Failure

    File Path#

    std::string get_path_dir ( const std::string & path )
    OasisUtil.h
    Obtains the directory path from a file path.
    Parameters
    path  The file path.
    Return Value
    Returns the directory path.
    std::string get_path_leaf ( const std::string & path )
    OasisUtil.h
    Obtains the Leaf string consisting of the file name, delimiter (.), and extension from a file path.
    Parameters
    path  The file path.
    Return Value
    Returns the Leaf string of the path. Returns an empty string if there is no Leaf.
    std::string get_path_leaf_name ( const std::string & path , bool make_lower )
    OasisUtil.h
    Obtains the file name excluding the extension from a file path.
    Parameters
    path  The file path.
    make_lower  Converts to lowercase and returns if "true". Returns without case conversion if "false".
    Return Value
    Returns the file name. Returns an empty string if there is no file name.
    std::string get_file_ext ( const std::string & path , bool make_lower = true )
    OasisUtil.h
    Returns the extension of the file from a file path.
    Parameters
    path  The file path.
    make_lower  Converts to lowercase and returns if "true". Returns without case conversion if "false".
    Return Value
    The extension string. Returns an empty string if there is no extension.

    Below are examples of applying the above functions to several paths:

    path: /a/b/c/d/file.ext
    get_path_dir: /a/b/c/d
    get_path_leaf: file.ext
    get_path_leaf_name: file
    get_file_ext: ext
    
    path: /a/b/c/d/
    get_path_dir: /a/b/c/d
    get_path_leaf: 
    get_path_leaf_name: 
    get_file_ext: 
    
    path: /a/b/c/d/file
    get_path_dir: /a/b/c/d
    get_path_leaf: file
    get_path_leaf_name: file
    get_file_ext: 
    
    path: /a/b/c/d/.ext
    get_path_dir: /a/b/c/d
    get_path_leaf: .ext
    get_path_leaf_name: 
    get_file_ext: ext
    
    path: /a/b/c/d/.
    get_path_dir: /a/b/c/d
    get_path_leaf: .
    get_path_leaf_name: 
    get_file_ext: 
    
    char * canonicalizePath ( const char * path , char * c_path )
    OasisUtil.h
    Changes an absolute path into a canonical absolute path.
    Parameters
    path  The absolute path. If it does not start with '/', it returns path as it is to c_path.
    c_path  OUT Returns the canonical absolute path.
    Return Value
    Returns the start pointer of the canonical absolute path string.
    const char * canonicalizePath ( const char * path , std::string & c_path )
    OasisUtil.h
    Changes an absolute path into a canonical absolute path.
    Parameters
    path  The absolute path. If it does not start with '/', it returns path as it is to c_path.
    c_path  OUT Returns the canonical absolute path.
    Return Value
    Returns the start pointer of the canonical absolute path string.

    File I/O#

    int32_t writeStringToFile ( const char * file_path , const char * value )
    OasisUtil.h
    Writes a string to a file. Overwrites if the file already exists.
    Parameters
    file_path  The file path.
    value  The string pointer.
    Return Value
    Returns the number of written bytes on success. Returns -1 on failure.
    int32_t appendStringToFile ( const char * file_path , const char * value )
    OasisUtil.h
    Appends a string to a file. Creates a new file if the file does not exist.
    Parameters
    file_path  The file path.
    value  The string pointer.
    Return Value
    Returns the number of written bytes on success. Returns -1 on failure.
    int32_t readStringFromFile ( const char * file_path , char * value , size_t size , bool trim_trailing_lws = false )
    OasisUtil.h
    Reads a string from a text file.
    Parameters
    file_path  The file path.
    value  OUT The buffer pointer to store the string.
    size  The buffer size. Measured in bytes.
    trim_trailing_lws  Removes whitespace characters (0x20, 0x0d, 0x0a, 0x09), etc. at the end of the last line string if "true".
    Return Value
    Returns the number of read bytes on success. Returns -1 on failure.
    ssize_t readLinesFromFile ( const char * file_path , char * value , size_t size )
    OasisUtil.h
    Reads all lines from a text file consisting of multiple lines. A null character ('\0') is appended to the end of each line. An additional null character ('\0') is appended at the very end.
    Parameters
    file_path  The file path.
    value  OUT The buffer pointer to store the entire string.
    size  The buffer size. Measured in bytes. If the buffer size is too small, the total string data read will be truncated.
    Return Value
    Returns the total number of read bytes on success. Returns -1 on failure.

    Below is an example of HEX DUMPing the read data buffer:

    readLinesFromFile, __data=0xfffff09f8d68, __size=206 of 206:
       00000000  23 20 63 6f 6d 6d 65 6e 74 0a 00 0a 00 5b 67 65   # comment....[ge
       00000010  6e 65 72 61 6c 5d 0a 00 6b 65 79 31 3d 76 61 6c   neral]..key1=val
       00000020  75 65 31 0a 00 6b 65 79 32 3d 22 67 65 6e 65 72   ue1..key2="gener
       00000030  61 6c 20 76 61 6c 75 65 32 22 20 20 3b 20 63 6f   al value2"  ; co
       00000040  6d 6d 65 6e 74 0a 00 0a 00 5b 73 65 63 74 69 6f   mment....[sectio
       00000050  6e 31 5d 0a 00 6b 65 79 32 3d 0a 00 6b 65 79 33   n1]..key2=..key3
       00000060  3d 22 6e 65 77 20 6b 65 79 20 76 61 6c 75 65 22   ="new key value"
       00000070  0a 00 6b 65 79 31 3d 22 73 68 6f 72 74 20 76 61   ..key1="short va
       00000080  6c 75 65 22 0a 00 0a 00 5b 73 65 63 74 69 6f 6e   lue"....[section
       00000090  32 5d 0a 00 6b 65 79 31 3d 73 65 63 74 69 6f 6e   2]..key1=section
       000000a0  32 5f 76 61 6c 75 65 31 0a 00 6b 65 79 32 3d 22   2_value1..key2="
       000000b0  73 65 63 74 69 6f 6e 32 20 76 61 6c 75 65 32 22   section2 value2"
       000000c0  20 20 3b 20 63 6f 6d 6d 65 6e 74 0a 00 00           ; comment...
    
    ssize_t readTailLinesFromFile ( const char * file_path , int32_t tail_count , std::list<std::string> & lines , bool trim_trailing_lws = false , bool skip_empty_lines = false , bool skip_bash_comment_lines = false )
    OasisUtil.h
    Reads tail_count lines from the end of a text file consisting of multiple lines. The first line of the list is stored starting from the top line. The last line of the list is identical to the last line of the file.
    Parameters
    file_path  The file path.
    tail_count  The number of lines from the end. If the value is "0", it reads the entire lines.
    lines  OUT The string list reference where each line will be stored.
    trim_trailing_lws  Removes whitespace characters (0x20, 0x0d, 0x0a, 0x09), etc. at the end of each line string if "true". Whitespace characters at the beginning of a line are always removed.
    skip_empty_lines  Excludes empty strings if "true".
    skip_bash_comment_lines  Excludes comment lines starting with '#' if "true".
    Return Value
    Returns the size of the lines list on success. Returns -1 on failure.
    ssize_t readMapFromFile ( const char * file_path , key_value_map_t & parameters , char delimiter = '=' )
    OasisUtil.h
    Reads a text file where each line consists of <Key><delimiter><Value> to obtain a key-value map.
    Parameters
    file_path  The file path.
    parameters  Returns the converted key-value map.
    delimiter  The delimiter between "Key" and "Value".
    Return Value
    Returns the key-value map size on success. Returns 0 on failure.
    ssize_t writeFile ( const void * data , size_t length , const char * file_path , mode_t mode = 0666 )
    OasisUtil.h
    Creates a file with the given access permission mode and writes data. Overwrites if the file already exists.
    Parameters
    data  The pointer to the data to write.
    length  The data length. Measured in bytes.
    file_path  The file path.
    mode  The access permission mode of the file to be created. An octal representation "0666" means "rw-rw-rw-".
    Return Value
    Returns the number of written bytes on success. Returns -1 on failure.
    ssize_t readFile ( const char * file_path , std::vector<uint8_t> & data )
    OasisUtil.h
    Reads data as a uint8_t type from a file.
    Parameters
    file_path  The file path.
    data  OUT The vector reference to store the read data.
    Return Value
    Returns the total number of read bytes on success. Returns -1 on failure.
    ssize_t readFile ( const char * file_path , std::vector<uint8_t> & data )
    OasisUtil.h
    Reads data as a char type from a file.
    Parameters
    file_path  The file path.
    data  OUT The vector reference to store the read data.
    Return Value
    Returns the total number of read bytes on success. Returns -1 on failure.
    char * getOFFSMountedPoint ( char * offs_mounted_path , int32_t size )
    OasisUtil.h
    Obtains the system folder path where the Oasis File System is mounted.
    Parameters
    offs_mounted_path  OUT The buffer pointer to store the mounted path.
    size  The buffer size. Measured in bytes.
    Return Value
    Returns the path string pointer if the mounted path is found. Returns nullptr if it cannot be found.
    bool isPathOnOFFS ( const char * path )
    OasisUtil.h
    Verifies whether a path is located within the path mounted on the Oasis File System.
    Parameters
    path  The path.
    Return Value
    • true: The path is located on the Oasis File System.
    • false: The path is not located on the Oasis File System.
    bool checkBlkDevMounted ( const char * blkdev_path , std::string * blkdev_mounted_path = nullptr , std::string * blkdev_fs_type = nullptr )
    OasisUtil.h
    Verifies whether a block device is mounted.
    Parameters
    blkdev_path  The block device path to verify. For example, it is like /dev/mmcblk0p1.
    blkdev_mounted_path  OUT Stores the mounted folder path if it is not nullptr and is mounted.
    blkdev_fs_type  OUT Stores the file system type string if it is not nullptr and is mounted.
    Return Value
    • true: It is mounted.
    • false: It is not mounted.
    bool checkMounted ( const char * path , const char * type = "fuse.sshfs" )
    OasisUtil.h
    Verifies whether a mounted path is a specific file system type.
    Parameters
    path  The mounting path.
    type  The file system type. Options include ext4, squashfs, devtmpfs, tmpfs, proc, sysfs, ubifs, fuse.ssfs, fuse.ntfs-3g, fuse.offs, etc. The file system types can be verified in /proc/filesystems.
    Return Value
    • true: It is the specific file system type.
    • false: It is not the specific file system type.

    Resources and Environment Variables#

    float getCPUUsage ( )
    OasisUtil.h
    Obtains the total CPU utilization.
    Return Value
    Returns the percentage rate.
    float getThisCPUUsage ( bool irix_mode )
    OasisUtil.h
    Obtains the CPU utilization of the calling process.
    Parameters
    irix_mode  Calculates the sum of all CPU utilization in Irix mode if "true", and calculates the value divided by the number of processes in Solaris mode if "false".
    Return Value
    Returns the percentage rate.
    uint64_t getFreeMemorySize ( )
    OasisUtil.h
    Obtains the available actual physical memory size. Returns the sum of unallocated memory and buffered or cached memory in bytes.
    Return Value
    The available memory size. Measured in bytes.
    uint64_t getThisMemoryUsage ( )
    OasisUtil.h
    Obtains the actual physical memory size currently used by the calling process.
    Return Value
    The used memory size. Measured in bytes.
    int32_t getThisThreadCount ( )
    OasisUtil.h
    Obtains the number of threads created and used by the calling process.
    Return Value
    Returns the thread count.
    void killProcess ( std::string pname , int32_t timeout_msec = 5000 )
    OasisUtil.h
    Terminates all processes with the specified program name. Sends a SIGTERM signal and waits for timeout_msec until all processes are terminated. If running processes are still found afterwards, it sends a SIGKILL signal. It does not wait further after sending the SIGKILL signal.
    Parameters
    pname 
    timeout_msec 
    void loadEnvironmentVariables ( const char * home_dir , const char * env_file_path , const char * known_env_variables[] , key_value_map_t & resolved_variables , std::list<std::string> & sources )
    OasisUtil.h
    Reads a file where shell environment variables are stored, and obtains the environment variable names and values specified in the known_env_variables list. Useful when a user with high privileges (such as root) reads that user's environment variables from another user's files like source or .profile. It does not eval shell script statements inside the file.
    Parameters
    home_dir  The $HOME value of the corresponding user.
    env_file_path  The environment variable file path.
    known_env_variables[]  The list of known environment variable names. The last element must be nullptr.
    resolved_variables  OUT Returns a key-value map with environment variable names as key names.
    sources  The list of paths of other environment variable files referenced when referencing other environment variable files inside env_file_path.

    When the /etc/profile file contents are as follows:

    export PATH="/bin:/sbin:/usr/bin:/usr/sbin"
    
    if [ "$PS1" ]; then
            if [ "`id -u`" -eq 0 ]; then
                    export PS1='# '
            else
                    export PS1='$ '
            fi
    fi
    
    export EDITOR='/bin/vi'
    
    # Source configuration files from /etc/profile.d
    for i in /etc/profile.d/*.sh ; do
            if [ -r "$i" ]; then
                    . $i
            fi
    done
    unset i
    

    When wishing to know the PATH and EDITOR environment variable values:

    const char *known_env_variables[] = { "PATH", "EDITOR", nullptr };
    key_value_map_t vars;
    std::list<std::string> sources;
    loadEnvironmentVariables("/" , "/etc/profile" , known_env_variables , vars , sources);
    
    dumpParameters("loadEnvironmentVariables", vars);
    

    Executing it displays results like the following:

    loadEnvironmentVariables parameters[PATH] /bin:/sbin:/usr/bin:/usr/sbin
    loadEnvironmentVariables parameters[EDITOR] /bin/vi