📄 ioutilities.c
字号:
#include "main.h"/* =============================================================================== Author: Hammer, May 2002, For www.cprogramming.com/cboard/ File: ioUtilities.c Contents: io_GetInt io_EnterToContinue io_GetYesNo io_GetLine io_WriteAllRecordsToFile io_WriteRecordToFile io_LoadAppConfig io_SaveAppConfig ===============================================================================*/static FILE *fp;/* =============================================================================== Function: ioLoadPhoneBookFromFile Args: Pointer to the node list Returns: Pointer to the node list. NULL Purpose: Loads the Record structs from the phone book file into the List ===============================================================================*/struct Node *io_LoadPhoneBookFromFile(struct Node *List){ struct Record rec, *recptr; struct Node *tmpListPtr; if ((fp = fopen(STR_FILENAME, "rb")) == NULL) { /* Failed to open database file */ printf("ERROR: Unable to open phone book file: %s\n", STR_FILENAME); return(List); } if (List) { /* List not empty, kill it before we start reloading */ (void)li_Traverse(List, free); List = li_Destroy(List); } while (fread(&rec, sizeof(struct Record), 1, fp) == 1) { if ((recptr = malloc(sizeof(struct Record))) == NULL) { /* Memory failure, don't do anymore */ perror("malloc"); break; } *recptr = rec; if ((tmpListPtr = li_Insert(List, recptr, rec_Compare)) == NULL) { printf("Error performing insert on list. Item not addded.\n"); free(recptr); /* throw record away */ } else { List = tmpListPtr; recptr->ID = gl_HighestID; recptr->Status = (unsigned char)0; } } (void)fclose(fp); gl_AppData.DataChanged = FALSE; return(List);}/* =============================================================================== Function: io_GetInt Args: maximum number for user to choose Returns: Number chosen, or -1 if x or X entered (for menu use) Notes: Assumes that 0 will never be a valid option, and will ask the user to re-enter. Purpose: Get an int from stdin (keyboard). =============================================================================== */int io_GetInt(int Max){ int Input, len; char buffer[BUFSIZ]; for (;;) { if ((len = io_GetLine(buffer, BUFSIZ, stdin)) != 0) { if (len == 1 && (buffer[0] == 'x' || buffer[0] == 'X')) { /* Allow for xX to be entered, and return -1 */ Input = RC_BAD; break; } if ((Input = atoi(buffer)) != 0) { if (Input >= 1 && Input <= Max) { break; } } } printf("Invalid. Try again (1-%d, x to exit)>", Max); } return(Input);}/* =============================================================================== Function: io_EnterToContinue Args: None Returns: None Purpose: Simulates the good old DOS PAUSE =============================================================================== */void io_EnterToContinue(void){ printf("---> Enter To Continue <---"); FLUSH_INPUT;}/* =============================================================================== Function: io_GetYesNo Args: String containing the prompt to be displayed Returns: TRUE if yY entered, FALSE if nN entered Purpose: Get a Y or N from stdin (keyboard) =============================================================================== */bool_t io_GetYesNo(char *prompt){ int c; bool_t rc; for (;;) { printf("%s", prompt); c = getchar(); switch (c) { case 'y': case 'Y': rc = TRUE; break; case 'n': case 'N': rc = FALSE; break; default: if (c != '\n') FLUSH_INPUT; continue; } break; } if (c != '\n') FLUSH_INPUT; return(rc);}/* =============================================================================== Function: io_GetLine Args: buffer to place the text length of the buffer file pointer to read from. Returns: Chars read and placed into buffer Purpose: Same as fgets() but does not keep the newline character =============================================================================== */int io_GetLine(char *buffer, int maxlen, FILE *fp){ int len = 0, c; char *ptr = buffer; char *endptr = buffer + maxlen - 1; if (fp != NULL) { while ((c = fgetc(fp)) != EOF) { if (c == '\n') break; *ptr = c; ptr++; len++; if (ptr == endptr) break; } } *ptr = '\0'; return(len);}/* =============================================================================== Function: io_WriteAllRecordsToFile Args: Pointer to list Returns: Number of items written. Purpose: Save all Record structs in the List to a disk file =============================================================================== */int io_WriteAllRecordsToFile(struct Node *List){ unsigned char status = ST_DELETED; if ((fp = fopen(STR_FILENAME, "wb")) == NULL) { /* Failed to open database file */ printf("ERROR: Unable to open phone book file: %s\n", STR_FILENAME); return(0); } /* First delete dead records from the list, then write them to disk */ gl_AppData.MainList = li_DeleteNodeAndData(gl_AppData.MainList, &status, rec_CompareStatus); (void)li_Traverse(List, io_WriteRecordToFile); (void)fclose(fp); gl_AppData.DataChanged = FALSE; return(gl_HighestID);}/* =============================================================================== Function: io_WriteRecordToFile Args: Pointer to record structure to be written. Returns: Nothing Purpose: Sub-function to write a single Record struct to disk =============================================================================== */void io_WriteRecordToFile(void *ptr){ struct Record *rec = ptr; if (rec->Status & ST_DELETED) return; /* Don't save deleted records */ if (fp == NULL) { fprintf(stderr, "ERROR: Attempting to write to unopened file\n"); return; } if (fwrite(rec, sizeof(struct Record), 1, fp) != 1) perror("WriteRecordToFile");}/* =============================================================================== Function: io_LoadAppConfig Args: None Returns: Nothing Purpose: Loads the appconfig structure from disk =============================================================================== */void io_LoadAppConfig(void){ FILE *myfp; struct appconfig tmpcfg; if ((myfp = fopen(STR_CFG_FILENAME, "rb")) == NULL) return; if (fread(&tmpcfg, sizeof(struct appconfig), 1, myfp) == 1) gl_AppCfg = tmpcfg; (void)fclose(myfp);}/* =============================================================================== Function: io_SaveAppConfig Args: None Returns: Nothing Purpose: Saves the appconfig structure to disk =============================================================================== */void io_SaveAppConfig(void){ FILE *myfp; if ((myfp = fopen(STR_CFG_FILENAME, "wb")) == NULL) return; if (fwrite(&gl_AppCfg, sizeof(struct appconfig), 1, myfp) != 1) perror (STR_CFG_FILENAME); (void)fclose(myfp);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -