~ubuntu-branches/ubuntu/karmic/iterm/karmic

« back to all changes in this revision

Viewing changes to lib/src/state.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Fok
  • Date: 2004-02-27 04:13:16 UTC
  • Revision ID: james.westby@ubuntu.com-20040227041316-q0jn37sia8mt0t9u
Tags: upstream-0.5
ImportĀ upstreamĀ versionĀ 0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This software is subject to the terms of the Common Public License
 
2
   You must accept the terms of this license to use this software.
 
3
 
 
4
   Copyright (C) 2002, International Business Machines Corporation
 
5
   and others.  All Rights Reserved.
 
6
 
 
7
   Further information about Common Public License Version 0.5 is obtained
 
8
   from url http://oss.software.ibm.com/developer/opensource/license-cpl.html */
 
9
 
 
10
#include <stdio.h>
 
11
#include <stdlib.h>
 
12
#include "state.h"
 
13
 
 
14
/* storing digits */
 
15
void State_store_digit(void *object, Char *ch, int *params, int n_params)
 
16
{
 
17
  int tmp;
 
18
  State *state = (State *)object;
 
19
  tmp = params[state->n_params-1];
 
20
  if(tmp == DEFAULT_PARAM)
 
21
      tmp = 0;
 
22
  state->params[state->n_params-1] = 10 * tmp + (Char_get_first_byte(ch) - '0');
 
23
}
 
24
 
 
25
void State_next_param(void *object, Char *ch, int *params, int n_params)
 
26
{
 
27
  State *state = (State *)object;
 
28
  if(state->n_params < state->max_params )
 
29
      state->params[state->n_params++] = DEFAULT_PARAM;
 
30
}
 
31
 
 
32
/* State method impl */
 
33
void State_register_char(State *state, State *next_state,
 
34
                                int c, callback_func func, void *object)
 
35
{
 
36
  state->sequence[c].next_state = next_state;
 
37
  state->sequence[c].update = func;
 
38
  state->sequence[c].object = object;
 
39
}
 
40
 
 
41
/**< parse method
 
42
   * @param state reference to State object
 
43
   * @param ch reference to Char object to be parsed
 
44
   */
 
45
State *State_parse(State *state, Char *ch)
 
46
{
 
47
  State *next_state = state;
 
48
 
 
49
  if(Char_get_length(ch) == 1 &&
 
50
     (state->sequence[Char_get_first_byte(ch)].update != NULL ||
 
51
      state->sequence[Char_get_first_byte(ch)].next_state != NULL))
 
52
  {
 
53
    if (state->sequence[Char_get_first_byte(ch)].update != NULL)
 
54
        state->sequence[Char_get_first_byte(ch)].update(state->sequence[Char_get_first_byte(ch)].object,
 
55
                                          ch,state->params,state->n_params);
 
56
    if (state->sequence[Char_get_first_byte(ch)].next_state != NULL)
 
57
        next_state = state->sequence[Char_get_first_byte(ch)].next_state;
 
58
  }
 
59
  else if(Char_is_printable(ch) && state->when_printable.update)
 
60
  {
 
61
    state->when_printable.update(state->when_printable.object,ch,
 
62
                                 state->params,state->n_params);
 
63
    if(state->when_printable.next_state != NULL)
 
64
        next_state = state->when_printable.next_state;
 
65
  }
 
66
  
 
67
  return next_state;
 
68
}
 
69
 
 
70
static void State_get_in(State *state, Char *ch)
 
71
{
 
72
  state->n_params = 1;
 
73
  state->params[0] = DEFAULT_PARAM;
 
74
}
 
75
 
 
76
void State_init(State *state, int max_params)
 
77
{
 
78
  int i;
 
79
  state->get_in = State_get_in;
 
80
  state->when_printable.update = NULL;
 
81
  state->when_printable.next_state = state;
 
82
 
 
83
  for(i=0;i<N_TRANS;i++)
 
84
  {
 
85
    state->sequence[i].update = NULL;
 
86
    state->sequence[i].next_state = NULL;
 
87
  }
 
88
 
 
89
  state->max_params = max_params;
 
90
  if(0<max_params)
 
91
      state->params = (int *)calloc(sizeof(int),state->max_params);
 
92
  else
 
93
      state->params = NULL;
 
94
  state->n_params = 0;
 
95
#ifdef DEBUG
 
96
  memset(state->state_name,0,sizeof(state->state_name));
 
97
#endif  
 
98
}
 
99
/* State private method impl end*/
 
100
 
 
101
/* State Objects end*/