重构的过程记录--之分模块:

代码:

  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] = '';
219 }
220 
221 /*
222    add_record Add a new CD to the collection
223  */
224 void add_record(){
225     char catalog_number[MAX_STRING];
226     char cd_title[MAX_STRING];
227     char cd_type[MAX_STRING];
228     char cd_artist[MAX_STRING];
229     char cd_entry[MAX_STRING];
230 
231     int screenrow = MESSAGE_LINE;
232     int screencol = 10;
233 
234     clear_all_screen();
235     mvprintw(screenrow, screencol, "Enter new CD details");
236     screenrow += 2;
237 
238     mvprintw(screenrow, screencol, "Catalog Number: ");
239     get_string(catalog_number);
240     screenrow++;
241 
242     mvprintw(screenrow, screencol, "      CD Title: ");
243     get_string(cd_title);
244     screenrow++;
245 
246     mvprintw(screenrow, screencol, "       CD Type: ");
247     get_string(cd_type);
248     screenrow++;
249 
250     mvprintw(screenrow, screencol, "        Artist: ");
251     get_string(cd_artist);
252     screenrow++;
253 
254     mvprintw(15, 5, "About to add this new entry:");
255     sprintf(cd_entry, "%s,%s,%s,%s", catalog_number, cd_title, cd_type, cd_artist);
256     mvprintw(17, 5, "%s", cd_entry);
257     refresh();
258 
259     move(PROMPT_LINE, 0);
260     if (get_confirm()) {
261     insert_title(cd_entry);
262     strcpy(current_cd, cd_title);
263     strcpy(current_cat, catalog_number);
264     }
265 }
266 
267 /*
268    count_cds - scan the database and count titles and tracks
269  */
270 void count_cds(){
271     FILE *titles_fp, *tracks_fp;
272     char entry[MAX_ENTRY];
273     int titles = 0;
274     int tracks = 0;
275 
276     titles_fp = fopen(title_file, "r");
277     if (titles_fp) {
278     while (fgets(entry, MAX_ENTRY, titles_fp))
279         titles++;
280     fclose(titles_fp);
281     }
282     tracks_fp = fopen(tracks_file, "r");
283     if (tracks_fp) {
284     while (fgets(entry, MAX_ENTRY, tracks_fp))
285         tracks++;
286     fclose(tracks_fp);
287     }
288     mvprintw(ERROR_LINE, 0, "Database contains %d titles, with a total of %d tracks.", titles, tracks);
289     get_return();
290 }
291 
292 /*
293    find_cd - locate a CD in the database prompt for a substring to match in the database set current_cd to the CD title
294  */
295 void find_cd(){
296     char match[MAX_STRING], entry[MAX_ENTRY];
297     FILE *titles_fp;
298     int count = 0;
299     char *found, *title, *catalog;
300 
301     mvprintw(Q_LINE, 0, "Enter a string to search for in CD titles: ");
302     get_string(match);
303 
304     titles_fp = fopen(title_file, "r");
305     if (titles_fp) {
306     while (fgets(entry, MAX_ENTRY, titles_fp)) {
307 
308         /* Skip past catalog number */
309         catalog = entry;
310         if (found = strstr(catalog, ",")) {
311         *found = 0;
312         title = found + 1;
313 
314         /* Zap the next comma in the entry to reduce it to title only */
315         if (found = strstr(title, ",")) {
316             *found = '';
317 
318             /* Now see if the match substring is present */
319             if (found = strstr(title, match)) {
320             count++;
321             strcpy(current_cd, title);
322             strcpy(current_cat, catalog);
323             }
324         }
325         }
326     }
327     fclose(titles_fp);
328     }
329     if (count != 1) {
330     if (count == 0)
331         mvprintw(ERROR_LINE, 0, "Sorry, no matching CD found. ");
332     if (count > 1)
333         mvprintw(ERROR_LINE, 0, "Sorry, match is ambiguous: %d CDs found. ", count);
334     current_cd[0] = '';
335     get_return();
336     }
337 }
338 
339 
340 /*
341    remove_tracks - delete tracks from the current CD
342  */
343 void remove_tracks(){
344     FILE *tracks_fp, *temp_fp;
345     char entry[MAX_ENTRY + 1];
346     int cat_length;
347 
348     if (current_cd[0] == '')
349     return;
350 
351     cat_length = strlen(current_cat);
352 
353     tracks_fp = fopen(tracks_file, "r");
354     if (tracks_fp == (FILE *)NULL) return;
355     temp_fp = fopen(temp_file, "w");    
356 
357     while (fgets(entry, MAX_ENTRY, tracks_fp)) {
358     /* Compare catalog number and copy entry if no match */
359     if (strncmp(current_cat, entry, cat_length) != 0)
360         fputs(entry, temp_fp);
361     }
362     fclose(tracks_fp);
363     fclose(temp_fp);
364 
365     unlink(tracks_file);
366     rename(temp_file, tracks_file);
367 }
368 
369 /*
370    remove_cd - delete the current CD from the database
371  */
372 void remove_cd(){
373     FILE *titles_fp, *temp_fp;
374     char entry[MAX_ENTRY];
375     int cat_length;
376 
377     if (current_cd[0] == '')
378     return;
379 
380     clear_all_screen();
381     mvprintw(PROMPT_LINE, 0, "About to remove CD %s: %s. ", current_cat, current_cd);
382     if (!get_confirm())
383     return;
384 
385     cat_length = strlen(current_cat);
386 
387     /* Copy the titles file to a temporary, ignoring this CD */
388     titles_fp = fopen(title_file, "r");
389     temp_fp = fopen(temp_file, "w");
390 
391     while (fgets(entry, MAX_ENTRY, titles_fp)) {
392     /* Compare catalog number and copy entry if no match */
393     if (strncmp(current_cat, entry, cat_length) != 0)
394         fputs(entry, temp_fp);
395     }
396     fclose(titles_fp);
397     fclose(temp_fp);
398 
399     /* Delete the titles file, and rename the temporary file */
400     unlink(title_file);
401     rename(temp_file, title_file);
402 
403     /* Now do the same for the tracks file */
404     remove_tracks();
405 
406     /* Reset current CD to 'None' */
407     current_cd[0] = '';
408 }
409 
410 /*
411    list_tracks - list the tracks for the current CD
412  */
413 void list_tracks(){
414     FILE *tracks_fp;
415     char entry[MAX_ENTRY];
416     int cat_length;
417     int lines_op = 0;
418     WINDOW *track_pad_ptr;
419     int tracks = 0;
420     int key;
421     int first_line = 0;
422 
423     if (current_cd[0] == '') {
424     mvprintw(ERROR_LINE, 0, "You must select a CD first. ", stdout);
425     get_return();
426     return;
427     }
428     clear_all_screen();
429     cat_length = strlen(current_cat);
430 
431     /* First count the number of tracks for the current CD */
432     tracks_fp = fopen(tracks_file, "r");
433     if (!tracks_fp)
434     return;
435     while (fgets(entry, MAX_ENTRY, tracks_fp)) {
436     if (strncmp(current_cat, entry, cat_length) == 0)
437         tracks++;
438     }
439     fclose(tracks_fp);
440 
441     /* Make a new pad, ensure that even if there is only a single
442        track the PAD is large enough so the later prefresh() is always
443        valid.
444      */
445     track_pad_ptr = newpad(tracks + 1 + BOXED_LINES, BOXED_ROWS + 1);
446     if (!track_pad_ptr)
447     return;
448 
449     tracks_fp = fopen(tracks_file, "r");
450     if (!tracks_fp)
451     return;
452 
453     mvprintw(4, 0, "CD Track Listing
");
454 
455     /* write the track information into the pad */
456     while (fgets(entry, MAX_ENTRY, tracks_fp)) {
457     /* Compare catalog number and output rest of entry */
458     if (strncmp(current_cat, entry, cat_length) == 0) {
459         mvwprintw(track_pad_ptr, lines_op++, 0, "%s", entry + cat_length + 1);
460     }
461     }
462     fclose(tracks_fp);
463 
464     if (lines_op > BOXED_LINES) {
465     mvprintw(MESSAGE_LINE, 0, "Cursor keys to scroll, RETURN or q to exit");
466     } else {
467     mvprintw(MESSAGE_LINE, 0, "RETURN or q to exit");
468     }
469     wrefresh(stdscr);
470     keypad(stdscr, TRUE);
471     cbreak();
472     noecho();
473 
474     key = 0;
475     while (key != 'q' && key != KEY_ENTER && key != '
') {
476     if (key == KEY_UP) {
477         if (first_line > 0)
478         first_line--;
479     }
480     if (key == KEY_DOWN) {
481         if (first_line + BOXED_LINES + 1 < tracks)
482         first_line++;
483     }
484     /* now draw the appropriate part of the pad on the screen */
485     prefresh(track_pad_ptr, first_line, 0,
486          BOX_LINE_POS, BOX_ROW_POS,
487          BOX_LINE_POS + BOXED_LINES, BOX_ROW_POS + BOXED_ROWS);
488 /*    wrefresh(stdscr); */
489     key = getch();
490     }
491 
492     delwin(track_pad_ptr);
493     keypad(stdscr, FALSE);
494     nocbreak();
495     echo();
496 }
497 
498 /*
499    update_cd - re-enter tracks for current CD deletes all tracks for the current CD in the database and then prompts for new ones.
500  */
501 void update_cd(){
502     FILE *tracks_fp;
503     char track_name[MAX_STRING];
504     int len;
505     int track = 1;
506     int screen_line = 1;
507     WINDOW *box_window_ptr;
508     WINDOW *sub_window_ptr;
509 
510     clear_all_screen();
511     mvprintw(PROMPT_LINE, 0, "Re-entering tracks for CD. ");
512     if (!get_confirm())
513     return;
514     move(PROMPT_LINE, 0);
515     clrtoeol();
516 
517     remove_tracks();
518 
519     mvprintw(MESSAGE_LINE, 0, "Enter a blank line to finish");
520 
521     tracks_fp = fopen(tracks_file, "a");
522 
523     /* Just to show how, enter the information in a scrolling, boxed,
524        window. The trick is to set-up a sub-window, draw a box around the
525        edge, then add a new, scrolling, sub-window just inside the boxed
526        sub-window. */
527     box_window_ptr = subwin(stdscr, BOXED_LINES + 2, BOXED_ROWS + 2,
528                 BOX_LINE_POS - 1, BOX_ROW_POS - 1);
529     if (!box_window_ptr)
530     return;
531     box(box_window_ptr, ACS_VLINE, ACS_HLINE);
532 
533     sub_window_ptr = subwin(stdscr, BOXED_LINES, BOXED_ROWS,
534                 BOX_LINE_POS, BOX_ROW_POS);
535     if (!sub_window_ptr)
536     return;
537     scrollok(sub_window_ptr, TRUE);
538     werase(sub_window_ptr);
539     touchwin(stdscr);
540 
541     do {
542 
543     mvwprintw(sub_window_ptr, screen_line++, BOX_ROW_POS + 2, "Track %d: ", track);
544     clrtoeol();
545     refresh();
546     wgetnstr(sub_window_ptr, track_name, MAX_STRING);
547     len = strlen(track_name);
548     if (len > 0 && track_name[len - 1] == '
')
549         track_name[len - 1] = '';
550 
551     if (*track_name)
552         fprintf(tracks_fp, "%s,%d,%s
", current_cat, track, track_name);
553     track++;
554     if (screen_line > BOXED_LINES - 1) {
555         /* time to start scrolling */
556         scroll(sub_window_ptr);
557         screen_line--;
558     }
559     } while (*track_name);
560     delwin(sub_window_ptr);
561 
562     fclose(tracks_fp);
563 }

代码:

  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 #ifndef diskManage_h
 59 #define diskManage_h
 60 
 61 #include <unistd.h>
 62 #include <stdlib.h>
 63 #include <stdio.h>
 64 #include <string.h>
 65 #include <curses.h>
 66 
 67 #define MAX_STRING (80)        /* Longest allowed response       */
 68 #define MAX_ENTRY (1024)    /* Longest allowed database entry */
 69 
 70 #define MESSAGE_LINE 6        /* Misc. messages go here         */
 71 #define ERROR_LINE   22        /* The line to use for errors     */
 72 #define Q_LINE       20        /* Line for Questions             */
 73 #define PROMPT_LINE  18        /* Line for prompting on          */
 74 
 75 /*
 76    Some defines we use only for showing or entering the track information
 77  */
 78 #define BOXED_LINES    11
 79 #define BOXED_ROWS     60
 80 #define BOX_LINE_POS   8
 81 #define BOX_ROW_POS    2
 82 
 83 /*
 84    The variable current_cd is used to store the CD title we are working with. It is initialized so that the first character is NUL to indicate 'no CD selected'. The  is strictly unnecessary, but serves to emphasize the point.The variable current_cat will be used to record the catalog number of the current CD.
 85  */
 86 
 87 static char current_cd[MAX_STRING] = "";
 88 static char current_cat[MAX_STRING];
 89 
 90 extern const char *title_file;
 91 extern const char *tracks_file;
 92 extern const char *temp_file;
 93 
 94 /* Prototypes for local functions */
 95 void clear_all_screen(void);
 96 void get_return(void);
 97 int get_confirm(void);
 98 int getchoice(char *greet, char *choices[]);
 99 void draw_menu(char *options[], int highlight, int start_row, int start_col);
100 void insert_title(char *cdtitle);
101 void get_string(char *string);
102 void add_record(void);
103 void count_cds(void);
104 void find_cd(void);
105 void list_tracks(void);
106 void remove_tracks(void);
107 void remove_cd(void);
108 void update_cd(void);
109 
110 /*
111    clear_all_screen Clear the screen and re-write the title. If a CD is selected then display the information.
112  */
113 void clear_all_screen();
114 
115 
116 /*
117    get_return Prompt for and read a carriage return.Ignore other characters.
118  */
119 void get_return();
120 
121 /*
122    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.
123  */
124 int get_confirm();
125 
126 /*
127    getchoice - ask the user to choose passed: greet, an introduction choices, an array of strings, NULL at end
128  */
129 int getchoice(char *greet, char *choices[]);
130 
131 void draw_menu(char *options[], int current_highlight, int start_row, int start_col);
132 
133 /*
134    Database File Manipulation Functions
135  */
136 
137 /*
138    insert_title Add a title to the CD database Simply add the title string to the end of the titles file
139  */
140 void insert_title(char *cdtitle);
141 
142 
143 /*
144    get_string At the current screen position prompt for and read a string Delete any trailing newline.
145  */
146 void get_string(char *string);
147 
148 /*
149    add_record Add a new CD to the collection
150  */
151 
152 void add_record();
153 
154 /*
155    count_cds - scan the database and count titles and tracks
156  */
157 void count_cds();
158 
159 /*
160    find_cd - locate a CD in the database prompt for a substring to match in the database set current_cd to the CD title
161  */
162 void find_cd();
163 
164 /*
165    remove_tracks - delete tracks from the current CD
166  */
167 void remove_tracks();
168 
169 /*
170    remove_cd - delete the current CD from the database
171  */
172 void remove_cd();
173 
174 /*
175    list_tracks - list the tracks for the current CD
176  */
177 void list_tracks();
178 
179 /*
180    update_cd - re-enter tracks for current CD deletes all tracks for the current CD in the database and then prompts for new ones.
181  */
182 void update_cd();
183 
184 #endif

test文件:

  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 //static char current_cd[MAX_STRING] = "";
 62 //static char current_cat[MAX_STRING];
 63 
 64 /*
 65    Menu structures. The first character is the character to return when the chice is selected, the remaining text is to be displayed.
 66  */
 67 char *main_menu[] = {
 68     "aadd new CD",
 69     "ffind CD",
 70     "ccount CDs and tracks in the catalog",
 71     "qquit",
 72     0,
 73 };
 74 
 75 /*
 76    The extended menu is displayed when a CD is currently selected
 77  */
 78 char *extended_menu[] = {
 79     "aadd new CD",
 80     "ffind CD",
 81     "ccount CDs and tracks in the catalog",
 82     "llist tracks on current CD",
 83     "rremove current CD",
 84     "uupdate track information",
 85     "qquit",
 86     0,
 87 };
 88 
 89 int main(int argc, char **argv)
 90 {
 91     int choice;
 92     initscr();
 93 
 94     do {
 95     choice = getchoice("Options:", current_cd[0] ? extended_menu : main_menu);
 96     switch (choice) {
 97     case 'q':
 98         break;
 99 
100     case 'a':
101         add_record();
102         break;
103 
104     case 'c':
105         count_cds();
106         break;
107 
108     case 'f':
109         find_cd();
110         break;
111 
112     case 'l':
113         list_tracks();
114         break;
115 
116     case 'r':
117         remove_cd();
118         break;
119 
120     case 'u':
121         update_cd();
122         break;
123     }
124     } while (choice != 'q');
125 
126     endwin();
127 
128     return 0;
129 }

Makefile文件:

1 #all: curses_app
2 #Uncomment and edit the line below if necessary
3 #CFLAGS=-I/usr/include/ncurses
4 LDFLAGS=-lcurses
5 testDiskManage:testDiskManage.c diskManage.c
6     gcc $^ -o $@ ${LDFLAGS}

顺利分了模块,但不是很彻底。

原文地址:https://www.cnblogs.com/guochaoxxl/p/14139945.html