#define DONGROUT "$VER: DLGGuide  by Don Grout  v2.32  03-May-94"
// converts DLG's BBS message file into an AmigaGuide series of files

#define DLGGUIDE   "DLGMsgs.Guide"
#define NEWMSGAREA "Message Area ["
#define ENDMSGAREA ']'
#define NEWSUBJECT "[From    ] "
#define SUBJECT    "[Subject ]"
#define SUBJECTNUM 4

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifndef __MSDOS__
#include <exec/types.h>
#include <exec/memory.h>
#include <dos/datetime.h>
#include <workbench/startup.h>
#include <workbench/workbench.h>

#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/icon_protos.h>
#endif

#define TRUE  1
#define FALSE 0

#define AMIGAGUIDEICON 518

int AmigaGuideIcon[AMIGAGUIDEICON]= {
227, 16,  0,  1,  0,  0,  0,  0,  0, 47,  0, 16,  0, 54,  0, 23,  0,  4,  0,
  1,  0,  1,  0, 32,203,136,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  4, 50,  0, 32,207,240,  0, 32,203,
 72,  0,  0,  0, 29,  0,  0,  0,  5,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
 16,  0,  0,  0,  0,  0,  0, 54,  0, 22,  0,  2,  0,  1,138,248,  3,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,  4,  0,  0,  0,  0,  0,  0,  0, 12,  0,
  0,  0,  0,  0,  0,  0, 12,  0,  7,255,255,255,255,224, 12,  0,  4,  0,  0,
  0,  0, 48, 12,  0,  4,171,250,  0,  0, 40, 12,  0,  4,  0,  0,  0,  0, 36,
 12,  0,  4,191,234,170,170,162, 12,  0,  4,  0,  0,  0,  0, 63, 12,  0,  4,
170,170,170,170,168,140,  0,  4,  0,  0,  0,  0,  0,140,  0,  4,170,171,254,
170,168,140,  0,  4,  0,  0,  0,  0,  0,140,  0,  4,170,170,170,170,168,140,
  0,  4,  0,  0,  0,  0,  0,140,  0,  4,191,234,170,254,168,140,  0,  4,  0,
  0,  0,  0,  0,140,  0,  4,  0,  0,  0,  0,  0,140,  0,  7,255,255,255,255,
255,140,  0,  0,  0,  0,  0,  0,  0, 12,  0,  0,  0,  0,  0,  0,  0, 12,  0,
127,255,255,255,255,255,252,  0,255,255,255,255,255,255,248,  0,213, 85, 85,
 85, 85, 85, 80,  0,213, 85, 85, 85, 85, 85, 80,  0,208,  0,  0,  0,  0, 21,
 80,  0,211,255,255,255,255,197, 80,  0,211, 85,245,255,255,213, 80,  0,211,
255,255,255,255,217, 80,  0,211, 95,213, 85, 85, 93, 80,  0,211,255,255,255,
255,192, 80,  0,211, 85, 85, 85, 85, 87, 80,  0,211,255,255,255,255,255, 80,
  0,211, 85, 85,253, 85, 87, 80,  0,211,255,255,255,255,255, 80,  0,211, 85,
 85, 85, 85, 87, 80,  0,211,255,255,255,255,255, 80,  0,211, 95,213, 85,125,
 87, 80,  0,211,255,255,255,255,255, 80,  0,211,255,255,255,255,255, 80,  0,
208,  0,  0,  0,  0,  0, 80,  0,213, 85, 85, 85, 85, 85, 80,  0,213, 85, 85,
 85, 85, 85, 80,  0,128,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 25, 83, 89,
 83, 58, 85,116,105,108,105,116,105,101,115, 47, 65,109,105,103, 97, 71,117,
105,100,101,  0,  0,  0,  0,  8,  0,  0,  0, 31, 70, 73, 76, 69, 84, 89, 80,
 69, 61, 84, 69, 88, 84,124, 65, 83, 67, 73, 73,124, 65, 77, 73, 71, 65, 71,
 85, 73, 68, 69,  0
};

FILE *TxtOut, *InputFile, *OutputFile, *GuideFile, *IndexFile;
unsigned char SubjectString[SUBJECTNUM+2][255], MessageArea[255];
unsigned char FileArg[255], PathArg[255], FileDate[20];
unsigned char MsgFileName[255], IndexFileName[255];
char *Flag;
int SubjectCount, PathFlag;

#ifndef __MSDOS__
unsigned char WindowArg[255];
#endif

