aboutsummaryrefslogtreecommitdiff
path: root/src/buffer.cpp
blob: d342c300ab1eb9a8788309ca6ddcabe6229e3ddf (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
    Copyright (C) 2014 BARRATERO Laurent                                   

    AeroUp 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.

    Aeroup 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, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
*/

/*
 *       Filename:  buffer.cpp
 *
 *    Description:  
 *
 *        Version:  0.3.0.1  
 *        Created:  28/12/2013 07:06:20
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  BARATTERO Laurent, laurentba<at>larueluberlu.net
 *   Organization:  La rue Luberlu 
 *
 */

#include <buffer.hpp>

using namespace std;

Buffer::Buffer():
    size(0)
{
   //memset(buff, 0, sizeof(buff));
}

int Buffer::addChr(unsigned char val)
{
  verify_size();
  //buff[size++] = val;
  //strcpy((char*)buff[size],(const char *)val);
  buff[size] = val;
  size++;
  return 1;
     
}

  string
Buffer::addDefsubToBuff()
{
  for(auto itr = sub_position.begin(); itr != sub_position.end(); ++itr)
  {
    while(((*itr).second.empty()) == false )
    {
      auto it = defsub_position.find((*itr).first);
      if(it != defsub_position.end())
      {
      buff[(*itr).second.back()] = defsub_position[(*itr).first][0];
      buff[(*itr).second.back() + 1] = defsub_position[(*itr).first][1];
      (*itr).second.pop_back();
      }
      else
      {
        return (*itr).first ;
      }
    }
  }
  return "";
}

  int 
Buffer::addDefsubPos(string defsubName)
{
  auto it = defsub_position.find(defsubName);
  if(it == defsub_position.end())
  {
    defsub_position[defsubName][0] = (uint8_t)(size & 0xff);
    defsub_position[defsubName][1] = (uint8_t)(size >> 8);
    return 0;
  }
  else
  {
    return 1;
  }
}

  void 
Buffer::addSubPos(string subName)
{
  sub_position[subName].push_back(size);
}

  void 
Buffer::printDefsubPos()
{
  cout << endl
       << "(Debug function) print map defsub_position :" << endl
       << "============================================" << endl;
  for(auto itr = defsub_position.begin(); itr != defsub_position.end(); ++itr)
  {
    cout << "Key : " << (*itr).first << endl
         << "Lsb : " << dec << (int)(*itr).second[0] << endl
         << "Msb : " << dec << (int)(*itr).second[1] << endl ;
  }
}

  void 
Buffer::printSubPos()
{
  cout << endl
       << "(Debug function) print map sub_position :" << endl
       << "=========================================" << endl;
  for(auto itr = sub_position.begin(); itr != sub_position.end(); ++itr)
  {
    cout << "Key : " << (*itr).first << endl
         << "Value : " ;
    for (auto itr_vect = (*itr).second.begin();
         itr_vect != (*itr).second.end();
         ++itr_vect)
    {
      cout << (*itr_vect) << " ";
    }

    cout << endl;
  }
}

  void
Buffer::printBuff(void) const
{
    for (unsigned int i = 0; i < size ; i++)
    {
        printf("buff[%d] : %d\n", i, buff[i]);
        
    }

}

  void 
Buffer::verify_size(void)
{
    if (size > BUFF_SIZE_MAX)
    {
        printf ("Error glow file too big !");
        exit (EXIT_FAILURE);
    }
}


/* 
 * where is sub map string vector int subnamekey, (byte,...)
 * defsub defsubname, localisation subfunction
 *
 * if sub define 2 times error
 *
 * */
  unsigned char*
Buffer::getSizeUpCmd()
{
  lsb_msb[0]= 100;
  lsb_msb[1]= (uint8_t)(size & 0xff);
  lsb_msb[2]= (uint8_t)(size >> 8);
  return lsb_msb;


}