
#include <stdio.h>
#include <string.h>
#include "misc.h"      /* NAME_LIST_ENTRY_TYPE, get_code() */
#include "io_nc.h"     /* get_quotation() */
#include "ioserv.h"    /* PARAMETER_LIST_ENTRY_TYPE, report_msg() */

#if PROTOTYPE_ALLOWED
int get_parameters(FILE *fp,
                   PARAMETER_LIST_ENTRY_TYPE *pl,
                   NAME_LIST_TYPE *nl_array)
#else
int get_parameters(fp, pl, nl_array)
FILE *fp;
PARAMETER_LIST_ENTRY_TYPE *pl;
NAME_LIST_TYPE *nl_array;
#endif
{
   char format[80];
   char trans_string[80];   /* to hold a word before translation */
   char buff[255], buff2[255];
   char *target;      /* either trans_string or pl[i].parameter */
   int  i_trans,     /* index into name list array */
        f_trans;     /* flag */
   int  i=0, j=0;

   while ((pl[i].prompt) != NULL)
   {
      if (pl[i].format != NULL)   /* there should be a parameter */
      {
         strcat(strcpy(format, pl[i].prompt), pl[i].format);
         f_trans = (pl[i].type%256) == TYPE_TRANS;
         target =  f_trans         ?
                   trans_string    :   /* translation required */
                   (char *) pl[i].parameter ;   /* no translation required */
         if (pl[i].type == TYPE_QUOTE)
         {
            if (fscanf(fp, format) != 0)
            {
               sprintf(buff, "\n ERROR in get_parameters: Reading %s\n\n",
                  pl[i].prompt);
               report_msg(buff);
               return(1);         /* error exit */
            }
            get_quotation(fp, target, 80);
         }
         else
         {
            if (fscanf(fp, format, target) != 1)
            {
               sprintf(buff, "\n ERROR in get_parameters: Reading %s\n\n",
                  pl[i].prompt);
               report_msg(buff);
               return(1);         /* error exit */
            }
            if (f_trans)   /* translate the string */
            {
               i_trans = pl[i].type/256;
               if ((*((int *)pl[i].parameter) =
                    get_code(nl_array[i_trans], target)) == BADINT) {
                    sprintf(buff, "\n ERROR in get_parameters: invalid input %s %s\n\n",
                            pl[i].prompt, target);
                    report_msg(buff);
                    report_msg("\n Valid options are: \n");
                    while((nl_array[i_trans]+j)->name != NULL) {
                      sprintf(buff,"      %s\n",(nl_array[i_trans]+j)->name);
                      report_msg(buff);
                      j++;
                      }
                    return(1);         /* error exit */
                 }
            }
         }
      }
      else if ( (fscanf(fp, " %s", buff) != 1) ||
                (sscanf(pl[i].prompt, " %s", buff2) != 1) ||
                 strcmp(buff, buff2) )
                                                   /* no parameter expected */
      {
         sprintf(buff, "\n ERROR in get_parameters: Did not find %s\n\n",
            pl[i].prompt);
         report_msg(buff);
         return(1);         /* error exit */
      }
      i++;      /* Either an empty prompt or a
                  prompt plus parameter has been read. */
   }
   return(0);   /* normal exit; no error */
}                       /* get_parameters() */

#if PROTOTYPE_ALLOWED
int print_parameters(FILE *fp,
                     PARAMETER_LIST_ENTRY_TYPE *pl,
                     NAME_LIST_TYPE *nl_array,
                     char *separator)
#else
int print_parameters(fp, pl, nl_array, separator)
FILE *fp;
PARAMETER_LIST_ENTRY_TYPE *pl;
NAME_LIST_TYPE *nl_array;
char *separator;
#endif
{
   char format[80];
   int i=0;
   int error = 0;

   while ((pl[i].prompt != NULL) && !error)
   {
      fprintf(fp, "%s", separator);
      if (pl[i].format != NULL)   /* there is a parameter */
      {
         strcat(strcpy(format, pl[i].prompt), pl[i].format);
         switch (pl[i].type%256)
         {
            case TYPE_CHAR:
               fprintf(fp, format, *((char *) pl[i].parameter));
               break;
            case TYPE_INT:
               fprintf(fp, format, *((int *) pl[i].parameter));
               break;
            case TYPE_LONG:
               fprintf(fp, format, *((long int*) pl[i].parameter));
               break;
            case TYPE_FLOAT:
               fprintf(fp, format, *((float *) pl[i].parameter));
               break;
            case TYPE_DOUBLE:
               fprintf(fp, format, *((double *) pl[i].parameter));
               break;
            case TYPE_QUOTE:
            case TYPE_STRING:
               fprintf(fp, "%s %s", pl[i].prompt, (char *) pl[i].parameter);
               break;
            case TYPE_TRANS:
               fprintf(fp, "%s %s", pl[i].prompt,
                  get_name( nl_array[pl[i].type/256],
                  *((int *)pl[i].parameter) ) );
               break;

            default:
               error = 1;
         }
      }
      else   /* there is no parameter, just a prompt */
         fprintf(fp, "\n%s%s", separator, pl[i].prompt);
         /* new line before a prompt without a parameter, since
            it is presumed to signal the start of a new group.
         */
      i++;
   }
   return(error);
}                       /* print_parameters() */