struct {int Num; int Skip; char Data[80];} MsgAreaList[99];
int MsgAreaSize, MsgAreaNum;

// Function Declaration *****************************************************

char *GetFileName(char *);
void master(FILE *);
void toc(FILE *);
void ver(FILE *);
void AddIndex(int,char *,char *);
void LoadMsgArea(char []);
int GetMsgArea(char []);
int IsSkip(int);
void SkipParagraph(FILE *,FILE *);
int SearchAndReplace(char *,char [],char []);
char *WordMonth(int);
void CleanExit(char *);

int main(int argc,char *argv[]) // *******************************************
{
char *Pointer;
unsigned char FileName[255], Line[255], xS[255];
int xI;
#ifdef DOS_DATETIME_H
char Date[80]="", Time[80]="";
struct FileLock *FLock;
struct FileInfoBlock *FIBlock;
struct DateTime *DTime;
#else
struct tm *TimePointer;
time_t SecsNow;
#endif

// Create an Argument String
strcpy(Line," ");

#ifndef __MSDOS__
if (!argc) {
 struct WBStartup  *WBenchMsg;
 struct WBArg      *WBenchArg;
 struct DiskObject *DskObj;
 struct Library    *IconBase;
 WBenchMsg=(struct WBStartup *)argv;
 WBenchArg=WBenchMsg->sm_ArgList;
 IconBase=OpenLibrary("icon.library",33);
 if (IconBase) {
  DskObj=GetDiskObject(WBenchArg->wa_Name);
  if (DskObj) {
   xI=0;
   while (DskObj->do_ToolTypes[xI]) {
    strcat(Line,DskObj->do_ToolTypes[xI]); strcat(Line," ");
    xI++;
    }
   FreeDiskObject(DskObj);
   }
  CloseLibrary(IconBase);
  }
 }
else
#endif
// Get Command Line Arguments
for (xI=1;xI < argc;xI++) {
 strcat(Line,argv[xI]); strcat(Line," ");
 }

#ifndef __MSDOS__
// Open Environment File - read this second so any args override the default env
if (InputFile=fopen("ENV:DLGGuide","rt"))
 while (fgets(xS,255,InputFile)) {
  if (xS[strlen(xS)-1] == '\n') xS[strlen(xS)-1]='\0';
  strcat(Line,xS); strcat(Line," ");
  }
fclose(InputFile);
#endif

strupr(Line);

// Parse Startup Arguments
#ifndef __MSDOS__
if ((Pointer=strstr(Line," WINDOW=")) != NULL) {
 strcpy(xS,Pointer+8);
 xI=strlen(xS)-strlen(strstr(xS," "));
 strncpy(WindowArg,xS,xI); WindowArg[xI]='\0';
 }
else
if (!argc) sprintf(WindowArg,"CON:10/10/620/180/%s",DONGROUT+6);
else strcpy(WindowArg,"");

if (strlen(WindowArg))
 if (!(TxtOut=fopen(WindowArg,"r+"))) {
  sprintf(xS,"DLGGuide Error - TxtOut=%s",WindowArg);
  CleanExit(xS);
  }
 else ;
else
#endif
 TxtOut=stdout;

fprintf(TxtOut,"%s\n\n",DONGROUT+6);
fflush(TxtOut);

// Parse Arguments
if ((Pointer=strstr(Line," MSGFILE=")) != NULL) {
 strcpy(xS,Pointer+9);
 xI=strlen(xS)-strlen(strstr(xS," "));
 strncpy(FileArg,xS,xI); FileArg[xI]='\0';
 LoadMsgArea(FileArg);
 }
if ((Pointer=strstr(Line," FILE=")) != NULL) {
 strcpy(xS,Pointer+6);
 xI=strlen(xS)-strlen(strstr(xS," "));
 strncpy(FileArg,xS,xI); FileArg[xI]='\0';
 }
else strcpy(FileArg,"");
if ((Pointer=strstr(Line," PATH=")) != NULL) {
 strcpy(xS,Pointer+6);
 xI=strlen(xS)-strlen(strstr(xS," "));
 strncpy(PathArg,xS,xI); PathArg[xI]='\0';
 if (strstr(Line," NOPATH ")) PathFlag=0; else PathFlag=1;
 }
else {
 strcpy(PathArg,""); PathFlag=0;
 }

// Test for the Right Arguments
if (strlen(FileArg) == 0) {
 fprintf(TxtOut,"Format Is - DLGGuide FILE=<inputfile> <options>\n\n");
 fprintf(TxtOut,"Options Are:\n");
 fprintf(TxtOut," PATH=<outputpath>  - path of where to send output files\n");
 fprintf(TxtOut," NOPATH             - don't place the path in output files\n");
 fprintf(TxtOut," MSGFILE=<filename> - name of message area file\n");
#ifndef __MSDOS__
 fprintf(TxtOut," WINDOW=<winspec>   - specify the window to use\n");
#endif
 CleanExit(" ");
 }

// Open Input File
if (!(InputFile=fopen(FileArg,"rt"))) {
 sprintf(xS,"DLGGuide Error - InputFile=fopen(%s,read)",FileArg);
 CleanExit(xS);
 }

// Get Input File Date & Time
#ifdef DOS_DATETIME_H
FIBlock=(struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock),
                                         MEMF_PUBLIC | MEMF_CLEAR);
