os: move out Format*() functions

Move out the Format*() functions to separate source and header.

These aren't exported, thus no module API change.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
Part-of: <https://gitlab.freedesktop.org/xorg/xserver/-/merge_requests/1312>
This commit is contained in:
Enrico Weigelt, metux IT consult 2024-02-18 12:40:42 +01:00 committed by Marge Bot
parent 040e41c7e9
commit 02ba47f09d
7 changed files with 122 additions and 94 deletions

View File

@ -236,10 +236,6 @@ padding_for_int32(const int bytes)
extern _X_EXPORT char **xstrtokenize(const char *str, const char *separators); extern _X_EXPORT char **xstrtokenize(const char *str, const char *separators);
extern void FormatInt64(int64_t num, char *string);
extern void FormatUInt64(uint64_t num, char *string);
extern void FormatUInt64Hex(uint64_t num, char *string);
extern void FormatDouble(double dbl, char *string);
/** /**
* Compare the two version numbers comprising of major.minor. * Compare the two version numbers comprising of major.minor.

101
os/fmt.c Normal file
View File

@ -0,0 +1,101 @@
/* SPDX-License-Identifier: MIT OR X11
*
* Copyright © 2024 Enrico Weigelt, metux IT consult <info@metux.net>
* Copyright © 1987, 1998 The Open Group
* Copyright © 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
* Copyright © 1994 Quarterdeck Office Systems.
*/
#include <stdint.h>
#include "os/fmt.h"
/* Format a signed number into a string in a signal safe manner. The string
* should be at least 21 characters in order to handle all int64_t values.
*/
void
FormatInt64(int64_t num, char *string)
{
if (num < 0) {
string[0] = '-';
num *= -1;
string++;
}
FormatUInt64(num, string);
}
/* Format a number into a string in a signal safe manner. The string should be
* at least 21 characters in order to handle all uint64_t values. */
void
FormatUInt64(uint64_t num, char *string)
{
uint64_t divisor;
int len;
int i;
for (len = 1, divisor = 10;
len < 20 && num / divisor;
len++, divisor *= 10);
for (i = len, divisor = 1; i > 0; i--, divisor *= 10)
string[i - 1] = '0' + ((num / divisor) % 10);
string[len] = '\0';
}
/**
* Format a double number as %.2f.
*/
void
FormatDouble(double dbl, char *string)
{
int slen = 0;
uint64_t frac;
frac = (dbl > 0 ? dbl : -dbl) * 100.0 + 0.5;
frac %= 100;
/* write decimal part to string */
if (dbl < 0 && dbl > -1)
string[slen++] = '-';
FormatInt64((int64_t)dbl, &string[slen]);
while(string[slen] != '\0')
slen++;
/* append fractional part, but only if we have enough characters. We
* expect string to be 21 chars (incl trailing \0) */
if (slen <= 17) {
string[slen++] = '.';
if (frac < 10)
string[slen++] = '0';
FormatUInt64(frac, &string[slen]);
}
}
/* Format a number into a hexadecimal string in a signal safe manner. The string
* should be at least 17 characters in order to handle all uint64_t values. */
void
FormatUInt64Hex(uint64_t num, char *string)
{
uint64_t divisor;
int len;
int i;
for (len = 1, divisor = 0x10;
len < 16 && num / divisor;
len++, divisor *= 0x10);
for (i = len, divisor = 1; i > 0; i--, divisor *= 0x10) {
int val = (num / divisor) % 0x10;
if (val < 10)
string[i - 1] = '0' + val;
else
string[i - 1] = 'a' + val - 10;
}
string[len] = '\0';
}

15
os/fmt.h Normal file
View File

