Move xAsprintf, xSnprintf and xStrdup to StringUtils.h

This commit is contained in:
Benny Baumann 2020-09-19 20:22:34 +02:00
parent 7cd093ce95
commit c6f04a9c5d
22 changed files with 101 additions and 75 deletions

View File

@ -5,14 +5,17 @@ Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/
#include "AffinityPanel.h"
#include "CRT.h"
#include "config.h"
#include "Vector.h"
#include "AffinityPanel.h"
#include <assert.h>
#include <string.h>
#include "CRT.h"
#include "StringUtils.h"
#include "Vector.h"
#ifdef HAVE_LIBHWLOC
#include <hwloc.h>
#endif

View File

@ -6,16 +6,17 @@ in the source distribution for its full text.
*/
#include "AvailableColumnsPanel.h"
#include "Platform.h"
#include "Header.h"
#include "ColumnsPanel.h"
#include <assert.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "ColumnsPanel.h"
#include "Header.h"
#include "Platform.h"
#include "StringUtils.h"
static const char* const AvailableColumnsFunctions[] = {" ", " ", " ", " ", "Add ", " ", " ", " ", " ", "Done ", NULL};

View File

@ -12,6 +12,7 @@ in the source distribution for its full text.
#include "Header.h"
#include "ListItem.h"
#include "Platform.h"
#include "StringUtils.h"
#include <assert.h>
#include <stdlib.h>

View File

@ -10,6 +10,7 @@ in the source distribution for its full text.
#include "CRT.h"
#include "Settings.h"
#include "Platform.h"
#include "StringUtils.h"
#include <assert.h>
#include <stdlib.h>

View File

@ -9,6 +9,7 @@ in the source distribution for its full text.
#include "CRT.h"
#include "CheckItem.h"
#include "StringUtils.h"
#include <assert.h>
#include <stdlib.h>

View File

