]> err.no Git - sope/blob - sope-core/NGExtensions/NGExtensions/NSString+Formatting.h
Add libxml2-dev to libsope-xml4.7-dev deps
[sope] / sope-core / NGExtensions / NGExtensions / NSString+Formatting.h
1 /*
2   Copyright (C) 2000-2007 SKYRIX Software AG
3   Copyright (C) 2007      Helge Hess
4
5   This file is part of SOPE.
6
7   SOPE is free software; you can redistribute it and/or modify it under
8   the terms of the GNU Lesser General Public License as published by the
9   Free Software Foundation; either version 2, or (at your option) any
10   later version.
11
12   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
13   WARRANTY; without even the implied warranty of MERCHANTABILITY or
14   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15   License for more details.
16
17   You should have received a copy of the GNU Lesser General Public
18   License along with SOPE; see the file COPYING.  If not, write to the
19   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20   02111-1307, USA.
21 */
22
23 #ifndef __NGExtensions_NSString_Formatting_H__
24 #define __NGExtensions_NSString_Formatting_H__
25
26 #import <Foundation/NSString.h>
27
28 // category
29
30 @interface NSString(XSFormatting)
31
32 + (id)stringWithCFormat:(const char *)_format arguments:(va_list)_ap;
33 + (id)stringWithCFormat:(const char *)_format, ...;
34
35 @end
36
37 @interface NSMutableString(XSFormatting)
38
39 - (void)appendFormat:(NSString *)_format arguments:(va_list)_ap;
40 - (void)appendFormat:(NSString *)_format, ...;
41
42 @end
43
44 // C support functions
45
46 static inline int 
47 xs_vsnprintf(char *_str, size_t max, const char *fmt, va_list _ap) 
48 {
49   NSString *obj = [NSString stringWithCFormat:_str arguments:_ap];
50 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
51   [obj getCString:_str maxLength:(max - 1)
52        encoding:[NSString defaultCStringEncoding]];
53   return strlen(_str);
54 #else
55   [obj getCString:_str maxLength:(max - 1)];
56   return [obj cStringLength]; // return the len the string would have consumed
57 #endif
58 }
59
60 static inline int xs_vsprintf (char *_str, const char *_fmt, va_list _ap) {
61   NSString *obj = [NSString stringWithCFormat:_str arguments:_ap];
62 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
63   [obj getCString:_str maxLength:65535 /* no limit ... */
64        encoding:[NSString defaultCStringEncoding]];
65   return strlen(_str);
66 #else
67   [obj getCString:_str];
68   return [obj cStringLength]; // return the length of the string
69 #endif
70 }
71
72 /*
73   Could use formats ..
74      //     __attribute__ ((format (printf, 2, 3)));
75      //     __attribute__ ((format (printf, 3, 4)));
76 */
77 int xs_sprintf (char *str, const char *format, ...);
78 int xs_snprintf(char *str, size_t size, const char *format, ...);
79
80 #endif /* __NGExtensions_NSString_Formatting_H__ */