Process Hacker
mxml-string.c
Go to the documentation of this file.
1 /*
2  * "$Id: mxml-string.c 387 2009-04-18 17:05:52Z mike $"
3  *
4  * String functions for Mini-XML, a small XML-like file parsing library.
5  *
6  * Copyright 2003-2009 by Michael Sweet.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * Contents:
19  *
20  * _mxml_snprintf() - Format a string.
21  * _mxml_strdup() - Duplicate a string.
22  * _mxml_strdupf() - Format and duplicate a string.
23  * _mxml_vsnprintf() - Format a string into a fixed size buffer.
24  * _mxml_vstrdupf() - Format and duplicate a string.
25  */
26 
27 /*
28  * Include necessary headers...
29  */
30 
31 #include <phbase.h>
32 #include "config.h"
33 
34 /*
35  * '_mxml_strdupf()' - Format and duplicate a string.
36  */
37 
38 char * /* O - New string pointer */
39 _mxml_strdupf(const char *format, /* I - Printf-style format string */
40  ...) /* I - Additional arguments as needed */
41 {
42  va_list ap; /* Pointer to additional arguments */
43  char *s; /* Pointer to formatted string */
44 
45 
46  /*
47  * Get a pointer to the additional arguments, format the string,
48  * and return it...
49  */
50 
51  va_start(ap, format);
52  s = _mxml_vstrdupf(format, ap);
53  va_end(ap);
54 
55  return (s);
56 }
57 
58 /*
59  * '_mxml_vstrdupf()' - Format and duplicate a string.
60  */
61 
62 char * /* O - New string pointer */
63 _mxml_vstrdupf(const char *format, /* I - Printf-style format string */
64  va_list ap) /* I - Pointer to additional arguments */
65 {
66  int bytes; /* Number of bytes required */
67  char *buffer, /* String buffer */
68  temp[256]; /* Small buffer for first vsnprintf */
69 
70 
71  /*
72  * First format with a tiny buffer; this will tell us how many bytes are
73  * needed...
74  */
75 
76  bytes = _vsnprintf(temp, sizeof(temp), format, ap);
77 
78  if (bytes < sizeof(temp))
79  {
80  /*
81  * Hey, the formatted string fits in the tiny buffer, so just dup that...
82  */
83 
84  return (PhDuplicateBytesZSafe(temp));
85  }
86 
87  /*
88  * Allocate memory for the whole thing and reformat to the new, larger
89  * buffer...
90  */
91 
92  if ((buffer = PhAllocateExSafe(bytes + 1, HEAP_ZERO_MEMORY)) != NULL)
93  _vsnprintf(buffer, bytes + 1, format, ap);
94 
95  /*
96  * Return the new string...
97  */
98 
99  return (buffer);
100 }
101 
102 /*
103  * End of "$Id: mxml-string.c 387 2009-04-18 17:05:52Z mike $".
104  */