[Freeciv-Dev] patch: end_of_strn(), cat_snprintf() (PR#212)
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
I started a patch to make extensive use of my_snprintf()
(replacing existing use of sprintf); this is not that patch,
but two convenience functions I wrote to make the patch
easier: end_of_strn(), and cat_snprintf(). (See descriptions
in comments in patch.)
-- David
diff -u -r --exclude-from exclude freeciv-mod/common/shared.c
fc-adv/common/shared.c
--- freeciv-mod/common/shared.c Tue Dec 28 18:47:37 1999
+++ fc-adv/common/shared.c Tue Dec 28 20:00:56 1999
@@ -44,6 +44,7 @@
#include "fcintl.h"
#include "log.h"
#include "mem.h"
+#include "support.h"
#include "shared.h"
@@ -398,6 +399,58 @@
}
}
return num_lines;
+}
+
+/***************************************************************************
+ Returns pointer to '\0' at end of string 'str', and decrements
+ *nleft by the length of 'str'. This is intended to be useful to
+ allow strcat-ing without traversing the whole string each time,
+ while still keeping track of the buffer length.
+ Eg:
+ char buf[128];
+ int n = sizeof(buf);
+ char *p = buf;
+
+ my_snprintf(p, n, "foo%p", p);
+ p = end_of_strn(p, &n);
+ mystrlcpy(p, "yyy", n);
+***************************************************************************/
+char *end_of_strn(char *str, int *nleft)
+{
+ int len = strlen(str);
+ *nleft -= len;
+ assert((*nleft)>0); /* space for the terminating nul */
+ return str + len;
+}
+
+/**********************************************************************
+ cat_snprintf is like a combination of my_snprintf and mystrlcat;
+ it does snprintf to the end of an existing string.
+
+ Like mystrlcat, n is the total length available for str, including
+ existing contents and trailing nul. If there is no extra room
+ available in str, does not change the string.
+
+ Also like mystrlcat, returns the final length that str would have
+ had without truncation. Ie, if return is >= n, truncation occured.
+**********************************************************************/
+int cat_snprintf(char *str, size_t n, const char *format, ...)
+{
+ size_t len;
+ int ret;
+ va_list ap;
+
+ assert(format);
+ assert(str);
+ assert(n>0);
+
+ len = strlen(str);
+ assert(len < n);
+
+ va_start(ap, format);
+ ret = my_vsnprintf(str+len, n-len, format, ap);
+ va_end(ap);
+ return ret + len;
}
/***************************************************************************
diff -u -r --exclude-from exclude freeciv-mod/common/shared.h
fc-adv/common/shared.h
--- freeciv-mod/common/shared.h Tue Dec 28 18:47:37 1999
+++ fc-adv/common/shared.h Tue Dec 28 19:58:14 1999
@@ -13,6 +13,10 @@
#ifndef FC__SHARED_H
#define FC__SHARED_H
+#include <stdlib.h> /* size_t */
+
+#include "attribute.h"
+
/* Note: the capability string is now in capstr.c --dwp */
/* Version stuff is now in version.h --dwp */
@@ -64,6 +68,10 @@
void remove_trailing_spaces(char *s);
void remove_trailing_char(char *s, char trailing);
int wordwrap_string(char *s, int len);
+
+char *end_of_strn(char *str, int *nleft);
+int cat_snprintf(char *str, size_t n, const char *format, ...)
+ fc__attribute((format (printf, 3, 4)));
char *user_home_dir(void);
char *user_username(void);
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Freeciv-Dev] patch: end_of_strn(), cat_snprintf() (PR#212),
David Pfitzner <=
|
|