if (FIBlock) {
DTime=(struct DateTime *)AllocMem(sizeof(struct DateTime),
                                  MEMF_PUBLIC | MEMF_CLEAR);
if (DTime) {
FLock=(struct FileLock *)Lock(FileArg,SHARED_LOCK);
if (FLock) {
if (Examine((BPTR)FLock,FIBlock)) {
 struct Library *LocaleBase;
 DTime->dat_Stamp=FIBlock->fib_Date;
 LocaleBase=OpenLibrary("locale.library",38);
 if (LocaleBase) {
  DTime->dat_Format=4;
  CloseLibrary(LocaleBase);
  }
 else DTime->dat_Format=FORMAT_DOS;
 DTime->dat_Flags=NULL;
 DTime->dat_StrDay=NULL;
 DTime->dat_StrDate=Date;
 DTime->dat_StrTime=Time;
 if (!DateToStr(DTime)) {
  strcpy(Date,"__-___-__"); strcpy(Time,"__:__:__");
  }
 sprintf(FileDate,"%s %s",Date,Time);
 }
UnLock((BPTR)FLock);
}
FreeMem(DTime,sizeof(struct DateTime));
}
FreeMem(FIBlock,sizeof(struct FileInfoBlock));
}
#else
time(&SecsNow);
TimePointer=localtime(&SecsNow);
sprintf(FileDate,"%02d-%3.3s-%02d %02d:%02d:%02d",
 TimePointer->tm_mday,WordMonth(TimePointer->tm_mon+1),TimePointer->tm_year,
 TimePointer->tm_hour,TimePointer->tm_min,TimePointer->tm_sec);
#endif

// Open Guide File
#ifndef __MSDOS__
sprintf(FileName,"%s%s.info",PathArg,DLGGUIDE);
if (!(GuideFile=fopen(FileName,"wb"))) {
 fprintf(TxtOut,"DLGGuide Warning - GuideFile.info=fopen(%s,write)\n",FileName);
 fflush(TxtOut);
 }
else {
 for (xI=0; xI<AMIGAGUIDEICON; xI++)
  fputc(AmigaGuideIcon[xI],GuideFile);
 fclose(GuideFile);
 }
#endif
sprintf(FileName,"%s%s",PathArg,DLGGUIDE);
if (!(GuideFile=fopen(FileName,"wt"))) {
 sprintf(xS,"DLGGuide Error - GuideFile=fopen(%s,write)",FileName);
 CleanExit(xS);
 }

if (PathFlag) fprintf(GuideFile,"@database \"%s%s\"\n",PathArg,DLGGUIDE);
else fprintf(GuideFile,"@database \"%s\"\n",DLGGUIDE);
master(GuideFile);
ver(GuideFile);

fprintf(GuideFile,"@node main \"DLG Messages - %s\"\n",FileDate);
toc(GuideFile);
fprintf(GuideFile,"\nSubjects - Message Areas\n\n");

// Get First Message Area
while (fgets(Line,255,InputFile))
 if (strncmp(Line,NEWMSGAREA,strlen(NEWMSGAREA)) == 0)
  break;

