Optimization

This commit is contained in:
stmctommyau 2023-09-26 00:48:34 +08:00
parent ecec3ea611
commit 6194472e39
No known key found for this signature in database
GPG Key ID: 87B1991E1277F054
4 changed files with 19 additions and 21 deletions

@ -169,7 +169,7 @@ void remove_admin(char* role){
}
char line[100];
while(fgets(line,100,fp) != NULL){
if(line[0] == '#') {
if(unlikely(line[0] == '#')) {
fprintf(fp2,"%s",line);
continue;
}
@ -194,7 +194,7 @@ int basic_get_row_count(int end, FILE *fp){
while(!feof(fp)&&!ferror(fp)){
char buffer[100];
fgets(buffer, sizeof(buffer),fp);
if(buffer[0] == '#' || buffer[0] == '\0'){//catch comment line and ignore
if(unlikely(buffer[0] == '#' || buffer[0] == '\0')){//catch comment line and ignore
continue;
}
colmun_count++;
@ -223,7 +223,7 @@ struct inventory read_db_invt(){//please open file in read mode
for(INVENTORY j=category;j<=ENDOFINVENTORY;j++){
char buffer[100];
fgets(buffer, sizeof(buffer),fp);
if(buffer[0] == '#'){//catch comment line and ignore
if(unlikely(buffer[0] == '#')){//catch comment line and ignore
j--;//decrement j to prevent skipping next column
}else{
buffer[strlen(buffer)] = '\0';
@ -265,8 +265,6 @@ struct inventory read_db_invt(){//please open file in read mode
fclose(fp);
db.db.init_status = true;
return db;
}
struct transaction read_db_tran(){
FILE* fp = fopen(TRANSACTION_DB, "r");
@ -285,7 +283,7 @@ struct transaction read_db_tran(){
for(TRANSACTION j=date;j<=ENDOFTRANSACTION;j++){
char buffer[100];
fgets(buffer, sizeof(buffer),fp);
if(buffer[0] == '#'){//catch comment line and ignore
if(unlikely(buffer[0] == '#')){//catch comment line and ignore
j--;//decrement j to prevent skipping next column
}else{
buffer[strlen(buffer)] = '\0';
@ -344,7 +342,7 @@ struct user read_db_user(){
for(USER j=name;j<=ENDOFUSER;j++){
char buffer[100];
fgets(buffer, sizeof(buffer),fp);
if(buffer[0] == '#'){//catch comment line and ignore
if(unlikely(buffer[0] == '#')){//catch comment line and ignore
j--;//decrement j to prevent skipping next column
}else{
buffer[strlen(buffer)] = '\0';//prevent any garbage value
@ -463,15 +461,12 @@ bool update_db_invt(struct inventory invt){
return false;
}
for(int i=0;i<invt.db.row_count;i++){
if(invt.row[i].barcode == -1024)//skip create new row
if(invt.row[i].barcode == -1024 || invt.row[i].isdeleted == true)//skip create new row
continue;
if(invt.row[i].isdeleted == true){
continue;
}
for(INVENTORY j=category;j<=ENDOFINVENTORY;j++){
char buffer[100];
do{
if(feof(fpR)){
if(unlikely(feof(fpR))){
break;
}
fgets(buffer, sizeof(buffer),fpR);
@ -527,7 +522,7 @@ bool update_db_tran(struct transaction tran){
for(TRANSACTION j=date;j<=ENDOFTRANSACTION;j++){
char buffer[100];
do{
if(feof(fpR)){
if(unlikely(feof(fpR))){
break;
}
fgets(buffer, sizeof(buffer),fpR);
@ -582,7 +577,7 @@ bool update_db_user(struct user user){
for(USER j=name;j<=ENDOFUSER;j++){
char buffer[100];
do{
if(feof(fpR)){
if(unlikely(feof(fpR))){
break;
}
fgets(buffer, sizeof(buffer),fpR);

@ -133,7 +133,7 @@ void add_tran(){
free(row);
return;
}
if(row == NULL || !append_transaction_db(row)){
if(!row || !append_transaction_db(row)){
printf("Failed to add item\n");
}else{
printf("Item added\n");
@ -165,7 +165,7 @@ struct transaction update_tran(struct transaction db,int index){
item_inputer("ID",LONG,&db.row[index].id,true);
item_inputer("Barcode",LONG,&db.row[index].barcode,true);
if(!update_db_tran(db)){
if(unlikely(!update_db_tran(db))){
printf("Failed to update transaction\n");
exit(1);//exit program to prevent errors
}else{
@ -180,7 +180,7 @@ struct transaction update_tran(struct transaction db,int index){
struct transaction remove_tran(struct transaction db,int index){
db.row[index].isdeleted = true;
if(!update_db_tran(db)){
if(unlikely(!update_db_tran(db))){
printf("Failed to delete transaction\n");
exit(1);//exit program to prevent errors
}else{

@ -90,7 +90,7 @@ struct user add_user(struct user db){
return db;
}
if(!append_user(temprow)){
if(unlikely(!append_user(temprow))){
printf("Failed to add user\n");
exit(1);//exit program to prevent errors
}else{
@ -111,7 +111,7 @@ struct user edit_user(struct user db,int index){
item_inputer("id",LONG,&db.row[index].id,true);
item_inputer("role",STRING,&db.row[index].role,true);
if(!update_db_user(db)){
if(unlikely(!update_db_user(db))){
printf("Failed to update user\n");
exit(1);//exit program to prevent errors
}else{
@ -125,7 +125,7 @@ struct user edit_user(struct user db,int index){
}
struct user delete_user(struct user db,int index){
db.row[index].isdeleted = true;
if(!update_db_user(db)){
if(unlikely(!update_db_user(db))){
printf("Failed to delete user\n");
exit(1);//exit program to prevent errors
}else{

@ -14,7 +14,10 @@
#include <stdio_ext.h>
#endif
#define SIZE_OF_PAGE 20
#ifndef likely
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#endif
void Cls(){
#ifdef _WIN32
system("cls");