@ -0,0 +1,15 @@
/* SPDX-License-Identifier: MIT OR X11
*
* Copyright © 2024 Enrico Weigelt, metux IT consult <info@metux.net>
*/
#ifndef _XSERVER_OS_FMT_H
#define _XSERVER_OS_FMT_H
#include <stdint.h>
void FormatInt64(int64_t num, char *string);
void FormatUInt64(uint64_t num, char *string);
void FormatUInt64Hex(uint64_t num, char *string);
void FormatDouble(double dbl, char *string);
#endif /* _XSERVER_OS_FMT_H */

View File

@ -87,6 +87,8 @@ OR PERFORMANCE OF THIS SOFTWARE.
#include <stdlib.h> /* for malloc() */ #include <stdlib.h> /* for malloc() */
#include <errno.h> #include <errno.h>
#include "os/fmt.h"
#include "input.h" #include "input.h"
#include "opaque.h" #include "opaque.h"
#include "osdep.h" #include "osdep.h"

View File

@ -6,6 +6,7 @@ srcs_os = [
'backtrace.c', 'backtrace.c',
'client.c', 'client.c',
'connection.c', 'connection.c',
'fmt.c',
'inputthread.c', 'inputthread.c',
'io.c', 'io.c',
'mitauth.c', 'mitauth.c',

View File

@ -1850,96 +1850,6 @@ CheckUserAuthorization(void)
#endif #endif
} }
/* Format a signed number into a string in a signal safe manner. The string
* should be at least 21 characters in order to handle all int64_t values.
*/
void
FormatInt64(int64_t num, char *string)
{
if (num < 0) {
string[0] = '-';
num *= -1;
string++;
}
FormatUInt64(num, string);
}
/* Format a number into a string in a signal safe manner. The string should be
* at least 21 characters in order to handle all uint64_t values. */
void
FormatUInt64(uint64_t num, char *string)
{
uint64_t divisor;
int len;
int i;
for (len = 1, divisor = 10;
len < 20 && num / divisor;
len++, divisor *= 10);
for (i = len, divisor = 1; i > 0; i--, divisor *= 10)
string[i - 1] = '0' + ((num / divisor) % 10);
string[len] = '\0';
}
/**
* Format a double number as %.2f.
*/
void
FormatDouble(double dbl, char *string)
{
int slen = 0;
uint64_t frac;
frac = (dbl > 0 ? dbl : -dbl) * 100.0 + 0.5;
frac %= 100;
/* write decimal part to string */
if (dbl < 0 && dbl > -1)
string[slen++] = '-';
FormatInt64((int64_t)dbl, &string[slen]);
while(string[slen] != '\0')
slen++;
/* append fractional part, but only if we have enough characters. We
* expect string to be 21 chars (incl trailing \0) */
if (slen <= 17) {
string[slen++] = '.';
if (frac < 10)
string[slen++] = '0';
FormatUInt64(frac, &string[slen]);
}
}
/* Format a number into a hexadecimal string in a signal safe manner. The string
* should be at least 17 characters in order to handle all uint64_t values. */
void
FormatUInt64Hex(uint64_t num, char *string)
{
uint64_t divisor;
int len;
int i;
for (len = 1, divisor = 0x10;
len < 16 && num / divisor;
len++, divisor *= 0x10);
for (i = len, divisor = 1; i > 0; i--, divisor *= 0x10) {
int val = (num / divisor) % 0x10;
if (val < 10)
string[i - 1] = '0' + val;
else
string[i - 1] = 'a' + val - 10;
}
string[len] = '\0';
}
#if !defined(WIN32) || defined(__CYGWIN__) #if !defined(WIN32) || defined(__CYGWIN__)
/* Move a file descriptor out of the way of our select mask; this /* Move a file descriptor out of the way of our select mask; this
* is useful for file descriptors which will never appear in the * is useful for file descriptors which will never appear in the

View File

@ -30,6 +30,9 @@
#include <stdint.h> #include <stdint.h>
#include <unistd.h> #include <unistd.h>
#include "os/fmt.h"
#include "assert.h" #include "assert.h"
#include "misc.h" #include "misc.h"