sba/utils.h
2023-07-18 13:03:34 +08:00

112 lines
2.4 KiB
C

#ifndef STD_LIB_H
#define STD_LIB_H
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<math.h>
#include<time.h>
#include<ctype.h>
#include <stddef.h>
#endif
void welcome_message(){
printf("><Welcome to the Student Union POS system><\n");//welcome messgae
}
int choices_selecter(char * Items[],int items,char * welcome_messages ){
int choice = 0;
do{
system("cls");
if (welcome_messages !=NULL){
printf("%s",welcome_messages);
}
printf("Select An Option:\n");
int i;
for (i=0; i<items;i++){
printf("%d. %s\n",i+1,Items[i]);
}
printf("%d. Exit\n",i+1);
printf(">");
fflush(stdin);
scanf("%d",&choice);
if(choice < 1 || choice > items+1){
printf("Invalid choice...press any key to retry....\n");
fflush(stdin);
getchar();
}
}while(choice < 1 || choice > items+1);
return choice;
}
char* prompt_item(char* prompt){
char* item = malloc(sizeof(char) * 100);
do{
printf("%s",prompt);
fflush(stdin);
scanf("%[^\n]",item);
if(strcmp(item,"") == 0){
printf("Invalid input, try again?(y/n)\n");
char temp[100];
fflush(stdin);
scanf("%[^\n]",temp);
if(strcmp(temp,"n") == 0){
return NULL;
}
}
}while(strcmp(item,"") == 0);
return item;
}
typedef enum{
INT=1,STRING=2,LONG=3,FLOAT=4
}INPUT_TYPE;
bool item_inputer(char * varname,INPUT_TYPE type,void * item){
char output[1024] = "Please input the ";
strcat(output,varname);
strcat(output,": \n>");
char* temp = prompt_item(output);
if(temp == NULL){
return false;
}
switch(type){
case INT:
item = atoi(temp);
break;
case STRING:
strcpy(item,temp);
break;
case LONG:
item = atol(temp);
break;
case FLOAT:
item = atof(temp);
break;
default:
return false;
}
return true;
}
//check if strcasestr is define in current environment
//if not, define it
#ifndef HAVE_STRCASESTR
char *strcasestr(const char *s, const char *find)
{
char c, sc;
size_t len;
if ((c = *find++) != 0) {
c = tolower((unsigned char)c);
len = strlen(find);
do {
do {
if ((sc = *s++) == 0)
return (NULL);
} while ((char)tolower((unsigned char)sc) != c);
} while (strncasecmp(s, find, len) != 0);
s--;
}
return ((char *)s);
}
#endif