summaryrefslogtreecommitdiff
path: root/strings/my_vsnprintf.c
diff options
context:
space:
mode:
authorunknown <monty@mashka.mysql.fi>2003-01-14 14:28:36 +0200
committerunknown <monty@mashka.mysql.fi>2003-01-14 14:28:36 +0200
commit910c125bb6528b08f7c30b7b75650076b7b953fe (patch)
tree7a63ba29620ada893aa14d1984554e0af0a0deaa /strings/my_vsnprintf.c
parent91f18f0a26e3714bd9edc78b1c6fe7a1fd243167 (diff)
downloadmariadb-git-910c125bb6528b08f7c30b7b75650076b7b953fe.tar.gz
Updates for multi-byte character sets
(Note: test 'union' fails, but Sanja promised to fix this) include/m_ctype.h: Changed prototype for strntod() to inform the user that source may be modified. include/m_string.h: Moved my_vsnprintf to strings library include/my_sys.h: Moved my_vsnprintf to strings library libmysql/Makefile.shared: Moved my_vsnprintf to strings library mysql-test/r/alter_table.result: Moved my_vsnprintf to strings library mysql-test/r/create.result: Moved my_vsnprintf to strings library mysql-test/r/ctype_many.result: Moved my_vsnprintf to strings library mysql-test/r/fulltext.result: Moved my_vsnprintf to strings library mysql-test/r/innodb.result: Moved my_vsnprintf to strings library mysql-test/r/merge.result: Moved my_vsnprintf to strings library mysql-test/r/select.result: Moved my_vsnprintf to strings library mysql-test/r/show_check.result: Moved my_vsnprintf to strings library mysql-test/r/type_blob.result: Moved my_vsnprintf to strings library mysql-test/r/type_enum.result: Moved my_vsnprintf to strings library mysql-test/r/type_ranges.result: Moved my_vsnprintf to strings library mysql-test/r/type_set.result: Moved my_vsnprintf to strings library mysys/Makefile.am: Moved my_vsnprintf to strings library sql/field.cc: Fixed for character set handling sql/field.h: Fixed for character set handling sql/item.cc: Fixed for character set handling sql/item.h: Fixed for character set handling sql/item_func.cc: Fixed for character set handling sql/item_func.h: Fixed for character set handling sql/item_strfunc.cc: Fixed for character set handling sql/item_sum.cc: Fixed for character set handling sql/item_sum.h: Fixed for character set handling sql/item_timefunc.cc: Fixed for character set handling sql/mysqld.cc: Update to use new test_if_int() sql/opt_range.cc: Fixed for character set handling sql/procedure.h: Fixed for character set handling sql/sql_class.cc: Fixed for character set handling sql/sql_string.cc: Added multi byte support to append. Added set_latin1() sql/sql_string.h: Added set_latin1() sql/sql_update.cc: Cosmetic changes strings/Makefile.am: Moved my_vsnprintf to strings library strings/ctype-simple.c: Code review + cleanup strings/ctype-utf8.c: Fixed strntod() strings/my_vsnprintf.c: Added support for %#d and %#u
Diffstat (limited to 'strings/my_vsnprintf.c')
-rw-r--r--strings/my_vsnprintf.c166
1 files changed, 166 insertions, 0 deletions
diff --git a/strings/my_vsnprintf.c b/strings/my_vsnprintf.c
new file mode 100644
index 00000000000..a67523af7bc
--- /dev/null
+++ b/strings/my_vsnprintf.c
@@ -0,0 +1,166 @@
+/* Copyright (C) 2000 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+
+#include <my_global.h>
+#include <m_string.h>
+#include <stdarg.h>
+#include <m_ctype.h>
+#include <assert.h>
+
+/*
+ Limited snprintf() implementations
+
+ IMPLEMENTION:
+ Supports following formats:
+ %#d
+ %#u
+ %#.#s Note #.# is skiped
+
+ RETURN
+ length of result string
+*/
+
+int my_snprintf(char* to, size_t n, const char* fmt, ...)
+{
+ va_list args;
+ int result;
+ va_start(args,fmt);
+ result= my_vsnprintf(to, n, fmt, args);
+ va_end(args);
+ return result;
+}
+
+
+int my_vsnprintf(char *to, size_t n, const char* fmt, va_list ap)
+{
+ char *start=to, *end=to+n-1;
+ uint length, num_state, pre_zero;
+
+ for (; *fmt ; fmt++)
+ {
+ if (fmt[0] != '%')
+ {
+ if (to == end) /* End of buffer */
+ break;
+ *to++= *fmt; /* Copy ordinary char */
+ continue;
+ }
+ fmt++; /* skip '%' */
+ /* Read max fill size (only used with %d and %u) */
+ if (*fmt == '-')
+ fmt++;
+ length= num_state= pre_zero= 0;
+ for (;; fmt++)
+ {
+ if (my_isdigit(system_charset_info,*fmt))
+ {
+ if (!num_state)
+ {
+ length=length*10+ (uint) (*fmt-'0');
+ if (!length)
+ pre_zero= 1; /* first digit was 0 */
+ }
+ continue;
+ }
+ if (*fmt != '.' || num_state)
+ break;
+ num_state= 1;
+ }
+ if (*fmt == 'l')
+ fmt++;
+ if (*fmt == 's') /* String parameter */
+ {
+ reg2 char *par = va_arg(ap, char *);
+ uint plen,left_len = (uint)(end-to);
+ if (!par) par = (char*)"(null)";
+ plen = (uint) strlen(par);
+ if (left_len <= plen)
+ plen = left_len - 1;
+ to=strnmov(to,par,plen);
+ continue;
+ }
+ else if (*fmt == 'd' || *fmt == 'u') /* Integer parameter */
+ {
+ register int iarg;
+ char *to_start= to;
+ if ((uint) (end-to) < max(16,length))
+ break;
+ iarg = va_arg(ap, int);
+ if (*fmt == 'd')
+ to=int10_to_str((long) iarg,to, -10);
+ else
+ to=int10_to_str((long) (uint) iarg,to,10);
+ /* If %#d syntax was used, we have to pre-zero/pre-space the string */
+ if (length)
+ {
+ uint res_length= (uint) (to - to_start);
+ if (res_length < length)
+ {
+ uint diff= (length- res_length);
+ bmove_upp(to+diff, to, res_length);
+ bfill(to-res_length, diff, pre_zero ? '0' : ' ');
+ to+= diff;
+ }
+ }
+ continue;
+ }
+ /* We come here on '%%', unknown code or too long parameter */
+ if (to == end)
+ break;
+ *to++='%'; /* % used as % or unknown code */
+ }
+ DBUG_ASSERT(to <= end);
+ *to='\0'; /* End of errmessage */
+ return (uint) (to - start);
+}
+
+#ifdef MAIN
+#define OVERRUN_SENTRY 250
+static void my_printf(const char * fmt, ...)
+{
+ char buf[33];
+ int n;
+ va_list ar;
+ va_start(ar, fmt);
+ buf[sizeof(buf)-1]=OVERRUN_SENTRY;
+ n = my_vsnprintf(buf, sizeof(buf)-1,fmt, ar);
+ printf(buf);
+ printf("n=%d, strlen=%d\n", n, strlen(buf));
+ if (buf[sizeof(buf)-1] != OVERRUN_SENTRY)
+ {
+ fprintf(stderr, "Buffer overrun\n");
+ abort();
+ }
+ va_end(ar);
+}
+
+int main()
+{
+
+ my_printf("Hello\n");
+ my_printf("Hello int, %d\n", 1);
+ my_printf("Hello string '%s'\n", "I am a string");
+ my_printf("Hello hack hack hack hack hack hack hack %d\n", 1);
+ my_printf("Hello %d hack %d\n", 1, 4);
+ my_printf("Hello %d hack hack hack hack hack %d\n", 1, 4);
+ my_printf("Hello '%s' hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\n", "hack");
+ my_printf("Hello hhhhhhhhhhhhhh %d sssssssssssssss\n", 1);
+ my_printf("Hello %u\n", 1);
+ my_printf("conn %ld to: '%-.64s' user: '%-.32s' host:\
+ `%-.64s' (%-.64s)", 1, 0,0,0,0);
+ return 0;
+}
+#endif