ui_stdio.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 02 Feb 2006 07:07:03 +0000
changeset 104 339482f858af
parent 81 50f6052737ce
child 108 0b8bcaef83e7
permissions -rw-r--r--
Argh, really fix byte order serialization.
icculus@0
     1
icculus@0
     2
#include <stdio.h>
icculus@81
     3
#include <stdlib.h>
icculus@81
     4
#include <string.h>
icculus@66
     5
#include <ctype.h>
icculus@0
     6
icculus@0
     7
#include "platform.h"
icculus@0
     8
#include "ui.h"
icculus@0
     9
icculus@81
    10
static void ui_title_stdio(const char *str)
icculus@0
    11
{
icculus@81
    12
    printf("=== %s ===\n\n", str);
icculus@81
    13
} /* ui_title_stdio */
icculus@0
    14
icculus@0
    15
icculus@81
    16
static void ui_real_deinit_stdio(void)
icculus@0
    17
{
icculus@0
    18
    printf("\n\nHit enter to quit.\n\n");
icculus@0
    19
    getchar();
icculus@81
    20
} /* ui_deinit_stdio */
icculus@0
    21
icculus@0
    22
icculus@81
    23
static void ui_pump_stdio(void)
icculus@0
    24
{
icculus@0
    25
    /* no-op. */
icculus@81
    26
} /* ui_pump_stdio */
icculus@0
    27
icculus@0
    28
icculus@81
    29
static void ui_add_to_log_stdio(const char *str, int debugging)
icculus@0
    30
{
icculus@0
    31
    printf("%s%s\n", debugging ? "debug: " : "", str);
icculus@81
    32
} /* ui_add_to_log_stdio */
icculus@0
    33
icculus@0
    34
icculus@81
    35
static void ui_fatal_stdio(const char *str)
icculus@0
    36
{
icculus@0
    37
    fprintf(stderr, "\n%s\n\n", str);
icculus@81
    38
} /* ui_fatal_stdio */
icculus@0
    39
icculus@0
    40
icculus@81
    41
static void ui_success_stdio(const char *str)
icculus@0
    42
{
icculus@0
    43
    fprintf(stderr, "\n%s\n\n", str);
icculus@81
    44
} /* ui_success_stdio */
icculus@0
    45
icculus@0
    46
icculus@81
    47
static void ui_total_progress_stdio(int percent)
icculus@0
    48
{
icculus@0
    49
    static int lastpercent = -1;
icculus@0
    50
    if (percent != lastpercent)
icculus@0
    51
    {
icculus@0
    52
        lastpercent = percent;
icculus@0
    53
        printf(".");
icculus@0
    54
        if (percent == 100)
icculus@0
    55
            printf("\n");
icculus@0
    56
    } /* if */
icculus@81
    57
} /* ui_total_progress_stdio */
icculus@0
    58
icculus@0
    59
icculus@81
    60
static void ui_status_stdio(const char *str)
icculus@0
    61
{
icculus@66
    62
    printf("Current operation: %s\n", str);
icculus@81
    63
} /* ui_status_stdio */
icculus@0
    64
icculus@66
    65
icculus@81
    66
static int ui_prompt_yn_stdio(const char *question)
icculus@66
    67
{
icculus@66
    68
    int c;
icculus@66
    69
    while (1)
icculus@66
    70
    {
icculus@66
    71
        printf("%s", question);
icculus@66
    72
        c = toupper(getchar());
icculus@66
    73
        if (c == 'N')
icculus@66
    74
            return(0);
icculus@66
    75
        else if ((c == 'Y') || (c == '\r') || (c == '\n'))
icculus@66
    76
            return(1);
icculus@66
    77
        printf("\n");
icculus@66
    78
    } /* while */
icculus@66
    79
icculus@66
    80
    return(1);
icculus@81
    81
} /* ui_prompt_yn_stdio */
icculus@66
    82
icculus@66
    83
