File: commandset.C

package info (click to toggle)
speaker 1.0.1-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 264 kB
  • ctags: 174
  • sloc: cpp: 1,521; tcl: 786; makefile: 91; sh: 22
file content (108 lines) | stat: -rw-r--r-- 2,229 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//************************************************************
//   commandSet classe definition for the speakerphone app.
//************************************************************
#include "incall.h"

CommandSet::CommandSet()
{
   Name = NULL;
   Commands = NULL;
   next = NULL;
}

int CommandSet::Init(ConfigInfo* conf)
{
   FILE* fp;
   int ret = 1;
   fp = fopen(conf->CommandSet, "r");

   if (!fp) return 0;
   ret = ReadFile(fp);
   fclose(fp);
   return ret;
}

int CommandSet::ReadFile(FILE* fp)
{
   int   state = 0;
   int   num;
   char  buf[1024];
   char  buf1[1024];
   char  buf2[1024];
   CommandSequence*  CurrCmd = NULL;
   CommandSequence*  LastCmd = NULL;

   while (NULL != fgets(buf, sizeof(buf), fp))
   {
      if (buf[0] == '#' || buf[0] == '\n') continue;

      buf[1023] = 0;
      switch (state)
      {
         case 0:
            if (sscanf(buf, " COMMAND %s", buf1) == 1)
            {
               state = 1;
               Name = new char[strlen(buf1) + 1];
               strcpy(Name, buf1);
            }
         break;
         case 1:
            num = sscanf(buf, " %s %s ", buf1, buf2);
            if (num == 1 && 0 == strcmp(buf1, "END_COMMAND"))
            {
               state = 2;
            }
            if (num == 2)
            {
               CurrCmd = new CommandSequence(buf1, buf2);
               if (LastCmd == NULL)
               {
                  LastCmd = CurrCmd;
                  Commands = LastCmd;
               } else
               {
                  LastCmd->SetNext( CurrCmd );
                  LastCmd = CurrCmd;
               }
            }
         break;
      }
      if (state == 2) break;
   }

   if (state == 2) 
   {
      next = new CommandSet();
      if (!next->ReadFile(fp))
      {
         delete next;
         next = NULL;
      }
      return 1;
   }
   return 0;
};

CommandSet::~CommandSet()
{
   if (Name) delete Name;
   if (next) delete next;
   if (Commands) delete Commands;
};

CommandSequence* CommandSet::GetSequence(char* Nm)
{
   if (Name && (0 == strcmp(Name, Nm)))
   {
      return Commands;
   } else 
   {
      if (next)
      {
         return next->GetSequence(Nm);
      }
   }
   return NULL;
};