kopia lustrzana https://github.com/F5OEO/tstools
Since none of the printing calls care about a return value,
make the new printing mechanism simpler, by having functions with void returns. --HG-- extra : convert_revision : svn%3Aeff31bef-be4a-0410-a8fe-e47997df2690/trunk%40118issue20
rodzic
87e2ce8ca6
commit
6d6349eea4
60
printing.c
60
printing.c
|
@ -37,32 +37,32 @@
|
||||||
// Default printing functions
|
// Default printing functions
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
static int print_message_to_stdout(const char *message)
|
static void print_message_to_stdout(const char *message)
|
||||||
{
|
{
|
||||||
return (fputs(message,stdout) == 0 ? 0:1);
|
(void) fputs(message,stdout);
|
||||||
}
|
}
|
||||||
static int print_message_to_stderr(const char *message)
|
static void print_message_to_stderr(const char *message)
|
||||||
{
|
{
|
||||||
return (fputs(message,stderr) == 0 ? 0:1);
|
(void) fputs(message,stderr);
|
||||||
}
|
}
|
||||||
static int fprint_message_to_stdout(const char *format, va_list arg_ptr)
|
static void fprint_message_to_stdout(const char *format, va_list arg_ptr)
|
||||||
{
|
{
|
||||||
return (vfprintf(stdout, format, arg_ptr) < 0 ? 1:0);
|
(void) vfprintf(stdout, format, arg_ptr);
|
||||||
}
|
}
|
||||||
static int fprint_message_to_stderr(const char *format, va_list arg_ptr)
|
static void fprint_message_to_stderr(const char *format, va_list arg_ptr)
|
||||||
{
|
{
|
||||||
return (vfprintf(stderr, format, arg_ptr) < 0 ? 1:0);
|
(void) vfprintf(stderr, format, arg_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// Print redirection
|
// Print redirection
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
static int (*print_message_fn) (const char *message) = print_message_to_stdout;
|
static void (*print_message_fn) (const char *message) = print_message_to_stdout;
|
||||||
static int (*print_error_fn) (const char *message) = print_message_to_stdout;
|
static void (*print_error_fn) (const char *message) = print_message_to_stdout;
|
||||||
|
|
||||||
static int (*fprint_message_fn) (const char *format, va_list arg_ptr) = fprint_message_to_stdout;
|
static void (*fprint_message_fn) (const char *format, va_list arg_ptr) = fprint_message_to_stdout;
|
||||||
static int (*fprint_error_fn) (const char *format, va_list arg_ptr) = fprint_message_to_stdout;
|
static void (*fprint_error_fn) (const char *format, va_list arg_ptr) = fprint_message_to_stdout;
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
@ -70,55 +70,43 @@ static int (*fprint_error_fn) (const char *format, va_list arg_ptr) = fprint_m
|
||||||
// ============================================================
|
// ============================================================
|
||||||
/*
|
/*
|
||||||
* Prints the given string, as a normal message.
|
* Prints the given string, as a normal message.
|
||||||
*
|
|
||||||
* Returns 0 if all goes well, 1 if something goes wrong.
|
|
||||||
*/
|
*/
|
||||||
extern int print_msg(const char *text)
|
extern void print_msg(const char *text)
|
||||||
{
|
{
|
||||||
return print_message_fn(text);
|
print_message_fn(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Prints the given string, as an error message.
|
* Prints the given string, as an error message.
|
||||||
*
|
|
||||||
* Returns 0 if all goes well, 1 if something goes wrong.
|
|
||||||
*/
|
*/
|
||||||
extern int print_err(const char *text)
|
extern void print_err(const char *text)
|
||||||
{
|
{
|
||||||
return print_error_fn(text);
|
print_error_fn(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Prints the given format text, as a normal message.
|
* Prints the given format text, as a normal message.
|
||||||
*
|
|
||||||
* Returns 0 if all goes well, 1 if something goes wrong.
|
|
||||||
*/
|
*/
|
||||||
extern int fprint_msg(const char *format, ...)
|
extern void fprint_msg(const char *format, ...)
|
||||||
{
|
{
|
||||||
int retval;
|
|
||||||
va_list va_arg;
|
va_list va_arg;
|
||||||
va_start(va_arg, format);
|
va_start(va_arg, format);
|
||||||
retval = fprint_message_fn(format, va_arg);
|
fprint_message_fn(format, va_arg);
|
||||||
va_end(va_arg);
|
va_end(va_arg);
|
||||||
return retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Prints the given formatted text, as an error message.
|
* Prints the given formatted text, as an error message.
|
||||||
*
|
|
||||||
* Returns 0 if all goes well, 1 if something goes wrong.
|
|
||||||
*/
|
*/
|
||||||
extern int fprint_err(const char *format, ...)
|
extern void fprint_err(const char *format, ...)
|
||||||
{
|
{
|
||||||
int retval;
|
|
||||||
va_list va_arg;
|
va_list va_arg;
|
||||||
va_start(va_arg, format);
|
va_start(va_arg, format);
|
||||||
retval = fprint_error_fn(format, va_arg);
|
fprint_error_fn(format, va_arg);
|
||||||
va_end(va_arg);
|
va_end(va_arg);
|
||||||
return retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
@ -172,10 +160,10 @@ extern void redirect_output_stdout(void)
|
||||||
*
|
*
|
||||||
* Returns 0 if all goes well, 1 if something goes wrong.
|
* Returns 0 if all goes well, 1 if something goes wrong.
|
||||||
*/
|
*/
|
||||||
extern int redirect_output( int (*new_print_message_fn) (const char *message),
|
extern int redirect_output( void (*new_print_message_fn) (const char *message),
|
||||||
int (*new_print_error_fn) (const char *message),
|
void (*new_print_error_fn) (const char *message),
|
||||||
int (*new_fprint_message_fn) (const char *format, va_list arg_ptr),
|
void (*new_fprint_message_fn) (const char *format, va_list arg_ptr),
|
||||||
int (*new_fprint_error_fn) (const char *format, va_list arg_ptr)
|
void (*new_fprint_error_fn) (const char *format, va_list arg_ptr)
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
if (new_print_message_fn == NULL || new_print_error_fn == NULL ||
|
if (new_print_message_fn == NULL || new_print_error_fn == NULL ||
|
||||||
|
|
|
@ -36,28 +36,20 @@
|
||||||
// ============================================================
|
// ============================================================
|
||||||
/*
|
/*
|
||||||
* Prints the given string, as a normal message.
|
* Prints the given string, as a normal message.
|
||||||
*
|
|
||||||
* Returns 0 if all goes well, 1 if something goes wrong.
|
|
||||||
*/
|
*/
|
||||||
extern int print_msg(const char *text);
|
extern void print_msg(const char *text);
|
||||||
/*
|
/*
|
||||||
* Prints the given string, as an error message.
|
* Prints the given string, as an error message.
|
||||||
*
|
|
||||||
* Returns 0 if all goes well, 1 if something goes wrong.
|
|
||||||
*/
|
*/
|
||||||
extern int print_err(const char *text);
|
extern void print_err(const char *text);
|
||||||
/*
|
/*
|
||||||
* Prints the given format text, as a normal message.
|
* Prints the given format text, as a normal message.
|
||||||
*
|
|
||||||
* Returns 0 if all goes well, 1 if something goes wrong.
|
|
||||||
*/
|
*/
|
||||||
extern int fprint_msg(const char *format, ...);
|
extern void fprint_msg(const char *format, ...);
|
||||||
/*
|
/*
|
||||||
* Prints the given formatted text, as an error message.
|
* Prints the given formatted text, as an error message.
|
||||||
*
|
|
||||||
* Returns 0 if all goes well, 1 if something goes wrong.
|
|
||||||
*/
|
*/
|
||||||
extern int fprint_err(const char *format, ...);
|
extern void fprint_err(const char *format, ...);
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// Choosing what the printing functions do
|
// Choosing what the printing functions do
|
||||||
|
@ -94,10 +86,10 @@ extern void redirect_output_stdout(void);
|
||||||
*
|
*
|
||||||
* Returns 0 if all goes well, 1 if something goes wrong.
|
* Returns 0 if all goes well, 1 if something goes wrong.
|
||||||
*/
|
*/
|
||||||
extern int redirect_output( int (*new_print_message_fn) (const char *message),
|
extern int redirect_output( void (*new_print_message_fn) (const char *message),
|
||||||
int (*new_print_error_fn) (const char *message),
|
void (*new_print_error_fn) (const char *message),
|
||||||
int (*new_fprint_message_fn) (const char *format, va_list arg_ptr),
|
void (*new_fprint_message_fn) (const char *format, va_list arg_ptr),
|
||||||
int (*new_fprint_error_fn) (const char *format, va_list arg_ptr)
|
void (*new_fprint_error_fn) (const char *format, va_list arg_ptr)
|
||||||
);
|
);
|
||||||
|
|
||||||
#endif // _printing_fns
|
#endif // _printing_fns
|
||||||
|
|
|
@ -37,16 +37,6 @@ I can test most easily!
|
||||||
import sys
|
import sys
|
||||||
import array
|
import array
|
||||||
|
|
||||||
from common cimport FILE, EOF, stdout, fopen, fclose, fileno
|
|
||||||
from common cimport errno, strerror, free
|
|
||||||
from common cimport const_void_ptr
|
|
||||||
from common cimport PyString_FromStringAndSize, PyString_AsStringAndSize, \
|
|
||||||
PyObject_AsReadBuffer
|
|
||||||
from common cimport uint8_t, uint16_t, uint32_t, uint64_t
|
|
||||||
from common cimport int8_t, int16_t, int32_t, int64_t
|
|
||||||
|
|
||||||
from tstools import TSToolsException
|
|
||||||
|
|
||||||
cdef extern from *:
|
cdef extern from *:
|
||||||
ctypedef char* const_char_ptr "const char*"
|
ctypedef char* const_char_ptr "const char*"
|
||||||
|
|
||||||
|
@ -66,6 +56,8 @@ cdef extern from "Python.h":
|
||||||
# written to the real (C level) stdout.
|
# written to the real (C level) stdout.
|
||||||
void PySys_WriteStdout(const_char_ptr format, ...)
|
void PySys_WriteStdout(const_char_ptr format, ...)
|
||||||
|
|
||||||
|
# Unfortunately, there are two common ways of implementing a va_list,
|
||||||
|
# and we just have to guess which is being used.
|
||||||
ctypedef void * va_list
|
ctypedef void * va_list
|
||||||
#ctypedef struct va_list va_list
|
#ctypedef struct va_list va_list
|
||||||
|
|
||||||
|
@ -77,32 +69,24 @@ cdef extern from "Python.h":
|
||||||
# ctypedef void* va_list "va_list"
|
# ctypedef void* va_list "va_list"
|
||||||
|
|
||||||
cdef extern from 'printing_fns.h':
|
cdef extern from 'printing_fns.h':
|
||||||
int print_msg(const_char_ptr text)
|
void print_msg(const_char_ptr text)
|
||||||
int print_err(const_char_ptr text)
|
void print_err(const_char_ptr text)
|
||||||
int fprint_msg(const_char_ptr format, ...)
|
void fprint_msg(const_char_ptr format, ...)
|
||||||
int fprint_err(const_char_ptr format, ...)
|
void fprint_err(const_char_ptr format, ...)
|
||||||
int redirect_output( int (*new_print_message_fn) (const_char_ptr message),
|
int redirect_output( void (*new_print_message_fn) (const_char_ptr message),
|
||||||
int (*new_print_error_fn) (const_char_ptr message),
|
void (*new_print_error_fn) (const_char_ptr message),
|
||||||
int (*new_fprint_message_fn) (const_char_ptr format, va_list arg_ptr),
|
void (*new_fprint_message_fn) (const_char_ptr format, va_list arg_ptr),
|
||||||
int (*new_fprint_error_fn) (const_char_ptr format, va_list arg_ptr)
|
void (*new_fprint_error_fn) (const_char_ptr format, va_list arg_ptr)
|
||||||
)
|
)
|
||||||
|
|
||||||
cdef int our_print_msg(const_char_ptr text):
|
cdef void our_print_msg(const_char_ptr text):
|
||||||
PySys_WriteStdout('%s',text)
|
PySys_WriteStdout('%s',text)
|
||||||
return 0
|
|
||||||
|
|
||||||
cdef int our_format_msg(const_char_ptr format, va_list arg_ptr):
|
cdef void our_format_msg(const_char_ptr format, va_list arg_ptr):
|
||||||
cdef int err
|
cdef int err
|
||||||
cdef char buffer[1000]
|
cdef char buffer[1000]
|
||||||
err = PyOS_vsnprintf(buffer, 1000, format, arg_ptr)
|
PyOS_vsnprintf(buffer, 1000, format, arg_ptr)
|
||||||
if err < 0:
|
PySys_WriteStdout('%s',buffer)
|
||||||
return 1
|
|
||||||
elif err > 999:
|
|
||||||
PySys_WriteStdout('%s',buffer)
|
|
||||||
return 1
|
|
||||||
else:
|
|
||||||
PySys_WriteStdout('%s',buffer)
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def setup_printing():
|
def setup_printing():
|
||||||
cdef int err
|
cdef int err
|
||||||
|
|
|
@ -33,25 +33,23 @@
|
||||||
|
|
||||||
// Some example redirection routines
|
// Some example redirection routines
|
||||||
|
|
||||||
static int print_message_to_stdout(const char *message)
|
static void print_message_to_stdout(const char *message)
|
||||||
{
|
{
|
||||||
(void) printf("<<<MSG>>> %s",message);
|
(void) printf("<<<MSG>>> %s",message);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
static int print_message_to_stderr(const char *message)
|
static void print_message_to_stderr(const char *message)
|
||||||
{
|
{
|
||||||
(void) printf("<<<ERR>>> %s",message);
|
(void) printf("<<<ERR>>> %s",message);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
static int fprint_message_to_stdout(const char *format, va_list arg_ptr)
|
static void fprint_message_to_stdout(const char *format, va_list arg_ptr)
|
||||||
{
|
{
|
||||||
printf("<<<MSG>>> ");
|
printf("<<<MSG>>> ");
|
||||||
return (vfprintf(stdout, format, arg_ptr) < 0 ? 1:0);
|
(void) vfprintf(stdout, format, arg_ptr);
|
||||||
}
|
}
|
||||||
static int fprint_message_to_stderr(const char *format, va_list arg_ptr)
|
static void fprint_message_to_stderr(const char *format, va_list arg_ptr)
|
||||||
{
|
{
|
||||||
printf("<<<ERR>>> ");
|
printf("<<<ERR>>> ");
|
||||||
return (vfprintf(stdout, format, arg_ptr) < 0 ? 1:0);
|
(void) vfprintf(stdout, format, arg_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_usage()
|
static void print_usage()
|
||||||
|
|
Ładowanie…
Reference in New Issue