/* Collection of code snippets by Arne VajhÃ¸j */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
/*
 *  NICE - utility
 *
 *  Programmed: Arne Vajhøj (august 1990)
 *
 */

/* code-generation help-routines */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

#define EXTERN extern
#include "glob.h"

void newl()
{
   int i;
   if(notfirst) {
      if (skipnewl) {
         skipnewl = FALSE;
      } else {
         printf("\n");
         if (indfact>=0) {
            for (i=0;i<ind*indfact+ind2;i++) printf(" ");
         } else {
            for (i=0;i<ind;i++) printf("\t");
            for (i=0;i<ind2;i++) printf(" ");
         }
      }
   }
   notfirst = TRUE;
   return;
}

void cvtupp(char *s1,char *s2)
{
   char *ss1,*ss2;
   ss2 = s2;
   for(ss1=s1;*ss1!='\0';ss1++) {
      if (islower((int) *ss1))
         *ss2 = toupper(*ss1);
      else
         *ss2 = *ss1;
      ss2++;
   }
   *ss2 = '\0';
   return;
}

void cvtlow(char *s1,char *s2)
{
   char *ss1,*ss2;
   ss2 = s2;
   for(ss1=s1;*ss1!='\0';ss1++) {
      if (isupper((int) *ss1))
         *ss2 = tolower(*ss1);
      else
         *ss2 = *ss1;
      ss2++;
   }
   *ss2 = '\0';
   return;
}

void kwrdcvt(char *s)
{
   if (*s == '\0') return;
   switch (keywrdstyle) {
      case UPPCAS: cvtupp(s,s);
                   break;
      case LOWCAS: cvtlow(s,s);
                   break;
      case MIXCAS: cvtlow(s,s);
                   *s = toupper(*s);
                   break;
   }
   return;
}

void idcvt(char *s)
{
   if (*s == '\0') return;
   switch (idstyle) {
      case UPPCAS: cvtupp(s,s);
                   break;
      case LOWCAS: cvtlow(s,s);
                   break;
      case MIXCAS: cvtlow(s,s);
                   *s = toupper(*s);
                   break;
   }
   return;
}

void gen(char *item)
{
   char tmp[max_name];
   char *s;
   if (*item == '\0') return;
   s = (char *) &tmp;
   strcpy(s,item);
   kwrdcvt(s);
   printf("%s",s);
   return;
}

void outcomm(int chalin1,int chalin2,int force)
{
   int i,l;
   l = strlen(comm);
   if (l<=0) {
      if (force && forcecomm) {
         strcpy(comm,MISSCOMM);
      } else {
         return;
      }
   }
   l = strlen(comm);
   if (chalin1) newl(); else printf(" ");
   if (commstyle != 1) printf("(*"); else printf("{");
   for (i=0;i<l;i++) {
      if (comm[i] == '\n') {
         newl();
      } else {
         printf("%c",comm[i]);
      }
   }
   if (commstyle != 1) printf("*)"); else printf("}");
   if (chalin2) newl();
   comm = nice_realloc(comm,1);
   comm[0] = '\0';
   return;
}
