/*
*  Naamari - an AES/SHA-based manual file crypto-synchronizer for cloud transfers
*  Copyright (c) 2015-2017 Sunkware
*  Key FP = 6B6D C8E9 3438 6E9C 3D97  56E5 2CE9 A476 99EF 28F6
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*  See the GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*  ----------------------------------------------------------------
*  WWW:       sunkware.org
*  E-mail:    sunkware@gmail.com
*  ----------------------------------------------------------------
*/

#include "commandlineparser.h"

namespace commander
{

namespace CommandLineParser
{


int            parse(QString commandLine, QStringList* pListTokens)
{
 int           iStart, iEnd;


 if (pListTokens == NULL)
     return COMERR_NULL_POINTER;


 pListTokens -> clear();

 commandLine = commandLine.trimmed();
 commandLine.append(" ");

 iStart = 0;
 iEnd = 0;

 while (iEnd >= 0)
  {
   if (commandLine.at(iStart) != '"')
    {
     iEnd = commandLine.indexOf(" ", iStart);

     if (iEnd > iStart)
      {
       (*pListTokens) << commandLine.mid(iStart, iEnd - iStart);
       iStart = iEnd;
      }
       else
        iStart++;

    }
     else
    {
     iEnd = commandLine.indexOf('"', iStart + 1);

     if (iEnd != (-1))
      {
       (*pListTokens) << commandLine.mid(iStart + 1, iEnd - iStart - 1);
       iStart = iEnd + 1;
      }

    }

  }

 return COMERR_OK;
}


int            separate(QStringList* pArgList, QStringList* pComList, QStringList* pOptList)
{
 if ( (pArgList == NULL) || (pComList == NULL) || (pOptList == NULL) )
  return COMERR_NULL_POINTER;


 pComList -> clear();
 pOptList -> clear();


 for (int i = 0; i < pArgList -> count(); i++)
  {
   QString str = pArgList -> at(i);

   if ( str.startsWith("--") )
   (*pOptList) << str;
     else
      (*pComList) << str;
  }


 return COMERR_OK;
}


} // namespace "CommandLineParser"

} // namespace "commander"
