2024-07-06 09:03:13 +01:00
|
|
|
/**
|
|
|
|
* @file util.c
|
|
|
|
* @author Onyx and Iris (code@onyxandiris.online)
|
|
|
|
* @brief Utility functions.
|
|
|
|
* @version 0.5.0
|
|
|
|
* @date 2024-07-06
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2024
|
|
|
|
* https://github.com/onyx-and-iris/vmrcli/blob/main/LICENSE
|
|
|
|
*/
|
|
|
|
|
2024-06-27 22:06:15 +01:00
|
|
|
#include <stddef.h>
|
2024-06-28 03:21:38 +01:00
|
|
|
#include <stdio.h>
|
2024-07-06 09:03:13 +01:00
|
|
|
#include "wrapper.h"
|
2024-06-28 03:21:38 +01:00
|
|
|
#include "util.h"
|
|
|
|
|
2024-07-02 11:15:17 +01:00
|
|
|
/**
|
|
|
|
* @brief Removes the last part of a path
|
|
|
|
*
|
2024-07-06 09:03:13 +01:00
|
|
|
* @param szPath Pointer to the path string
|
2024-07-02 11:15:17 +01:00
|
|
|
*/
|
2024-06-28 03:21:38 +01:00
|
|
|
void remove_name_in_path(char *szPath)
|
|
|
|
{
|
|
|
|
char *p = szPath;
|
|
|
|
|
|
|
|
while (*p++)
|
|
|
|
;
|
|
|
|
while (p > szPath && *p != '\\')
|
|
|
|
p--;
|
|
|
|
if (*p == '\\')
|
|
|
|
*p = '\0';
|
|
|
|
}
|
2024-06-27 19:18:28 +01:00
|
|
|
|
2024-06-29 03:05:51 +01:00
|
|
|
/**
|
|
|
|
* @brief replaces multiple newlines and tabs with single spaces
|
|
|
|
*
|
2024-07-06 09:03:13 +01:00
|
|
|
* @param s The tring to be reduced
|
|
|
|
* @param len Current length of the string
|
|
|
|
* @return int New length of the string
|
2024-06-29 03:05:51 +01:00
|
|
|
*/
|
|
|
|
int replace_multiple_space_with_one(char *s, size_t len)
|
2024-06-27 19:18:28 +01:00
|
|
|
{
|
|
|
|
int j = 0;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
if (len == 1 && (s[0] == ' ' || s[0] == '\t'))
|
|
|
|
{
|
|
|
|
s[0] = '\0';
|
2024-06-29 03:05:51 +01:00
|
|
|
return len;
|
2024-06-27 19:18:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len < 2)
|
2024-06-29 03:05:51 +01:00
|
|
|
return len;
|
2024-06-27 19:18:28 +01:00
|
|
|
|
|
|
|
for (int i = 0; s[i] != '\0'; i++)
|
|
|
|
{
|
|
|
|
if (s[i] == ' ' || s[i] == '\t')
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s[i] != ' ' && s[i] != '\t')
|
|
|
|
{
|
|
|
|
if (count >= 1)
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
s[j++] = ' ';
|
|
|
|
}
|
|
|
|
s[j++] = s[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s[j] = '\0';
|
2024-06-29 03:05:51 +01:00
|
|
|
return j;
|
2024-06-28 03:21:38 +01:00
|
|
|
}
|
|
|
|
|
2024-07-02 11:15:17 +01:00
|
|
|
/**
|
|
|
|
* @brief
|
|
|
|
*
|
|
|
|
* @param s Pointer to a character buffer
|
|
|
|
* @param kind The kind of Voicemeeter.
|
2024-07-06 09:03:13 +01:00
|
|
|
* @param n Maximum number of characters to be written to the buffer
|
2024-07-02 11:15:17 +01:00
|
|
|
* @return char* The kind of Voicemeeter as a string
|
|
|
|
*/
|
2024-07-03 15:24:44 +01:00
|
|
|
char *kind_as_string(char *s, int kind, int n)
|
2024-06-29 03:05:51 +01:00
|
|
|
{
|
2024-07-03 17:52:08 +01:00
|
|
|
static const char *kinds[] = {
|
2024-06-29 03:05:51 +01:00
|
|
|
"Basic",
|
|
|
|
"Banana",
|
|
|
|
"Potato",
|
|
|
|
"Basic x64",
|
|
|
|
"Banana x64",
|
|
|
|
"Potato x64",
|
|
|
|
};
|
|
|
|
snprintf(s, n, kinds[kind - 1]);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief returns Voicemeeter's version as a string
|
|
|
|
*
|
2024-07-06 09:03:13 +01:00
|
|
|
* @param s Pointer to a character buffer
|
|
|
|
* @param v Unprocessed version as a long int
|
|
|
|
* @param n Maximum number of characters to be written to the buffer
|
|
|
|
* @return char* Pointer to a character buffer
|
2024-06-29 03:05:51 +01:00
|
|
|
*/
|
2024-06-28 03:21:38 +01:00
|
|
|
char *version_as_string(char *s, long v, int n)
|
|
|
|
{
|
|
|
|
long v1 = (v & 0xFF000000) >> 24,
|
|
|
|
v2 = (v & 0x00FF0000) >> 16,
|
|
|
|
v3 = (v & 0x0000FF00) >> 8,
|
|
|
|
v4 = (v & 0x000000FF);
|
|
|
|
snprintf(s, n, "%i.%i.%i.%i", (int)v1, (int)v2, (int)v3, (int)v4);
|
|
|
|
return s;
|
2024-06-27 19:18:28 +01:00
|
|
|
}
|