代码:
1 //This is c program code! 2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= 3 * 文档信息: *** :~/WORKM/stutyCode/linuxPrograming/manageDisk/diskManage.c 4 * 版权声明: *** :(魎魍魅魑)MIT 5 * 联络信箱: *** :guochaoxxl@163.com 6 * 创建时间: *** :2020年12月12日的上午10:43 7 * 文档用途: *** :数据结构与算法分析-c语言描述 8 * 作者信息: *** :guochaoxxl(http://cnblogs.com/guochaoxxl) 9 * 修订时间: *** :2020年第49周 12月12日 星期六 上午10:43 (第347天) 10 * 文件描述: *** :自行添加 11 * *+=+=+=+=* *** *+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/ 12 13 /* 14 CD Database Application 15 16 Beginning Linux Programming 17 18 Version: Terminals 19 20 Copyright (c) 1996,2007 Wrox Press 21 22 This program is free software; you can redistribute it and/or modify 23 it under the terms of the GNU General Public License as published by 24 the Fee Software Foundation; either version 2 of the License, or (at 25 your option) any later version. 26 27 This program is distributed in the hopes that it will be useful, but 28 WITHOUT ANY WARRANTY; without even the implied warranty of 29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 30 General Public License for more details. 31 32 You should have received a copy of the GNU General Public License 33 along with this program; if not, write to the Free Software 34 Foundation, Inc. 675 Mass Ave, Cambridge, MA 02139, USA. 35 36 */ 37 38 /* 39 Notes 40 41 This version of the CD database application has been written 42 using the information presented in the Terminals chapter. 43 44 It is derived from the shell script presented in the Shell 45 Programming chapter. It has not been redesigned for the C 46 implementation, so many features of the shell original can 47 still be seen in this version. 48 49 There are some problems with this implementation that will 50 be resolved in later revisions: 51 52 It does not deal with commas in titles. 53 It has a practical limit on tracks per CD to keep them on screen. 54 55 The program deliberately uses the standard input and output 56 file streams. It does not deal with re-directed input or 57 output explicitly. */ 58 59 #include "diskManage.h" 60 61 char const *title_file = "title.cdb"; 62 char const *tracks_file = "tracks.cdb"; 63 char const *temp_file = "cdb.tmp"; 64 65 void draw_menu(char *options[], int current_highlight, int start_row, int start_col){ 66 int current_row = 0; 67 char **option_ptr; 68 char *txt_ptr; 69 70 option_ptr = options; 71 while (*option_ptr) { 72 if (current_row == current_highlight) attron(A_STANDOUT); 73 txt_ptr = options[current_row]; 74 txt_ptr++; 75 mvprintw(start_row + current_row, start_col, "%s", txt_ptr); 76 if (current_row == current_highlight) attroff(A_STANDOUT); 77 current_row++; 78 option_ptr++; 79 } 80 81 mvprintw(start_row + current_row + 3, start_col, "Move highlight then press Return "); 82 83 refresh(); 84 } 85 86 /* 87 clear_all_screen Clear the screen and re-write the title. If a CD is selected then display the information. 88 */ 89 void clear_all_screen(){ 90 clear(); 91 mvprintw(2, Q_LINE, "%s", "CD Database Application"); 92 if (current_cd[0]) { 93 mvprintw(ERROR_LINE, 0, "Current CD: %s: %s ", current_cat, current_cd); 94 } 95 refresh(); 96 } 97 98 /* 99 get_return Prompt for and read a carriage return. Ignore other characters. 100 */ 101 void get_return(){ 102 int ch; 103 mvprintw(23, 0, "%s", " Press return "); 104 refresh(); 105 while ((ch = getchar()) != ' ' && ch != EOF); 106 } 107 108 /* 109 get_confirm Prompt for and read confirmation. Read a string and check first character for Y or y. On error or other character return no confirmation. 110 */ 111 int get_confirm(){ 112 int confirmed = 0; 113 char first_char = 'N'; 114 115 mvprintw(Q_LINE, 5, "Are you sure? "); 116 clrtoeol(); 117 refresh(); 118 119 cbreak(); 120 first_char = getch(); 121 if (first_char == 'Y' || first_char == 'y') { 122 confirmed = 1; 123 } 124 nocbreak(); 125 126 if (!confirmed) { 127 mvprintw(Q_LINE, 1, " Cancelled"); 128 clrtoeol(); 129 refresh(); 130 sleep(1); 131 } 132 return confirmed; 133 } 134 135 /* 136 getchoice - ask the user to choose passed: greet, an introduction 137 */ 138 int getchoice(char *greet, char *choices[]){ 139 static int selected_row = 0; 140 int max_row = 0; 141 int start_screenrow = MESSAGE_LINE, start_screencol = 10; 142 char **option; 143 int selected; 144 int key = 0; 145 146 option = choices; 147 while (*option) { 148 max_row++; 149 option++; 150 } 151 152 /* protect against menu getting shorted when CD deleted */ 153 if (selected_row >= max_row) 154 selected_row = 0; 155 156 clear_all_screen(); 157 mvprintw(start_screenrow - 2, start_screencol, greet); 158 159 keypad(stdscr, TRUE); 160 cbreak(); 161 noecho(); 162 163 key = 0; 164 while (key != 'q' && key != KEY_ENTER && key != ' ') { 165 if (key == KEY_UP) { 166 if (selected_row == 0) 167 selected_row = max_row - 1; 168 else 169 selected_row--; 170 } 171 if (key == KEY_DOWN) { 172 if (selected_row == (max_row - 1)) 173 selected_row = 0; 174 else 175 selected_row++; 176 } 177 selected = *choices[selected_row]; 178 draw_menu(choices, selected_row, start_screenrow, start_screencol); 179 key = getch(); 180 } 181 182 keypad(stdscr, FALSE); 183 nocbreak(); 184 echo(); 185 186 if (key == 'q') 187 selected = 'q'; 188 189 return (selected); 190 } 191 192 /* 193 Database File Manipulation Functions 194 */ 195 196 /* 197 insert_title Add a title to the CD database Simply add the title string to the end of the titles file 198 */ 199 void insert_title(char *cdtitle){ 200 FILE *fp = fopen(title_file, "a"); 201 if (!fp) { 202 mvprintw(ERROR_LINE, 0, "cannot open CD titles database"); 203 } else { 204 fprintf(fp, "%s ", cdtitle); 205 fclose(fp); 206 } 207 } 208 209 /* 210 get_string At the current screen position prompt for and read a string Delete any trailing newline. 211 */ 212 void get_string(char *string){ 213 int len; 214 215 wgetnstr(stdscr, string, MAX_STRING); 216 len = strlen(string); 217 if (len > 0 && string[len - 1] == ' ') 218 string[len - 1] = '