Some more cleanup

This commit is contained in:
Dale Weiler 2013-10-11 06:36:05 -04:00
parent 87d9371a5c
commit 63fdab8422
5 changed files with 77 additions and 9 deletions

View file

@ -20,9 +20,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#define GMQCC_PLATFORM_HEADER
#include <stdio.h>
#include "gmqcc.h"
#include "platform.h"
#define GMQCC_IS_STDOUT(X) ((fs_file_t*)((void*)X) == (fs_file_t*)stdout)
#define GMQCC_IS_STDERR(X) ((fs_file_t*)((void*)X) == (fs_file_t*)stderr)
@ -50,10 +49,8 @@ static con_t console;
* checks.
*/
static void con_enablecolor(void) {
if (console.handle_err == (fs_file_t*)stderr || console.handle_err == (fs_file_t*)stdout)
console.color_err = !!(platform_isatty(STDERR_FILENO));
if (console.handle_out == (fs_file_t*)stderr || console.handle_out == (fs_file_t*)stdout)
console.color_out = !!(platform_isatty(STDOUT_FILENO));
console.color_err = util_isatty(console.handle_err);
console.color_out = util_isatty(console.handle_out);
}
/*

View file

@ -312,6 +312,9 @@ const char *util_strerror(int err);
const struct tm *util_localtime(const time_t *timer);
const char *util_ctime (const time_t *timer);
typedef struct fs_file_s fs_file_t;
int util_isatty (fs_file_t *);
/*
* A flexible vector implementation: all vector pointers contain some
* data about themselfs exactly - sizeof(vector_t) behind the pointer
@ -404,7 +407,7 @@ int util_snprintf(char *str, size_t, const char *fmt, ...);
#define FS_FILE_EOF -1
typedef struct fs_dir_s fs_dir_t;
typedef struct fs_file_s fs_file_t;
/*typedef struct fs_file_s fs_file_t;*/
typedef struct dirent fs_dirent_t;
void fs_file_close (fs_file_t *);

View file

@ -75,9 +75,71 @@
# include <dirent.h>
#endif /*!_WIN32*/
/*
* Function: platform_vsnprintf
* Write formatted output using a pointer to a lis of arguments.
*
* Parameters:
* buffer - Storage location for output.
* bytes - Maximum number of characters to write.
* format - Format specification.
* arg - Variable argument list.
*
* Returns:
* The number of characters written if the number of characters to write
* is less than or equal to `bytes`; if the number of characters to write
* is greater than `bytes`, this function returns -1 indicating that the
* output has been truncated. The return value does not include the
* terminating null, if one is written.
*
* Remarks:
* Function takes pointer to an argument list, then formats the data,
* and writes up to `bytes` characters to the memory pointed to by
* `buffer`. If there is room at the end (that is, if the number of
* character to write is less than `bytes`), the buffer will be null-terminated.
*/
int platform_vsnprintf(char *buffer, size_t bytes, const char *format, va_list arg);
int platform_vsscanf(const char *str, const char *format, va_list arg);
/*
* Function: platform_vsscanf
* Reads formatted data from a string.
*
* Parameters:
* buffer - Stored data to read.
* format - Format specification.
* arg - Variable argument list.
*
* Returns:
* The number of fields that are successfully converted and assigned;
* the return value does not include fields that were read but not
* assigned. A return vlaue of 0 indicated that no fields were assigned.
* The return value if EOF for error or if the end of the string is
* reached before the first conversion.
*
* Remarks:
* Reads data from `buffer` into the locations that are given by each
* argument in the `arg` argument list. Every argument in the list must
* be a pointer to a variable that has a type that corresponds to a
* type specifier in `format`. The `format` argument controls th
* interpretation of the input fields and has the same form and function
* as the `format` argument for the *scanf* function. If copying takes
* place between strings that overlap, the behaviour is undefined.
*/
int platform_vsscanf(const char *buffer, const char *format, va_list arg);
/*
* Function: platform_localtime
* Convert a time value and correct for the local time zone.
*
* Parameters
* timer - Pointer to stored time.
*
* Returns:
* A pointer to a structure result, or NULL if the date passed to
* the function is before midnight, January 1, 1970.
*/
const struct tm *platform_localtime(const time_t *timer);
const char *platform_ctime(const time_t *timer);
char *platform_strncat(char *dest, const char *src, size_t num);
const char *platform_tmpnam(char *str);

2
test.c
View file

@ -20,7 +20,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#define GMQCC_PLATFORM_HEADER
#define GMQCC_PLATFORM_HEADER /* TODO: eliminate! */
#include <stdlib.h>
#include <string.h>

6
util.c
View file

@ -280,6 +280,12 @@ const char *util_ctime(const time_t *timer) {
return platform_ctime(timer);
}
bool util_isatty(fs_file_t *file) {
if (file == (fs_file_t*)stdout) return !!platform_isatty(STDOUT_FILENO);
if (file == (fs_file_t*)stderr) return !!platform_isatty(STDERR_FILENO);
return false;
}
void util_seed(uint32_t value) {
srand((int)value);
}