/* $Id: machinin.cpp 1.1 1997/10/02 21:51:34 leon Exp leon $
   Machining commands as defined by users for controling outputs of the
   controller.
 */

#include <assert.h>

#include <string.h>

#include <stdlib.h>

#include <stdio.h>

#include <strstream.h>


#include "profport.h"

#include "machinin.h"

#include "listiter.h"


extern char IniFile[100];   // .INI Filename




#define MACHINING_SECTION "Machining"


/** Inserts command in the list of the commands **/
void Machining::InsertCommand(char type, int number, const char *commandString)
{
  Command *cmd = new Command;
  cmd->type = type;
  cmd->number = number;
  cmd->commandString = strdup(commandString);
  assert(cmd->commandString != NULL);
  commands.append(*cmd);
}

/** Write list to the INI file **/
Machining::WriteINI(char *filename)
{
   ostrstream active;

   ListIterators<Command> iterator(commands);

   for (; !iterator.isDone(); iterator.next())
    {
      char item[6];
      sprintf(item, "%c%02d", iterator.currentItem()->type,
              iterator.currentItem()->number);
      write_private_profile_string (MACHINING_SECTION, item,
                     iterator.currentItem()->commandString, filename);
      active << item << " ";
    }
   active << ends;
   write_private_profile_string (MACHINING_SECTION, "Active", 
                                 active.str(), filename);
   return 0;
}



/** Read list from the INI file **/
Machining::ReadINI(char *filename)
{
   char buffer[MAX_COMMAND_STRING_LEN];
   char buff[MAX_COMMAND_STRING_LEN];

   get_private_profile_string (MACHINING_SECTION, "Active", "",
        buffer, MAX_COMMAND_STRING_LEN, filename);
   char *item, *p = buffer;
   while ((item = strtok(p, " ,\t")) != NULL)
     {
        char type;
        int number;
        p = NULL;
        if (sscanf(item, "%c%d", &type, &number) == 2)
          {
            get_private_profile_string (MACHINING_SECTION, item,
                  "", buff, MAX_COMMAND_STRING_LEN, filename);
            InsertCommand(type, number, buff);
          }
     }
   return 0;
}

/** Returns pointer to the command string or NULL if not found */
char *Machining::GetCommand(char type, int number)
{
   ListIterators<Command> iterator(commands);

   for (; !iterator.isDone(); iterator.next())
    {
        
      if (iterator.currentItem()->type == type 
          && iterator.currentItem()->number == number)
        return iterator.currentItem()->commandString;

    } 
   return NULL;
}



syntax highlighted by Code2HTML, v. 0.9.1