//---------------------------------------------------------------------------

#pragma hdrstop

#include "Main.h"
//---------------------------------------------------------------------------

#pragma argsused
//---------------------------------------------------------------------------
void ReadTextFile(const char* File,vector<string>& Strvect, const bool StripQuotes)
{
  Strvect.clear();
  fstream f(File, ios::in);
  if(!f.is_open())
  {
    cout << "could not open file : " << File << endl;
    return;
  }
  f.seekg(0);
  try
  {
    while(!f.eof())
    {
      string s;
      getline(f, s);
      if(StripQuotes)
      {
        while(s.find("\""))  s.erase(s.find("\""));
      }
      if(s.size()) Strvect.push_back(s);
    }
  }
  __finally
  {
  f.close();
  }
}
//---------------------------------------------------------------------------
// write a strings array to a textfile
void WriteTextFile(const char* File, const vector<string> Strvect)
{
  fstream f(File, ios::out| ios::trunc);
  if(!f.is_open())
  {
    cout << "could not open file : " << File << endl;
    return;
  }
  f.seekg(0);
  try
  {
    for(unsigned i = 0; i < Strvect.size(); ++i)
      f << Strvect[i] << endl;
  }
  __finally
  {
    f.close();
  }
}
//---------------------------------------------------------------------------
int main(int argc, char* argv[])
{
   if(((argc != 4) && (argc != 5)) ||
      ((argc == 5) && strcmp(argv[1], "-n")))
  {
    cout << "usage: resmaker <inputfile.txt> <template.rc> <outputfile.rc>";
    return 1;
  }
  int ParamN = 0;
  bool ReplaceN = false;
  if(argc == 5)
  {
    ++ParamN;
    ReplaceN = true;
  }
  ReadTextFile(argv[ParamN + 1], StrIn, false);
  ReadTextFile(argv[ParamN + 2], StrTpl, false);
  if(StrIn.empty() || StrTpl.empty())
  {
    cout << "empty input from file";
    return 1;
  }
  for(unsigned Loop1 = 0; Loop1 < StrIn.size(); ++Loop1)
  {
    string StrLine = StrIn[Loop1];
    if((StrLine.find("//") == 0) || (StrLine.find("=") == 0))
      continue;
    string StrKey(StrLine,0, StrLine.find("="));
    string StrVal(StrLine, StrLine.find("=")+1, StrLine.size());

    if(ReplaceN) // replace /n with / and newline to make file better readable
    {
      char CRLF[2] = {0x5C, 0x0A};
      int Pos = StrVal.find("\\n\\n");
      while(Pos >= 0)
      {
        StrVal.insert(Pos +4, CRLF,2);
        Pos = StrVal.find("\\n\\n", Pos + 8);
      }
      Pos = StrVal.find("\\n-");
      while(Pos >= 0)
      {
        StrVal.insert(Pos +2, CRLF,2);
        Pos = StrVal.find("\\n-", Pos + 6);
      }
      Pos = StrVal.find(",\\n");
      while(Pos >= 0)
      {
        StrVal.insert(Pos +3, CRLF,2);
        Pos = StrVal.find(",\\n", Pos + 5);
      }
    }
    // substitute all ##key## values in
    for(unsigned Loop2 = 0; Loop2 < StrTpl.size(); ++Loop2)
    {
      int Pos;
      string Str2 = StrTpl[Loop2];
      do
      {
        Pos = Str2.find("##" + StrKey + "##");
        if(Pos >= 0)
        {
          Str2.replace(Pos, StrKey.size() + 4, StrVal);
          // special for NL because files in NL needs wider label
          if(StrVal == "&Bestanden:")
          {
            Pos = Str2.find(" 10, 32, 30");
            Str2.replace(Pos,11," 5, 32, 35");
          }
        }
      }while(Pos >= 0);
      StrTpl[Loop2] = Str2;
    }
  }
   // finally write the file to the output file
  WriteTextFile(argv[ParamN + 3], StrTpl);
  return 0;
}


