// file: $(nedc_nfc)/class/cpp/cmdl/cmdl_01.cc // // This file contains methods that support help functions. // // local include files // #include "Cmdl.h" // method: expand_filename // // arguments: // char* fname_out: expanded filename (output) // const char* fname_in: original filename (input) // // return: a boolean value indicating status // // This method expands any environment variables that appear in a name. // bool Cmdl::expand_filename(char* fname_out_a, const char* fname_in_a) { // display a debug message // if (dbgl_d > Dbgl::BRIEF) { fprintf(stdout, "%s (line: %d) %s: input name (%s)\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, fname_in_a); } // loop to file an environment variable // char* pname = fname_out_a; while (*fname_in_a != (char)NULL) { if (*fname_in_a == DELIM_ENV_VAR) { fname_in_a++; // find the next file delimiter // char* delim = strchr((char*)fname_in_a, (int)DELIM_DIR); // create a string with the environment variable name // char buf[MAX_ENV_SIZE + 1]; strncpy(buf, fname_in_a, delim - fname_in_a); buf[delim - fname_in_a] = (char)NULL; // expand this variable // strcpy(pname, getenv(buf)); pname += strlen(pname); *pname++ = DELIM_DIR; // skip over the environment variable // fname_in_a = delim + 1; } else { *pname++ = *fname_in_a++; } } // finish the output string with a null character // *pname = (char)NULL; // display a debug message // if (dbgl_d > Dbgl::BRIEF) { fprintf(stdout, "%s line: %d) %s: output name (%s)\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, fname_out_a); } // exit gracefully // return true; } // method: display_usage // // arguments: // FILE* fp: file pointer to be used for i/o (input) // // return: a boolean value indicating status // // This method displays the usage message. // bool Cmdl::display_usage(FILE* fp_a) { // check for an open stream // if (fp_a == (FILE*)NULL) { return false; } // check for a null message // else if (usage_fname_d == (char*)NULL) { return false; } // open the usage file // FILE* fp_h; if ((fp_h = fopen(usage_fname_d, "r")) == (FILE*)NULL) { return false; } // loop over the file and read it line by line // char buf[MAX_HLINE_SIZE + 1]; while (fgets(buf, MAX_HLINE_SIZE, fp_h) != (char*)NULL) { // display the line // fprintf(fp_a, "%s", buf); } // close the file // fclose(fp_h); // exit gracefully: note we terminate // exit (EXIT_FAILURE); } // method: display_help // // arguments: // FILE* fp: file pointer to be used for i/o (input) // // return: a boolean value indicating status // // This method displays the usage message. // bool Cmdl::display_help(FILE* fp_a) { // check for an open stream // if (fp_a == (FILE*)NULL) { return false; } // check for a null message // else if (help_fname_d == (char*)NULL) { return false; } // open the help file // FILE* fp_h; if ((fp_h = fopen(help_fname_d, "r")) == (FILE*)NULL) { return false; } // loop over the file and read it line by line // char buf[MAX_HLINE_SIZE + 1]; while (fgets(buf, MAX_HLINE_SIZE, fp_h) != (char*)NULL) { // display the line // fprintf(fp_a, "%s", buf); } // close the file // fclose(fp_h); // exit gracefully: note we terminate // exit (EXIT_SUCCESS); } // // end of file