icculus@81
    84
static int ui_prompt_ny_stdio(const char *question)
icculus@66
    85
{
icculus@66
    86
    int c;
icculus@66
    87
    while (1)
icculus@66
    88
    {
icculus@66
    89
        printf("%s", question);
icculus@66
    90
        c = toupper(getchar());
icculus@66
    91
        if (c == 'Y')
icculus@66
    92
            return 1;
icculus@66
    93
        else if ((c == 'N') || (c == '\r') || (c == '\n'))
icculus@66
    94
            return 0;
icculus@66
    95
        printf("\n");
icculus@66
    96
    } /* while */
icculus@66
    97
icculus@66
    98
    return(0);
icculus@81
    99
} /* ui_prompt_ny_stdio */
icculus@66
   100
icculus@0
   101
icculus@81
   102
static void ui_msgbox_stdio(const char *str)
icculus@81
   103
{
icculus@81
   104
    printf("\n\n-----\n%s\n-----\n\nHit enter to continue.\n\n", str);
icculus@81
   105
    getchar();
icculus@81
   106
} /* ui_msgbox_stdio */
icculus@81
   107
icculus@81
   108
icculus@81
   109
static int ui_file_picker_stdio(char *buf, size_t bufsize)
icculus@81
   110
{
icculus@81
   111
    while (1)
icculus@81
   112
    {
icculus@81
   113
        size_t len;
icculus@81
   114
        puts("Please enter the path, or blank to cancel.\n> ");
icculus@81
   115
icculus@81
   116
        buf[0] = '\0';
icculus@81
   117
        fgets(buf, bufsize, stdin);
icculus@81
   118
        len = strlen(buf);
icculus@81
   119
        while ((len > 0) && ((buf[len-1] == '\n') || (buf[len-1] == '\r')))
icculus@81
   120
            buf[--len] = '\0';
icculus@81
   121
icculus@81
   122
        if (len == 0)
icculus@81
   123
            return(0);  /* user "cancelled". */
icculus@81
   124
icculus@81
   125
        if (file_exists(buf))
icculus@81
   126
            return(1);  /* user entered valid path. */
icculus@81
   127
icculus@81
   128
        puts("That path does not exist. Please try again.\n\n");
icculus@81
   129
    } /* while */
icculus@81
   130
icculus@81
   131
    return(0);  /* should never hit this. */
icculus@81
   132
} /* ui_file_picker_stdio */
icculus@81
   133
icculus@81
   134
icculus@81
   135
static int ui_show_readme_stdio(const char *fname,const char *text)
icculus@81
   136
{
icculus@81
   137
    /*
icculus@81
   138
     * Cheat for now and push this off to "less", which will warn if the
icculus@81
   139
     *  readme is a binary file and prompt the user about avoiding it.
icculus@81
   140
     */
icculus@81
   141
icculus@81
   142
    size_t allocsize = strlen(fname) + 32;
icculus@81
   143
    char *cmd = (char *) alloca(allocsize);
icculus@81
   144
    if (!cmd)
icculus@81
   145
    {
icculus@81
   146
        _fatal("Out of memory.");
icculus@81
   147
        return(0);
icculus@81
   148
    } /* if */
icculus@81
   149
icculus@81
   150
    snprintf(cmd, allocsize, "less %s", fname);
icculus@81
   151
    system(cmd);  /* !!! FIXME: error check? */
icculus@81
   152
    return(1);
icculus@81
   153
} /* ui_show_readme_stdio */
icculus@81
   154
icculus@81
   155
icculus@81
   156
int ui_init_stdio(void)
icculus@81
   157
{
icculus@81
   158
    UI_SET_FUNC_POINTERS(stdio);
icculus@81
   159
    return(1); /* always succeeds. */
icculus@81
   160
} /* ui_init */
icculus@81
   161
icculus@81
   162
/* end of ui_stdio.c ... */
icculus@81
   163