do {
 strcpy(FileName,GetFileName(Line));

 // Open Output File
 sprintf(MsgFileName,"%s%s",PathArg,FileName);
 if (!(OutputFile=fopen(MsgFileName,"wt"))) {
  sprintf(xS,"DLGGuide Error - OutputFile=fopen(%s,write)",MsgFileName);
  CleanExit(xS);
  }
 if (!PathFlag) strcpy(MsgFileName,FileName);

 // Open Index File
#ifndef __MSDOS__
 sprintf(IndexFileName,"%s%s.Index",PathArg,FileName);
#else
 sprintf(IndexFileName,"%sIndex",PathArg);
#endif
 if (!(IndexFile=fopen(IndexFileName,"wt"))) {
  sprintf(xS,"DLGGuide Error - IndexFile=fopen(%s,write)",IndexFileName);
  CleanExit(xS);
  }
 if (!PathFlag) sprintf(IndexFileName,"%s.Index",FileName);

 if (MsgAreaSize) MsgAreaNum=GetMsgArea(MessageArea);
 else MsgAreaNum=0;

 if (MsgAreaNum)
  fprintf(TxtOut,"Working on Message Area %3d [%s]\n",MsgAreaNum,MessageArea);
 else
  fprintf(TxtOut,"Working on Message Area [%s]\n",MessageArea);
 fflush(TxtOut);

 fprintf(IndexFile,"@database \"%s\"\n",IndexFileName);
 master(IndexFile);
 ver(IndexFile);
 fprintf(IndexFile,"@node main \"Message Area ");
 if (MsgAreaNum)
  fprintf(IndexFile,"%3d [%s]",MsgAreaNum,MessageArea);
 else
  fprintf(IndexFile,"[%s]",MessageArea);
 fprintf(IndexFile," - Index - %s\"\n",FileDate);
 toc(IndexFile);

 fprintf(OutputFile,"@database \"%s\"\n",MsgFileName);
 master(OutputFile);
 ver(OutputFile);
 fprintf(OutputFile,"@index \"%s/main\"\n",IndexFileName);
 if (MsgAreaNum)
  fprintf(OutputFile,"\n@node main \"Message Area %3d [%s]\"\n"
                    ,MsgAreaNum,MessageArea);
 else
  fprintf(OutputFile,"\n@node main \"Message Area [%s]\"\n",MessageArea);
 toc(OutputFile);
 fprintf(OutputFile,"@next subject1\n");
 fprintf(OutputFile,"\n@{\"Index\" link \"%s/main\"}\n",IndexFileName);
 fprintf(OutputFile,"@endnode\n");

 // Get Next Message Area
 SubjectCount=0;
 do {
  Flag=fgets(Line,255,InputFile);
  if (!strncmp(Line,NEWSUBJECT,strlen(NEWSUBJECT))) {
   // Start of a Subject Found - get Prev. to [Subject] line
   strcpy(SubjectString[1],Line);
   for (xI=2; xI<=SUBJECTNUM; xI++)
    fgets(SubjectString[xI],255,InputFile);

   // Reduce unnessary characters from [Subject] line
   SearchAndReplace(SubjectString[SUBJECTNUM],"\"","");
   strcpy(SubjectString[SUBJECTNUM+1],SubjectString[SUBJECTNUM]);
   strlwr(SubjectString[SUBJECTNUM+1]);
   SearchAndReplace(SubjectString[SUBJECTNUM+1]," ","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"re:","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"~","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"`","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"!","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"@","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"#","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"$","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"%","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"^","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"&","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"*","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"(","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],")","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"_","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"-","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"+","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"=","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"{","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"[","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"}","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"]","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"|","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"\\","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],":","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],";","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"'","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"<","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],",","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],">","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],".","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"?","");
   SearchAndReplace(SubjectString[SUBJECTNUM+1],"/","");

   if (stricmp(SubjectString[SUBJECTNUM+1],SubjectString[0])) {
    // New Subject Found
    SubjectCount++;
    AddIndex(SubjectCount,SubjectString[SUBJECTNUM],MsgFileName);
    if (SubjectCount != 1)
     fprintf(OutputFile,"@endnode\n");
    fprintf(OutputFile,"@node subject%d \"Message Area ",SubjectCount);
    if (MsgAreaNum)
     fprintf(OutputFile,"%3d [%s]",MsgAreaNum,MessageArea);
    else
     fprintf(OutputFile,"[%s]",MessageArea);
    fprintf(OutputFile," - Subject %d - %s\"\n",SubjectCount,FileDate);
    toc(OutputFile);
    if (SubjectCount != 1)
     fprintf(OutputFile,"@prev subject%d\n",SubjectCount-1);
    fprintf(OutputFile,"@next subject%d\n",SubjectCount+1);
    strcpy(SubjectString[0],SubjectString[SUBJECTNUM+1]);
    }

   // Write Subject Header
   for (xI=1; xI<SUBJECTNUM; xI++)
    fprintf(OutputFile,"%s",SubjectString[xI]);
   fprintf(OutputFile,"@{\"%s\" link \"%s/main\" %d}%s",SUBJECT,IndexFileName,
                         SubjectCount-1,SubjectString[SUBJECTNUM]+strlen(SUBJECT));

   if (IsSkip(MsgAreaNum)) SkipParagraph(InputFile,OutputFile);
   continue;
   }
  if (strncmp(Line,NEWMSGAREA,strlen(NEWMSGAREA)) == 0)
   break;
  else
   fprintf(OutputFile,"%s",Line);
  } while (Flag);

 fprintf(OutputFile,"@endnode\n");
 fprintf(OutputFile,"@node subject%d \"Message Area ",SubjectCount+1);
 if (MsgAreaNum)
  fprintf(OutputFile,"%3d [%s]",MsgAreaNum,MessageArea);
 else
  fprintf(OutputFile,"[%s]",MessageArea);
 fprintf(OutputFile," - %s\"\n",FileDate);
 toc(OutputFile);
 if (PathFlag)
  fprintf(OutputFile,"@next \"%s%s/main\"",PathArg,DLGGUIDE);
 else
  fprintf(OutputFile,"@next \"%s/main\"",DLGGUIDE);
 fprintf(OutputFile,"@prev subject%d\n",SubjectCount);
 fprintf(OutputFile,"\nThat's All Folks!\n\n");
 if (PathFlag)
  fprintf(OutputFile,"@{\"   OK   \" link \"%s%s/main\"}",PathArg,DLGGUIDE);
 else
  fprintf(OutputFile,"@{\"   OK   \" link \"%s/main\"}",DLGGUIDE);
 fprintf(OutputFile,"     @{\" Again! \" link subject1}");
 fprintf(OutputFile,"     @{\" Index  \" link \"%s/main\" %d}\n",IndexFileName
                                                                ,SubjectCount-1);
 fprintf(OutputFile,"@endnode\n");
 fclose(OutputFile);

 fprintf(IndexFile,"@endnode\n");
 fclose(IndexFile);

 fprintf(GuideFile,"@{\"  %4d  \" link \"%s/main\"}",SubjectCount,IndexFileName);
 fprintf(GuideFile," @{\"-\" system \"delete %s#?\"} ",MsgFileName); // VERSION 2.32
 if (MsgAreaNum)
  fprintf(GuideFile,"@{\"%3d. %s\" link \"%s/subject1\"}\n",MsgAreaNum
                                                         ,MessageArea,MsgFileName);
 else
  fprintf(GuideFile,"@{\"     %s\" link \"%s/subject1\"}\n",MessageArea
                                                           ,MsgFileName);
 } while (Flag);

fprintf(GuideFile,"@endnode\n");
CleanExit("");
return 0;
}

// Function Declaration *****************************************************

char *GetFileName(char *String) // ******************************************
{
char xS[255];
int xI;

// Get Actual Message Area
strcpy(MessageArea,String+strlen(NEWMSGAREA));
for (xI=0; xI < strlen(MessageArea); xI++)
 if (MessageArea[xI] == ENDMSGAREA) {
  MessageArea[xI]='\0'; break;
  }

// Get Shortened FileName
strcpy(xS,MessageArea);
SearchAndReplace(xS," ","");
SearchAndReplace(xS,"/","-");
SearchAndReplace(xS,"\\","-");
SearchAndReplace(xS,"(",".");
SearchAndReplace(xS,")","");
SearchAndReplace(xS,"Comp.Sys.Amiga.","CSA");
SearchAndReplace(xS,"Amiga","A-");
SearchAndReplace(xS,"General","Gen");
SearchAndReplace(xS,"GreatValleyProducts","GVP");
SearchAndReplace(xS,"International","Int");
SearchAndReplace(xS,"National","Nat");
SearchAndReplace(xS,"Programmer","Prog");
SearchAndReplace(xS,"Saskatchewan","Sask");
SearchAndReplace(xS,"Saskatoon","Stoon");
SearchAndReplace(xS,"S.A.U.G","SAUG");

return xS;
}

void master(FILE *File) // **************************************************
{ fprintf(File,"@master \"%s\"\n",FileArg); }

void toc(FILE *File) // *****************************************************
{
if (PathFlag) fprintf(File,"@toc \"%s%s/main\"\n",PathArg,DLGGUIDE);
else fprintf(File,"@toc \"%s/main\"\n",DLGGUIDE);
}

void ver(FILE *File) // *****************************************************
{ fprintf(File,"@%s\n",DONGROUT); }

void AddIndex(int Num,char *Subject,char *File) // **************************
{
unsigned char xS[255];

strcpy(xS,Subject+strlen(NEWSUBJECT));
SearchAndReplace(xS,"Re: ","");
SearchAndReplace(xS,"re: ","");
SearchAndReplace(xS,"RE: ","");
SearchAndReplace(xS,"Re:","");
SearchAndReplace(xS,"re:","");
SearchAndReplace(xS,"RE:","");
SearchAndReplace(xS,"  "," ");
if (xS[strlen(xS)-1] == '\n') xS[strlen(xS)-1]='\0';
fprintf(IndexFile,"Subject %3d. @{\"%s\" link \"%s/subject%d\"}\n"
                 ,Num,xS,File,Num);
}

void LoadMsgArea(char FileName[80]) // *****************************************
{
FILE *File;
char xS[80];

if (!(File=fopen(FileName,"rt"))) {
 sprintf(xS,"DLGGuide Error - MsgAreaFile=fopen(%s,read)",FileName);
 CleanExit(xS);
 }
MsgAreaSize=0;
while (fgets(xS,80,File))
 if (strlen(xS) && (xS[0]>='0' && xS[0]<='9')) {
  if (xS[strlen(xS)-1] == '\n') xS[strlen(xS)-1]='\0';
  MsgAreaSize++;
  MsgAreaList[MsgAreaSize].Num=atoi(xS);
  if (strstr(xS,"*\t"))
   MsgAreaList[MsgAreaSize].Skip=1; else MsgAreaList[MsgAreaSize].Skip=0;
  strcpy(MsgAreaList[MsgAreaSize].Data,strstr(xS,"\t")+1);
  }

fclose(File);
}

int GetMsgArea(char xS[80]) // **********************************************
{
int xI;

for (xI=1; xI<=MsgAreaSize; xI++)
 if (!stricmp(xS,MsgAreaList[xI].Data)) // VERSION 2.31
  return MsgAreaList[xI].Num;
return 0;
}

int IsSkip(int Num) // ******************************************************
{
int xI;

for (xI=1; xI<=MsgAreaSize; xI++)
 if (MsgAreaList[xI].Num == Num)
  return MsgAreaList[xI].Skip;
return 0;
}

void SkipParagraph(FILE *In,FILE *Out) // ***********************************
{
char xS[255];

// Blank Line
fgets(xS,255,In);
fputs(xS,Out);

// Paragraph
while (fgets(xS,255,In)) {
 if (strlen(xS) <= 1) {
  fputs(xS,Out); break;
  }
 if (!strnicmp(xS,"From: ",6) ||
     !strnicmp(xS,"Organization: ",14))
  fputs(xS,Out);
 }
}

int SearchAndReplace(char *String,char Search[255],char Replace[255]) // ****
{ // Search & Replace function for Strings of text
char Prefix[255]="", Suffix[255]="";

/* Replace string can't contain Search string
   Otherwise an infinite loop is created */
if (strstr(Replace,Search)) return FALSE;

while (strstr(String,Search)) {
 strcpy(Suffix,strstr(String,Search)+strlen(Search));
 strcpy(Prefix,String);
 Prefix[strlen(String)-strlen(Suffix)-strlen(Search)]='\0';
 sprintf(String,"%s%s%s",Prefix,Replace,Suffix);
 }
return TRUE;
}

char *WordMonth(int x) // Get A Number And Return The Name Of The Month ******
{static char *MonthNames[]={"","January","February","March","April","May",
"June","July","August","September","October","November","December"};
return ((x<1 || x>12) ? MonthNames[0] : MonthNames[x]);}

void CleanExit(char *ErrorMsg) // *******************************************
{
if (InputFile)  fclose(InputFile);
if (OutputFile) fclose(OutputFile);
if (IndexFile)  fclose(IndexFile);
if (GuideFile)  fclose(GuideFile);
#ifndef __MSDOS__
#endif
if (strcmp(ErrorMsg,"")) {
 fprintf(TxtOut,"%s\n",ErrorMsg);
 fflush(TxtOut);
#ifndef __MSDOS__
 if (strlen(WindowArg)) Delay(5*50);
#endif
 exit(10);
 }
else
 exit(0);
}
