aboutsummaryrefslogtreecommitdiff
path: root/basic_curses.c
blob: ce6a338afb95a0b144afb681b2c20bc2bf5f26d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "basic_curses.h"

void printfc(char* fstrc, int max_length, ...) {
    int i = 0;
    int nbC = 0;
    char* tmp = NULL;
    int toP = 0;


    va_list ap;
    va_start(ap, max_length);


    while(fstrc[i] && nbC < max_length) {
        if( strchr( "${<%>}#", fstrc[i]) ) {
            if( tmp ) {
                printw("%.*s", toP, tmp);
                tmp=NULL;
                toP=0;
            }
            switch( fstrc[i] ) {
                case '$':
                    if( fstrc[i+1] ) {
                        if( isdigit(fstrc[i+1]) ) {
                            if( fstrc[i+1] == '9') {
                                ;
                            }else
                                attrset( COLOR_PAIR(fstrc[i+1]-'0') | A_NORMAL);
                        }else{
                            fprintf(stderr,"%c isn't a color, skipping", fstrc[i+1]);
                        }
                    }else{
                        fprintf(stderr,"end of line by '$', expect a color after '$'");
                        return;
                    }
                    i+=2;
                    break;
                case '%':
                    {
                        int j = 1;
                        char format[10] = "%";
                        while(j < 9 && !strchr("diouxXeEfFgGaAcsp%", format[j] = fstrc[i+j]) );
                        if( j == 9 ) {
                            fprintf(stderr,"format too long %s...",format);
                            return;
                        }
                        if( strchr("di", format[j] ) ) {
                            printw(format, va_arg(ap, int));
                        }else if( strchr("ouxX", format[j]) ) {
                            printw(format, va_arg(ap, unsigned int));
                        }else if( strchr("eE", format[j]) ) {
                            printw(format, va_arg(ap, double));
                        }else if( strchr("fF", format[j]) ) {
                            printw(format, va_arg(ap, double));
                        }else if( strchr("gG", format[j]) ) {
                            printw(format, va_arg(ap, double));
                        }else if( strchr("aA", format[j]) ) {
                            printw(format, va_arg(ap, double));
                        }else if( strchr("c", format[j]) ) {
                            printw(format, va_arg(ap, int));
                        }else if( strchr("s", format[j]) ) {
                            printw(format, va_arg(ap, const char*));
                        }else if( strchr("p", format[j]) ) {
                            printw(format, va_arg(ap, void*));
                        }else if( strchr("%", format[j]) ) {
                            printw("%%");
                        }
//                        nbC+=

                        i+=j;
                    }
            }
        }else{
            if( ! tmp )
                tmp = fstrc+i;
            toP += 1;
            nbC++;
            i++;
        }
    }
    if(tmp)
        printw("%.*s", toP, tmp);
    fflush(NULL);
}

void printc(char* fstrc, int max_length) {
    int i = 0;
    int nbC = 0;
    char* tmp = NULL;
    int toP = 0;
    while(fstrc[i] && nbC < max_length) {
        if( strchr( "${<>}#", fstrc[i]) ) {
            if( tmp ) {
                printw("%.*s", toP, tmp);
                tmp=NULL;
                toP=0;
            }
            switch( fstrc[i] ) {
                case '$':
                    if( fstrc[i+1] ) {
                        if( isdigit(fstrc[i+1]) ) {
                            if( fstrc[i+1] == '9') {
                                ;
                            }else
                                attrset( COLOR_PAIR(fstrc[i+1]-'0') | A_NORMAL);
                        }else{
                            fprintf(stderr,"%c isn't a color, skipping", fstrc[i+1]);
                        }
                    }else{
                        fprintf(stderr,"end of line by '$', expect a color after '$'");
                        return;
                    }
                    i+=2;
            }
        }else{
            if( ! tmp )
                tmp = fstrc+i;
            toP += 1;
            nbC++;
            i++;
        }
    }
    if(tmp)
        printw("%.*s", toP, tmp);
    fflush(NULL);
}
void mvprintc(int x, int y, char* fstrc, int max_length) {
    move(y, x);
    printc(fstrc, max_length);
}