@ -11,6 +11,7 @@ in the source distribution for its full text.
#include "CRT.h"
#include "Platform.h"
#include "StringUtils.h"
static const int DiskIOMeter_attributes[] = {

View File

@ -7,13 +7,14 @@ in the source distribution for its full text.
#include "DisplayOptionsPanel.h"
#include "CheckItem.h"
#include "CRT.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "CheckItem.h"
#include "CRT.h"
#include "StringUtils.h"
static const char* const DisplayOptionsFunctions[] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", "Done ", NULL};

View File

@ -8,6 +8,7 @@ in the source distribution for its full text.
#include "FunctionBar.h"
#include "CRT.h"
#include "RichString.h"
#include "StringUtils.h"
#include "XAlloc.h"
#include <assert.h>

View File

@ -9,6 +9,7 @@ in the source distribution for its full text.
#include "CRT.h"
#include "Platform.h"
#include "StringUtils.h"
static const int LoadAverageMeter_attributes[] = {

View File

@ -7,16 +7,16 @@ in the source distribution for its full text.
#include "Panel.h"
#include "SignalsPanel.h"
#include "Platform.h"
#include <assert.h>
#include <ctype.h>
#include <signal.h>
#include <stdlib.h>
#include "ListItem.h"
#include "Platform.h"
#include "RichString.h"
#include <stdlib.h>
#include <assert.h>
#include <signal.h>
#include <ctype.h>
#include "StringUtils.h"
Panel* SignalsPanel_new() {

View File

@ -5,14 +5,17 @@ Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/
#include "StringUtils.h"
#include "XAlloc.h"
#include "config.h"
#include "StringUtils.h"
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include "XAlloc.h"
char* String_cat(const char* s1, const char* s2) {
int l1 = strlen(s1);
@ -140,3 +143,37 @@ char* String_readLine(FILE* fd) {
at = buffer + bufSize - step;
}
}
int xAsprintf(char** strp, const char* fmt, ...) {
va_list vl;
va_start(vl, fmt);
int r = vasprintf(strp, fmt, vl);
va_end(vl);
if (r < 0 || !*strp) {
fail();
}
return r;
}
int xSnprintf(char* buf, int len, const char* fmt, ...) {
va_list vl;
va_start(vl, fmt);
int n = vsnprintf(buf, len, fmt, vl);
va_end(vl);
if (n < 0 || n >= len) {
fail();
}
return n;
}
char* xStrdup(const char* str) {
char* data = strdup(str);
if (!data) {
fail();
}
return data;
}

View File

@ -9,6 +9,9 @@ in the source distribution for its full text.
#include <stdio.h>
#include "Macros.h"
#define String_startsWith(s, match) (strncmp((s),(match),strlen(match)) == 0)
#define String_contains_i(s1, s2) (strcasestr(s1, s2) != NULL)
@ -31,4 +34,12 @@ char* String_getToken(const char* line, const unsigned short int numMatch);
char* String_readLine(FILE* fd);
ATTR_FORMAT(printf, 2, 3)
int xAsprintf(char **strp, const char* fmt, ...);
ATTR_FORMAT(printf, 3, 4)
int xSnprintf(char *buf, int len, const char* fmt, ...);
char* xStrdup(const char* str) ATTR_NONNULL;
#endif

View File

@ -7,8 +7,9 @@ in the source distribution for its full text.
#include "TasksMeter.h"
#include "Platform.h"
#include "CRT.h"
#include "Platform.h"
#include "StringUtils.h"
static const int TasksMeter_attributes[] = {

View File

@ -6,8 +6,10 @@ in the source distribution for its full text.
*/
#include "UptimeMeter.h"
#include "Platform.h"
#include "CRT.h"
#include "Platform.h"
#include "StringUtils.h"
static const int UptimeMeter_attributes[] = {

View File

@ -5,11 +5,10 @@ Released under the GNU GPLv2, see the COPYING file
in the source distribution for its full text.
*/
#include "UsersTable.h"
#include "XAlloc.h"
#include "config.h"
#include "UsersTable.h"
#include <stdio.h>
#include <string.h>
#include <strings.h>
@ -18,6 +17,9 @@ in the source distribution for its full text.
#include <stdlib.h>
#include <assert.h>
#include "StringUtils.h"
#include "XAlloc.h"
UsersTable* UsersTable_new() {
UsersTable* this;

View File

@ -40,37 +40,3 @@ void* xRealloc(void* ptr, size_t size) {
}
return data;
}
int xAsprintf(char** strp, const char* fmt, ...) {
va_list vl;
va_start(vl, fmt);
int _r = vasprintf(strp, fmt, vl);
va_end(vl);
if (_r < 0) {
fail();
}
return _r;
}
int xSnprintf(char* buf, int len, const char* fmt, ...) {
va_list vl;
va_start(vl, fmt);
int _n=vsnprintf(buf, len, fmt, vl);
va_end(vl);
if (!(_n > -1 && _n < len)) {
fail();
}
return _n;
}
char* xStrdup(const char* str) {
char* data = strdup(str);
if (!data) {
fail();
}
return data;
}

View File

@ -19,12 +19,4 @@ void* xCalloc(size_t nmemb, size_t size);
void* xRealloc(void* ptr, size_t size);
ATTR_FORMAT(printf, 2, 3)
int xAsprintf(char **strp, const char* fmt, ...);
ATTR_FORMAT(printf, 3, 4)
int xSnprintf(char *buf, int len, const char* fmt, ...);
char* xStrdup(const char* str) ATTR_NONNULL;
#endif

1
htop.c
View File

@ -16,6 +16,7 @@ in the source distribution for its full text.
#include "ProcessList.h"
#include "ScreenManager.h"
#include "Settings.h"
#include "StringUtils.h"
#include "UsersTable.h"
#include "Platform.h"

View File

@ -7,6 +7,8 @@ in the source distribution for its full text.
#include "IOPriorityPanel.h"
#include "StringUtils.h"
Panel* IOPriorityPanel_new(IOPriority currPrio) {
Panel* this = Panel_new(1, 1, 1, 1, true, Class(ListItem), FunctionBar_newEnterEsc("Set ", "Cancel "));

View File

@ -9,8 +9,9 @@ in the source distribution for its full text.
#include "Process.h"
#include "ProcessList.h"
#include "LinuxProcess.h"
#include "Platform.h"
#include "CRT.h"
#include "Platform.h"
#include "StringUtils.h"
#include <stdlib.h>
#include <unistd.h>

View File

@ -7,14 +7,13 @@ in the source distribution for its full text.
*/
#include "PressureStallMeter.h"
#include "Platform.h"
#include "CRT.h"
#include <string.h>
/*{
#include "Meter.h"
}*/
#include "CRT.h"
#include "Platform.h"
#include "StringUtils.h"
static const int PressureStallMeter_attributes[] = {
PRESSURE_STALL_TEN, PRESSURE_STALL_SIXTY, PRESSURE_STALL_THREEHUNDRED

View File

@ -10,6 +10,7 @@ in the source distribution for its full text.
#include "CRT.h"
#include "Platform.h"
#include "StringUtils.h"
#include <stdlib.h>
#include <string.h>