| 1 | /* |
|---|
| 2 | * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com> |
|---|
| 3 | * |
|---|
| 4 | * This library is free software; you can redistribute it and/or modify |
|---|
| 5 | * it under the terms of the GNU Lesser General Public License as published by |
|---|
| 6 | * the Free Software Foundation; either version 2.1 of the License, or |
|---|
| 7 | * (at your option) any later version. |
|---|
| 8 | * |
|---|
| 9 | * This library is distributed in the hope that it will be useful, |
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | * GNU Lesser General Public License for more details. |
|---|
| 13 | * |
|---|
| 14 | * You should have received a copy of the GNU Lesser General Public License |
|---|
| 15 | * along with this library; if not, write to the Free Software |
|---|
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include "iostream.h" |
|---|
| 20 | #include "sys_string.h" |
|---|
| 21 | |
|---|
| 22 | /** Print on a stream, like vfprintf(). |
|---|
| 23 | * |
|---|
| 24 | * @param stream to print to |
|---|
| 25 | * @param format for the print (as fprintf()) |
|---|
| 26 | * @param args arguments to print |
|---|
| 27 | * @return result code from the print |
|---|
| 28 | */ |
|---|
| 29 | int IOStream_vprint(IOStream *stream, const char *format, va_list args){ |
|---|
| 30 | char buffer[1024]; |
|---|
| 31 | int k = sizeof(buffer), n; |
|---|
| 32 | |
|---|
| 33 | n = vsnprintf(buffer, k, (char*)format, args); |
|---|
| 34 | if(n < 0 || n > k ){ |
|---|
| 35 | n = k; |
|---|
| 36 | } |
|---|
| 37 | n = IOStream_write(stream, buffer, n); |
|---|
| 38 | return n; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | /** Print on a stream, like fprintf(). |
|---|
| 42 | * |
|---|
| 43 | * @param stream to print to |
|---|
| 44 | * @param format for the print (as fprintf()) |
|---|
| 45 | * @return result code from the print |
|---|
| 46 | */ |
|---|
| 47 | int IOStream_print(IOStream *stream, const char *format, ...){ |
|---|
| 48 | va_list args; |
|---|
| 49 | int result = -1; |
|---|
| 50 | |
|---|
| 51 | va_start(args, format); |
|---|
| 52 | result = IOStream_vprint(stream, format, args); |
|---|
| 53 | va_end(args); |
|---|
| 54 | return result; |
|---|
| 55 | } |
|---|