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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
/*
** userinput.c User input functions for k2pdfopt.c.
**
** Copyright (C) 2020 http://willus.com
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
*/
#include "k2pdfopt.h"
static void string_display(char *s);
static int string_match(char *s,int c);
int userinput_float(char *message,double defval,double *dstval,int nmax,
double min,double max,char *extramessage)
{
char buf[256];
int i,na;
double v[8];
if (nmax>8)
nmax=8;
while (1)
{
if (extramessage!=NULL && extramessage[0]!='\0')
k2printf(TTEXT_BOLD2 "%s" TTEXT_NORMAL "\n",extramessage);
k2printf(TTEXT_BOLD2 "%s" TTEXT_NORMAL,message);
if (defval > -1e9)
k2printf(" [%g]",defval);
k2printf(": ");
k2gets(buf,255,"");
k2printf(TTEXT_NORMAL "\n");
clean_line(buf);
if (buf[0]=='\0')
{
dstval[0]=defval;
return(1);
}
if (tolower(buf[0])=='q')
return(-1);
na=string_read_doubles(buf,v,nmax);
if (na<=0)
{
k2printf(TTEXT_WARN "\aThe response '%s' is not valid.\n\n" TTEXT_NORMAL,buf);
continue;
}
for (i=0;i<na;i++)
{
if (atof(buf)<min || atof(buf)>max)
{
k2printf(TTEXT_WARN "\aThe response must be between %g and %g.\n\n" TTEXT_NORMAL,min,max);
break;
}
}
if (i<na)
continue;
for (i=0;i<na;i++)
dstval[i]=v[i];
return(na);
}
}
int userinput_integer(char *message,int defval,int *dstval,int min,int max)
{
char buf[256];
while (1)
{
k2printf(TTEXT_BOLD2 "%s" TTEXT_NORMAL " [%d]: " TTEXT_INPUT,
message,defval);
k2gets(buf,255,"");
k2printf(TTEXT_NORMAL "\n");
clean_line(buf);
if (buf[0]=='\0')
{
(*dstval)=defval;
return(0);
}
if (tolower(buf[0])=='q')
return(-1);
if (!is_an_integer(buf))
{
k2printf(TTEXT_WARN "\aThe response '%s' is not valid.\n\n" TTEXT_NORMAL,buf);
continue;
}
if (atoi(buf)<min || atoi(buf)>max)
{
k2printf(TTEXT_WARN "\aThe response must be between %d and %d.\n\n" TTEXT_NORMAL,min,max);
continue;
}
(*dstval)=atoi(buf);
return(0);
}
}
int userinput_any_string(char *message,char *dstval,int maxlen,char *defname)
{
char buf[1024];
if (maxlen>1023)
maxlen=1023;
while (1)
{
k2printf(TTEXT_BOLD2 "%s" TTEXT_NORMAL " [%s]: " TTEXT_INPUT,
message,defname);
k2gets(buf,maxlen,"");
k2printf(TTEXT_NORMAL "\n");
clean_line(buf);
if (buf[0]=='\0')
{
dstval[0]='\0';
return(0);
}
if (tolower(buf[0])=='q')
return(-1);
xstrncpy(dstval,buf,maxlen-1);
return(0);
}
}
int userinput_string(char *message,char *selection[],char *def)
{
char buf[256];
int i;
while (1)
{
k2printf(TTEXT_BOLD2 "%s" TTEXT_NORMAL " (",message);
for (i=0;selection[i][0]!='\0';i++)
{
if (i>0)
k2printf(", ");
string_display(selection[i]);
}
k2printf(") [%c]: " TTEXT_INPUT,def[0]);
k2gets(buf,255,"");
k2printf(TTEXT_NORMAL "\n");
clean_line(buf);
if (buf[0]=='\0')
strcpy(buf,def);
if (tolower(buf[0])=='q')
return(-1);
for (i=0;selection[i][0]!='\0';i++)
if (string_match(selection[i],buf[0]))
return(i);
k2printf(TTEXT_WARN "\aThe response '%s' is not valid.\n\n" TTEXT_NORMAL,
buf);
}
}
/* v2.15 new function so that strings can have an asterisk to mark the selection char */
static void string_display(char *s)
{
int i,ap;
for (i=0;s[i]!='\0';i++)
if (s[i]=='*')
break;
ap = (s[i]=='*');
for (i=0;s[i]!='\0';i++)
{
if (s[i]=='*' || (i==0 && !ap))
{
k2printf("%s",TTEXT_BOLD);
if (s[i]=='*' && s[i+1]!='\0')
i++;
k2printf("%c",s[i]);
k2printf("%s",TTEXT_NORMAL);
continue;
}
k2printf("%c",s[i]);
}
}
/* v2.15 new function so that strings can have an asterisk to mark the selection char */
static int string_match(char *s,int c)
{
int i;
if (s[0]=='\0')
return(0);
for (i=0;s[i]!='\0';i++)
if (s[i]=='*')
return(tolower(c)==tolower(s[i+1]));
return(tolower(c)==tolower(s[0]));
}
int get_ttyrows(void)
{
int i,j;
if (ansi_rows_cols(stdout,&i,&j))
return(i);
#if (defined(WIN32) || defined(WIN64))
return(25);
#else
return(24);
#endif
}
|