ffead.server.doc
AMEFObject.h
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 #ifndef AMEFOBJECT_H_
18 #define AMEFOBJECT_H_
19 
20 #include "string"
21 #include "cstring"
22 #include "vector"
23 #include "CastUtil.h"
24 #include "Constants.h"
25 using namespace std;
26 
28 {
29  /*The type of the Object can be string, number, date, boolean, character or any complex object*/
30  char type;
31 
32  /*The name of the Object if required can be used to represent object properties*/
33  string name;
34 
35  /*The Length of the Object value*/
36  int length;
37 
38  /*The Length of the Object value*/
39  int namedLength;
40 
41  /*The Object value in string format*/
42  string value;
43 
44  /*The properties of a complex object*/
45  vector<AMEFObject*> packets;
46 
47  static string longTocharArray(long l,int ind)
48  {
49  string result;
50  for (int i = 0; i<ind; i++)
51  {
52  int offset = (ind - 1 - i) * 8;
53  result.push_back((char) ((l >> offset) & 0xFF));
54  }
55  return result;
56  }
57 
58  static string intTocharArray(int l,int ind)
59  {
60  string result;
61  for (int i = 0; i<ind; i++)
62  {
63  int offset = (ind - 1 - i) * 8;
64  result.push_back((char) ((l >> offset) & 0xFF));
65  }
66  return result;
67  }
68 
69  static char* intTocharArrayWI(int l)
70  {
71  int ind = 1;
72  if(l<256)
73  ind =1;
74  else if(l<65536)
75  ind = 2;
76  else if(l<16777216)
77  ind =3;
78  else
79  ind =4;
80  char* result = new char[ind];
81  for (int i = 0; i<ind; i++)
82  {
83  int offset = (ind - 1 - i) * 8;
84  result[i] = (char) ((l >> offset) & 0xFF);
85  }
86  return result;
87  }
88 
89  static int charArrayToInt(string l)
90  {
91  int t = 0;
92  int ind = l.length();
93  for (int i = 0; i < ind; i++)
94  {
95  int offset = (ind -1 - i) * 8;
96  t += (l[i] & 0x000000FF) << offset;
97  }
98  return t;
99  }
100 
101  static int charArrayToInt(string l,int off,int ind)
102  {
103  int t = 0;
104  for (int i = 0; i < ind; i++)
105  {
106  int offset = (ind -1 - i) * 8;
107  t += (l[off+i] & 0x000000FF) << offset;
108  }
109  return t;
110  }
111 
112  static long charArrayToLong(string l)
113  {
114  long t = 0;
115  int ind = l.length();
116  for (int i = 0; i < ind; i++)
117  {
118  int offset = (ind -1 - i) * 8;
119  t += (l[i] & 0x000000FF) << offset;
120  }
121  return t;
122  }
123  static long charArrayToLong(string l,int off,int ind)
124  {
125  long t = 0;
126  for (int i = 0; i < ind; i++)
127  {
128  int offset = (ind -1 - i) * 8;
129  t += (l[off+i] & 0x000000FF) << offset;
130  }
131  return t;
132  }
133  static long charArrayToLong(string l,int ind)
134  {
135  long t = 0;
136  for (int i = 0; i < ind; i++)
137  {
138  int offset = (ind -1 - i) * 8;
139  t += (l[i] & 0x000000FF) << offset;
140  }
141  return t;
142  }
143 
144  static string intTocharArrayS(int l, int ind)
145  {
146  char* result = new char[ind];
147  for (int i = 0; i<ind; i++)
148  {
149  int offset = (ind - 1 - i) * 8;
150  result[i] = (char) ((l >> offset) & 0xFF);
151  }
152  string tem(result);
153  return tem;
154  }
155 
156 public:
157  static const char NULL_STRING = 'a';
158 
159  static const char NULL_NUMBER = 'g';
160 
161  static const char NULL_DATE = 'j';
162 
163  static const char NULL_FPN = 'k';
164 
165  static const char NULL_BOOL = 'v';
166 
167  static const char NULL_CHAR = 'z';
168 
169  /*The Date type*/
170  static const char DATE_TYPE = 'd';
171 
172  /*The 4GB string type*/
173  static const char STRING_TYPE = 's';
174 
175  /*The max 256 length string type*/
176  static const char STRING_256_TYPE = 't';
177 
178  /*The max 65536 length string type*/
179  static const char STRING_65536_TYPE = 'h';
180 
181  static const char STRING_16777216_TYPE = 'y';
182 
183  /*The bool type*/
184  static const char BOOLEAN_TYPE = 'b';
185 
186  /*The character type*/
187  static const char CHAR_TYPE = 'c';
188 
189  /*The Number types*/
190  static const char VERY_SMALL_INT_TYPE = 'n';
191 
192  static const char SMALL_INT_TYPE = 'w';
193 
194  static const char BIG_INT_TYPE = 'r';
195 
196  static const char INT_TYPE = 'i';
197 
198  static const char VS_LONG_INT_TYPE = 'f';
199 
200  static const char S_LONG_INT_TYPE = 'x';
201 
202  static const char B_LONG_INT_TYPE = 'e';
203 
204  static const char LONG_INT_TYPE = 'l';
205 
206  static const char DOUBLE_FLOAT_TYPE = 'u';
207 
208  /*The Object type*/
209  static const char VS_OBJECT_TYPE = 'm';
210 
211  /*The Object type*/
212  static const char S_OBJECT_TYPE = 'q';
213 
214  /*The Object type*/
215  static const char B_OBJECT_TYPE = 'p';
216 
217  /*The Object type*/
218  static const char OBJECT_TYPE = 'o';
219 
220  bool isNull()
221  {
222  if(type=='a' || type=='g' || type=='j' || type=='k' || type=='v' || type=='z')
223  return true;
224  else
225  return false;
226  }
227 
233  static char getEqvNullType(char type)
234  {
235  if(type=='s' || type=='t' || type=='h' || type=='y')
236  return NULL_STRING;
237  else if(type=='n' || type=='w' || type=='r' || type=='q'
238  || type=='f' || type=='x' || type=='e' || type=='l')
239  return NULL_NUMBER;
240  else if(type=='d')
241  return NULL_DATE;
242  else if(type=='b')
243  return NULL_BOOL;
244  else if(type=='c')
245  return NULL_CHAR;
246  else
247  return 0;
248  }
249 
250 
251  void clear()
252  {
253  this->packets.clear();
254  }
255 
256  /*Create a new AMEF object which will initilaize the values*/
257  AMEFObject()
258  {
259  type = OBJECT_TYPE;
260  length = 0;
261  name = "";
262  namedLength = 0;
263  }
264 
265  ~AMEFObject();
266 
267  void addNullPacket(char type)
268  {
269  AMEFObject* JDBObjectNew = new AMEFObject();
270  JDBObjectNew->namedLength += 1;
271  length += 1;
272  namedLength += 3;
273  JDBObjectNew->type = type;
274  packets.push_back(JDBObjectNew);
275  }
276 
277  void addNullPacket(char type,string name)
278  {
279  AMEFObject* JDBObjectNew = new AMEFObject();
280  JDBObjectNew->name = name;
281  namedLength += name.length();
282  JDBObjectNew->namedLength += name.length() + 1;
283  length += 1;
284  namedLength += 3;
285  JDBObjectNew->type = type;
286  packets.push_back(JDBObjectNew);
287  }
288 
294  void addPacket(const string& stringa,const string& name)
295  {
296  AMEFObject* JDBObjectNew = addPacket(stringa);
297  JDBObjectNew->name = name;
298  //length += name.length();
299  namedLength += name.length();
300  JDBObjectNew->namedLength += name.length();
301  }
306  AMEFObject* addPacket(const string& stringa)
307  {
308  AMEFObject* JDBObjectNew = new AMEFObject();
309  JDBObjectNew->name = "";
310 
311  if(stringa.length()<=256)
312  {
313  JDBObjectNew->type = STRING_256_TYPE;
314  length += stringa.length() + 2;
315  namedLength += stringa.length() + 4;
316  JDBObjectNew->namedLength = stringa.length() + 2;
317  }
318  else if(stringa.length()<=65536)
319  {
320  JDBObjectNew->type = STRING_65536_TYPE;
321  length += stringa.length() + 3;
322  namedLength += stringa.length() + 5;
323  JDBObjectNew->namedLength = stringa.length() + 3;
324  }
325  else if(stringa.length()<=16777216)
326  {
327  JDBObjectNew->type = STRING_16777216_TYPE;
328  length += stringa.length() + 4;
329  namedLength += stringa.length() + 6;
330  JDBObjectNew->namedLength = stringa.length() + 4;
331  }
332  else
333  {
334  JDBObjectNew->type = STRING_TYPE;
335  length += stringa.length() + 5;
336  namedLength += stringa.length() + 7;
337  JDBObjectNew->namedLength = stringa.length() + 5;
338  }
339  JDBObjectNew->length = stringa.length();
340  JDBObjectNew->value = stringa;
341  packets.push_back(JDBObjectNew);
342  return JDBObjectNew;
343  }
344 
350  void addPacket(char stringa[],string name)
351  {
352  AMEFObject* JDBObjectNew = addPacket(stringa);
353  JDBObjectNew->name = name;
354  //length += name.length();
355  namedLength += name.length();
356  JDBObjectNew->namedLength += name.length();
357  }
362  AMEFObject* addPacket(char stringa[])
363  {
364  AMEFObject* JDBObjectNew = new AMEFObject();
365  JDBObjectNew->name = "";
366 
367  if(strlen(stringa)<=256)
368  {
369  JDBObjectNew->type = STRING_256_TYPE;
370  length += strlen(stringa) + 2;
371  namedLength += strlen(stringa) + 4;
372  JDBObjectNew->namedLength = strlen(stringa) + 2;
373  }
374  else if(strlen(stringa)<=65536)
375  {
376  JDBObjectNew->type = STRING_65536_TYPE;
377  length += strlen(stringa) + 3;
378  namedLength += strlen(stringa) + 5;
379  JDBObjectNew->namedLength = strlen(stringa) + 3;
380  }
381  else if(strlen(stringa)<=16777216)
382  {
383  JDBObjectNew->type = STRING_16777216_TYPE;
384  length += strlen(stringa) + 4;
385  namedLength += strlen(stringa) + 6;
386  JDBObjectNew->namedLength = strlen(stringa) + 4;
387  }
388  else
389  {
390  JDBObjectNew->type = STRING_TYPE;
391  length += strlen(stringa) + 5;
392  namedLength += strlen(stringa) + 7;
393  JDBObjectNew->namedLength = strlen(stringa) + 5;
394  }
395  JDBObjectNew->length = strlen(stringa);
396  JDBObjectNew->value = stringa;
397  packets.push_back(JDBObjectNew);
398  return JDBObjectNew;
399  }
400 
406  void addPacket(bool boole,string name)
407  {
408  AMEFObject* JDBObjectNew = addPacket(boole);
409  JDBObjectNew->name = name;
410  //length += name.length();
411  namedLength += name.length();
412  JDBObjectNew->namedLength += name.length();
413  }
418  AMEFObject* addPacket(bool boole)
419  {
420  AMEFObject* JDBObjectNew = new AMEFObject();
421  JDBObjectNew->type = BOOLEAN_TYPE;
422  JDBObjectNew->name = "";
423  JDBObjectNew->length = 1;
424  if(boole==true)
425  {
426  JDBObjectNew->value = "1";
427  }
428  else
429  {
430  JDBObjectNew->value = "0";
431  }
432  packets.push_back(JDBObjectNew);
433  length += 2;
434  namedLength += 4;
435  JDBObjectNew->namedLength = 2;
436  return JDBObjectNew;
437  }
438 
439  void addPacket(char chr,string name)
440  {
441  AMEFObject* JDBObjectNew = addPacket(chr);
442  JDBObjectNew->name = name;
443  //length += name.length();
444  namedLength += name.length();
445  JDBObjectNew->namedLength += name.length();
446  }
451  AMEFObject* addPacket(char chr)
452  {
453  AMEFObject* JDBObjectNew = new AMEFObject();
454  JDBObjectNew->type = 'c';
455  JDBObjectNew->name = "";
456  JDBObjectNew->length = 1;
457  JDBObjectNew->value.push_back(chr);
458  packets.push_back(JDBObjectNew);
459  length += 2;
460  namedLength += 4;
461  JDBObjectNew->namedLength = 2;
462  return JDBObjectNew;
463  }
464 
465  AMEFObject* addPacket(char value, char type)
466  {
467  AMEFObject* JDBObjectNew = new AMEFObject();
468  JDBObjectNew->type = type;
469  JDBObjectNew->name = "";
470  JDBObjectNew->length = 1;
471  JDBObjectNew->value.push_back(value);
472  packets.push_back(JDBObjectNew);
473  length += 2;
474  namedLength += 4;
475  JDBObjectNew->namedLength = 2;
476  return JDBObjectNew;
477  }
478 
484  void addPacket(long lon,string name)
485  {
486  AMEFObject* JDBObjectNew = addPacket(lon);
487  JDBObjectNew->name = name;
488  //length += name.length();
489  namedLength += name.length();
490  JDBObjectNew->namedLength += name.length();
491  }
496  #ifdef IS_64_BIT
497  AMEFObject* addPacket(unsigned long long lon)
498  {
499  AMEFObject* JDBObjectNew = new AMEFObject();
500  if(lon<256)
501  {
502  JDBObjectNew->type = VERY_SMALL_INT_TYPE;
503  JDBObjectNew->value = longTocharArray(lon, 1);
504  length += 2;
505  namedLength += 4;
506  JDBObjectNew->namedLength = 2;
507  JDBObjectNew->length = 1;
508  }
509  else if(lon<65536)
510  {
511  JDBObjectNew->type = SMALL_INT_TYPE;
512  JDBObjectNew->value = longTocharArray(lon, 2);
513  length += 3;
514  namedLength += 5;
515  JDBObjectNew->namedLength = 3;
516  JDBObjectNew->length = 2;
517  }
518  else if(lon<16777216)
519  {
520  JDBObjectNew->type = BIG_INT_TYPE;
521  JDBObjectNew->value = longTocharArray(lon, 3);
522  length += 4;
523  namedLength += 6;
524  JDBObjectNew->namedLength = 4;
525  JDBObjectNew->length = 3;
526  }
527  else if(lon<4294967296ULL)
528  {
529  JDBObjectNew->type = INT_TYPE;
530  JDBObjectNew->value = longTocharArray(lon, 4);
531  length += 5;
532  namedLength += 7;
533  JDBObjectNew->namedLength = 5;
534  JDBObjectNew->length = 4;
535  }
536  else if(lon<1099511627776ULL)
537  {
538  JDBObjectNew->type = VS_LONG_INT_TYPE;
539  JDBObjectNew->value = longTocharArray(lon, 5);
540  length += 6;
541  namedLength += 8;
542  JDBObjectNew->namedLength = 6;
543  JDBObjectNew->length = 5;
544  }
545  else if(lon<281474976710656ULL)
546  {
547  JDBObjectNew->type = S_LONG_INT_TYPE;
548  JDBObjectNew->value = longTocharArray(lon, 6);
549  length += 7;
550  namedLength += 9;
551  JDBObjectNew->namedLength = 7;
552  JDBObjectNew->length = 6;
553  }
554  else if(lon<72057594037927936ULL)
555  {
556  JDBObjectNew->type = B_LONG_INT_TYPE;
557  JDBObjectNew->value = longTocharArray(lon, 7);
558  length += 8;
559  namedLength += 10;
560  JDBObjectNew->namedLength = 8;
561  JDBObjectNew->length = 7;
562  }
563  else
564  {
565  JDBObjectNew->type = LONG_INT_TYPE;
566  JDBObjectNew->value = longTocharArray(lon, 8);
567  length += 9;
568  namedLength += 11;
569  JDBObjectNew->namedLength = 9;
570  JDBObjectNew->length = 8;
571  }
572  JDBObjectNew->name = "";
573  packets.push_back(JDBObjectNew);
574  return JDBObjectNew;
575  }
576  AMEFObject* addPacket(long lon)
577  {
578  AMEFObject* JDBObjectNew = new AMEFObject();
579  if(lon<256)
580  {
581  JDBObjectNew->type = VERY_SMALL_INT_TYPE;
582  JDBObjectNew->value = longTocharArray(lon, 1);
583  length += 2;
584  namedLength += 4;
585  JDBObjectNew->namedLength = 2;
586  JDBObjectNew->length = 1;
587  }
588  else if(lon<65536)
589  {
590  JDBObjectNew->type = SMALL_INT_TYPE;
591  JDBObjectNew->value = longTocharArray(lon, 2);
592  length += 3;
593  namedLength += 5;
594  JDBObjectNew->namedLength = 3;
595  JDBObjectNew->length = 2;
596  }
597  else if(lon<16777216)
598  {
599  JDBObjectNew->type = BIG_INT_TYPE;
600  JDBObjectNew->value = longTocharArray(lon, 3);
601  length += 4;
602  namedLength += 6;
603  JDBObjectNew->namedLength = 4;
604  JDBObjectNew->length = 3;
605  }
606  else if(lon<(long)4294967296ULL)
607  {
608  JDBObjectNew->type = INT_TYPE;
609  JDBObjectNew->value = longTocharArray(lon, 4);
610  length += 5;
611  namedLength += 7;
612  JDBObjectNew->namedLength = 5;
613  JDBObjectNew->length = 4;
614  }
615  else if(lon<(long)1099511627776ULL)
616  {
617  JDBObjectNew->type = VS_LONG_INT_TYPE;
618  JDBObjectNew->value = longTocharArray(lon, 5);
619  length += 6;
620  namedLength += 8;
621  JDBObjectNew->namedLength = 6;
622  JDBObjectNew->length = 5;
623  }
624  else if(lon<(long)281474976710656ULL)
625  {
626  JDBObjectNew->type = S_LONG_INT_TYPE;
627  JDBObjectNew->value = longTocharArray(lon, 6);
628  length += 7;
629  namedLength += 9;
630  JDBObjectNew->namedLength = 7;
631  JDBObjectNew->length = 6;
632  }
633  else if(lon<(long)72057594037927936ULL)
634  {
635  JDBObjectNew->type = B_LONG_INT_TYPE;
636  JDBObjectNew->value = longTocharArray(lon, 7);
637  length += 8;
638  namedLength += 10;
639  JDBObjectNew->namedLength = 8;
640  JDBObjectNew->length = 7;
641  }
642  else
643  {
644  JDBObjectNew->type = LONG_INT_TYPE;
645  JDBObjectNew->value = longTocharArray(lon, 8);
646  length += 9;
647  namedLength += 11;
648  JDBObjectNew->namedLength = 9;
649  JDBObjectNew->length = 8;
650  }
651  JDBObjectNew->name = "";
652  packets.push_back(JDBObjectNew);
653  return JDBObjectNew;
654  }
655  #else
656  AMEFObject* addPacket(unsigned long long lon)
657  {
658  AMEFObject* JDBObjectNew = new AMEFObject();
659  if(lon<256)
660  {
661  JDBObjectNew->type = VERY_SMALL_INT_TYPE;
662  JDBObjectNew->value = longTocharArray(lon, 1);
663  length += 2;
664  namedLength += 4;
665  JDBObjectNew->namedLength = 2;
666  JDBObjectNew->length = 1;
667  }
668  else if(lon<65536)
669  {
670  JDBObjectNew->type = SMALL_INT_TYPE;
671  JDBObjectNew->value = longTocharArray(lon, 2);
672  length += 3;
673  namedLength += 5;
674  JDBObjectNew->namedLength = 3;
675  JDBObjectNew->length = 2;
676  }
677  else if(lon<16777216)
678  {
679  JDBObjectNew->type = BIG_INT_TYPE;
680  JDBObjectNew->value = longTocharArray(lon, 3);
681  length += 4;
682  namedLength += 6;
683  JDBObjectNew->namedLength = 4;
684  JDBObjectNew->length = 3;
685  }
686  else if(lon<4294967296ULL)
687  {
688  JDBObjectNew->type = INT_TYPE;
689  JDBObjectNew->value = longTocharArray(lon, 4);
690  length += 5;
691  namedLength += 7;
692  JDBObjectNew->namedLength = 5;
693  JDBObjectNew->length = 4;
694  }
695  else if(lon<1099511627776ULL)
696  {
697  JDBObjectNew->type = VS_LONG_INT_TYPE;
698  JDBObjectNew->value = longTocharArray(lon, 5);
699  length += 6;
700  namedLength += 8;
701  JDBObjectNew->namedLength = 6;
702  JDBObjectNew->length = 5;
703  }
704  else if(lon<281474976710656ULL)
705  {
706  JDBObjectNew->type = S_LONG_INT_TYPE;
707  JDBObjectNew->value = longTocharArray(lon, 6);
708  length += 7;
709  namedLength += 9;
710  JDBObjectNew->namedLength = 7;
711  JDBObjectNew->length = 6;
712  }
713  else if(lon<72057594037927936ULL)
714  {
715  JDBObjectNew->type = B_LONG_INT_TYPE;
716  JDBObjectNew->value = longTocharArray(lon, 7);
717  length += 8;
718  namedLength += 10;
719  JDBObjectNew->namedLength = 8;
720  JDBObjectNew->length = 7;
721  }
722  else
723  {
724  JDBObjectNew->type = LONG_INT_TYPE;
725  JDBObjectNew->value = longTocharArray(lon, 8);
726  length += 9;
727  namedLength += 11;
728  JDBObjectNew->namedLength = 9;
729  JDBObjectNew->length = 8;
730  }
731  JDBObjectNew->name = "";
732  packets.push_back(JDBObjectNew);
733  return JDBObjectNew;
734  }
735  AMEFObject* addPacket(long lon)
736  {
737  AMEFObject* JDBObjectNew = new AMEFObject();
738  if(lon<256)
739  {
740  JDBObjectNew->type = VERY_SMALL_INT_TYPE;
741  JDBObjectNew->value = longTocharArray(lon, 1);
742  length += 2;
743  namedLength += 4;
744  JDBObjectNew->namedLength = 2;
745  JDBObjectNew->length = 1;
746  }
747  else if(lon<65536)
748  {
749  JDBObjectNew->type = SMALL_INT_TYPE;
750  JDBObjectNew->value = longTocharArray(lon, 2);
751  length += 3;
752  namedLength += 5;
753  JDBObjectNew->namedLength = 3;
754  JDBObjectNew->length = 2;
755  }
756  else if(lon<16777216)
757  {
758  JDBObjectNew->type = BIG_INT_TYPE;
759  JDBObjectNew->value = longTocharArray(lon, 3);
760  length += 4;
761  namedLength += 6;
762  JDBObjectNew->namedLength = 4;
763  JDBObjectNew->length = 3;
764  }
765  else
766  {
767  JDBObjectNew->type = INT_TYPE;
768  JDBObjectNew->value = longTocharArray(lon, 4);
769  length += 5;
770  namedLength += 7;
771  JDBObjectNew->namedLength = 5;
772  JDBObjectNew->length = 4;
773  }
774  JDBObjectNew->name = "";
775  packets.push_back(JDBObjectNew);
776  return JDBObjectNew;
777  }
778  #endif
779 
784  void addPacket(float doub,string name)
785  {
786  AMEFObject* JDBObjectNew = addPacket(doub);
787  JDBObjectNew->name = name;
788  //length += name.length();
789  namedLength += name.length();
790  JDBObjectNew->namedLength += name.length();
791  }
796  AMEFObject* addPacket(float doub)
797  {
798  AMEFObject* JDBObjectNew = new AMEFObject();
799  JDBObjectNew->type = DOUBLE_FLOAT_TYPE;
800  JDBObjectNew->name = "";
801  JDBObjectNew->value = CastUtil::lexical_cast<string>(doub);
802  JDBObjectNew->length = JDBObjectNew->value.length();
803  length += JDBObjectNew->value.length() + 2;
804  namedLength += JDBObjectNew->value.length() + 4;
805  JDBObjectNew->namedLength = JDBObjectNew->value.length() + 2;
806  packets.push_back(JDBObjectNew);
807  return JDBObjectNew;
808  }
809 
810 
816  void addPacket(double doub,string name)
817  {
818  AMEFObject* JDBObjectNew = addPacket(doub);
819  JDBObjectNew->name = name;
820  //length += name.length();
821  namedLength += name.length();
822  JDBObjectNew->namedLength += name.length();
823  }
828  AMEFObject* addPacket(double doub)
829  {
830  AMEFObject* JDBObjectNew = new AMEFObject();
831  JDBObjectNew->type = DOUBLE_FLOAT_TYPE;
832  JDBObjectNew->name = "";
833  JDBObjectNew->value = CastUtil::lexical_cast<string>(doub);
834  JDBObjectNew->length = JDBObjectNew->value.length();
835  length += JDBObjectNew->value.length() + 2;
836  namedLength += JDBObjectNew->value.length() + 4;
837  JDBObjectNew->namedLength = JDBObjectNew->value.length() + 2;
838  packets.push_back(JDBObjectNew);
839  return JDBObjectNew;
840  }
841 
847  void addPacket(int integer,string name)
848  {
849  AMEFObject* JDBObjectNew = addPacket(integer);
850  JDBObjectNew->name = name;
851  //length += name.length();
852  namedLength += name.length();
853  JDBObjectNew->namedLength += name.length();
854  }
859  AMEFObject* addPacket(int integer)
860  {
861  AMEFObject* JDBObjectNew = new AMEFObject();
862  if(integer<256)
863  {
864  JDBObjectNew->type = VERY_SMALL_INT_TYPE;
865  JDBObjectNew->value = intTocharArray(integer, 1);
866  length += 2;
867  namedLength += 4;
868  JDBObjectNew->namedLength = 2;
869  JDBObjectNew->length = 1;
870  }
871  else if(integer<65536)
872  {
873  JDBObjectNew->type = SMALL_INT_TYPE;
874  JDBObjectNew->value = intTocharArray(integer, 2);
875  length += 3;
876  namedLength += 5;
877  JDBObjectNew->namedLength = 3;
878  JDBObjectNew->length = 2;
879  }
880  else if(integer<16777216)
881  {
882  JDBObjectNew->type = BIG_INT_TYPE;
883  JDBObjectNew->value = intTocharArray(integer, 3);
884  length += 4;
885  namedLength += 6;
886  JDBObjectNew->namedLength = 4;
887  JDBObjectNew->length = 3;
888  }
889  else
890  {
891  JDBObjectNew->type = INT_TYPE;
892  JDBObjectNew->value = intTocharArray(integer, 4);
893  length += 5;
894  namedLength += 7;
895  JDBObjectNew->namedLength = 5;
896  JDBObjectNew->length = 4;
897  }
898  JDBObjectNew->name = "";
899  packets.push_back(JDBObjectNew);
900  return JDBObjectNew;
901  }
902 
908  /*void addPacket(Date date,string name)
909  {
910  AMEFObject* JDBObjectNew = addPacket(date);
911  JDBObjectNew->name = name;
912  namedLength += name.length();
913  JDBObjectNew->namedLength += name.length();
914  }*/
919  /*AMEFObject* addPacket(Date date)
920  {
921  AMEFObject* JDBObjectNew = new AMEFObject();
922  JDBObjectNew->type = DATE_TYPE;
923  JDBObjectNew->name = "";
924  SimpleDateFormat format = new SimpleDateFormat("ddMMyyyy HHmmss");
925  JDBObjectNew->length = 15;
926  JDBObjectNew->value = format.format(date).getchars();
927  JDBObjectNew->namedLength += JDBObjectNew->value.length;
928  length += JDBObjectNew->value.length + 2;
929  namedLength += JDBObjectNew->value.length + 4;
930  JDBObjectNew->namedLength = JDBObjectNew->value.length + 2;
931  JDBObjectNew->length = JDBObjectNew->value.length;
932  packets.push_back(JDBObjectNew);
933  return JDBObjectNew;
934  }*/
935 
940  void addPacket(AMEFObject *packet)
941  {
942  packets.push_back(packet);
943  if(packet->type=='o')
944  {
945  if(packet->length+1<256)
946  packet->type = 'm';
947  else if(packet->length+1<65536)
948  packet->type = 'q';
949  else if(packet->length+1<16777216)
950  packet->type = 'p';
951  else
952  packet->type = 'o';
953  }
954  length += packet->getLength();
955  namedLength += packet->getNamedLength(false);
956  }
957 
958  /*void set(int i,AMEFObject jdbo)
959  {
960  packets.set(i,jdbo);
961  }*/
962 
967  void addPacket(char packet[],char type)
968  {
969  if(type=='s' || type=='d' || type=='t' || type=='h' || type=='y')
970  {
971  addPacket(packet);
972  }
973  else if(type=='n' || type=='w' || type=='r' || type=='i')
974  {
975  addPacket(charArrayToInt(packet));
976  }
977  else if(type=='f' || type=='x' || type=='e' || type=='l')
978  {
979  addPacket(charArrayToLong(packet));
980  }
981  else if(type=='u')
982  {
983  //addPacket(Double.parseDouble(new string(packet)));
984  }
985  else if(type=='b')
986  {
987  addPacket(packet[0]=='1'?true:false);
988  }
989  else if(type=='c')
990  {
991  addPacket((char)packet[0]);
992  }
993  else if(type=='a' || type=='g' || type=='j' || type=='k' || type=='v' || type=='z')
994  {
995  addNullPacket(type);
996  }
997  }
998 
999  /*void addPacket(Object obj)
1000  {
1001  if(obj instanceof Long)
1002  addPacket(((Long)obj).longValue());
1003  else if(obj instanceof Double)
1004  addPacket(((Double)obj).doubleValue());
1005  else if(obj instanceof string)
1006  addPacket((string)obj);
1007  else if(obj instanceof char)
1008  addPacket(((char)obj).charValue());
1009  }*/
1010 
1011  int getlength()
1012  {
1013  return length ;
1014  }
1015 
1016  int getLength()
1017  {
1018  if(type=='m')
1019  {
1020  return 2 + length;
1021  }
1022  else if(type=='q')
1023  {
1024  return 3 + length;
1025  }
1026  else if(type=='p')
1027  {
1028  return 4 + length;
1029  }
1030  else if(type=='o')
1031  {
1032  return 5 + length;
1033  }
1034  else
1035  return length;
1036  }
1037 
1038 
1039  static bool isstring(char type)
1040  {
1041  if(type=='s' || type=='t' || type=='h' || type=='y' || type=='a')
1042  return true;
1043  return false;
1044  }
1045 
1046  static bool isFloatingPoint(char type)
1047  {
1048  if(type=='u' || type=='k')
1049  return true;
1050  return false;
1051  }
1052 
1053  static bool isNumber(char type)
1054  {
1055  if(type=='n' || type=='w' || type=='r' || type=='i'
1056  || type=='f' || type=='x' || type=='e' || type=='l' || type=='g')
1057  return true;
1058  return false;
1059  }
1060 
1061  static bool isInteger(char type)
1062  {
1063  if(type=='n' || type=='w' || type=='r' || type=='i')
1064  return true;
1065  return false;
1066  }
1067 
1068  static bool isLong(char type)
1069  {
1070  if(type=='n' || type=='w' || type=='r' || type=='i'
1071  || type=='f' || type=='x' || type=='e' || type=='l')
1072  return true;
1073  return false;
1074  }
1075 
1076  static bool isChar(char type)
1077  {
1078  if(type=='c' || type=='z')
1079  return true;
1080  return false;
1081  }
1082 
1083  static bool isBoolean(char type)
1084  {
1085  if(type=='b' || type=='v')
1086  return true;
1087  return false;
1088  }
1089 
1090  static bool isDate(char type)
1091  {
1092  if(type=='d' || type=='j')
1093  return true;
1094  return false;
1095  }
1096 
1097  bool isStringOrNullString()
1098  {
1099  if(type=='s' || type=='t' || type=='h' || type=='y' || type=='a')
1100  return true;
1101  return false;
1102  }
1103 
1104  bool isString()
1105  {
1106  if(type=='s' || type=='t' || type=='h' || type=='y')
1107  return true;
1108  return false;
1109  }
1110 
1111  bool isFloatingPoint()
1112  {
1113  if(type=='u' || type=='k')
1114  return true;
1115  return false;
1116  }
1117 
1118  bool isNumber()
1119  {
1120  if(type=='n' || type=='w' || type=='r' || type=='i'
1121  || type=='f' || type=='x' || type=='e' || type=='l' || type=='g')
1122  return true;
1123  return false;
1124  }
1125 
1126  bool isNumberOrNullNumber()
1127  {
1128  if(type=='n' || type=='w' || type=='r' || type=='i'
1129  || type=='f' || type=='x' || type=='e' || type=='l')
1130  return true;
1131  return false;
1132  }
1133 
1134  bool isChar()
1135  {
1136  if(type=='b' || type=='c' || type=='v' || type=='z')
1137  return true;
1138  return false;
1139  }
1140 
1141  bool isDate()
1142  {
1143  if(type=='d' || type=='j')
1144  return true;
1145  return false;
1146  }
1147 
1148  int getNamedLength(bool ignoreName)
1149  {
1150  if(ignoreName)
1151  {
1152  if(getType()=='o')
1153  {
1154  if(length<256)
1155  type = 'm';
1156  else if(length<65536)
1157  type = 'q';
1158  else if(length<16777216)
1159  type = 'p';
1160  else
1161  type = 'o';
1162  return getLength();
1163  }
1164  else
1165  {
1166  int len = length;
1167  if(getType()!='n' && getType()!='w' && getType()!='r'
1168  && getType()!='i' && getType()!='f' && getType()!='x'
1169  && getType()!='e' && getType()!='l' && getType()!='b'
1170  && getType()!='c')
1171  {
1172  len++;
1173  }
1174  if(getType()=='a' || getType()=='g'
1175  || getType()=='j' || getType()=='v' || getType()=='z')
1176  return len;
1177  if(length<256)
1178  len++;
1179  else if(length<65536)
1180  len+=2;
1181  else if(length<16777216)
1182  len+=3;
1183  else
1184  len+=4;
1185  return len;
1186  }
1187  }
1188  else
1189  {
1190  if(getType()=='o')
1191  {
1192  if(2 + namedLength<256)
1193  {
1194  type = 'm';
1195  namedLength += 2;
1196  }
1197  else if(2 + namedLength<65536)
1198  {
1199  type = 'q';
1200  namedLength += 3;
1201  }
1202  else if(2 + namedLength<16777216)
1203  {
1204  type = 'p';
1205  namedLength += 4;
1206  }
1207  else
1208  {
1209  type = 'o';
1210  namedLength += 5;
1211  }
1212  return namedLength;
1213  }
1214  else if(getType()=='m' || getType()=='p' || getType()=='q')
1215  {
1216  return namedLength;
1217  }
1218  else
1219  {
1220  return 2 + namedLength;
1221  }
1222  }
1223 
1224  }
1225 
1226  void setLength(int length)
1227  {
1228  this->length = length;
1229  }
1230 
1231  char* getName()
1232  {
1233  return (char*)name.c_str();
1234  }
1235  string getNameStr()
1236  {
1237  return name;
1238  }
1239  void setName(const string& name)
1240  {
1241  this->name = name;
1242  }
1243  /*void setName(char name[])
1244  {
1245  this->name = name;
1246  }*/
1247  vector<AMEFObject*> getPackets()
1248  {
1249  return packets;
1250  }
1251  void setPackets(vector<AMEFObject*> packets)
1252  {
1253  this->packets = packets;
1254  }
1255 
1256  char getType()
1257  {
1258  return type;
1259  }
1260  void setType(char type)
1261  {
1262  this->type = type;
1263  }
1264 
1265  string getValue()
1266  {
1267  return value;
1268  }
1269  /*Object getTValue()
1270  {
1271  if(type=='s' || type=='t' || type=='d' || type=='h' || type=='y' || type=='u')
1272  return value;
1273  else if(getType()!='n' && getType()!='w' && getType()!='r'
1274  && getType()!='i')
1275  {
1276  return getIntValue();
1277  }
1278  else if(getType()!='f' && getType()!='x'
1279  && getType()!='e' && getType()!='l')
1280  {
1281  return getLongValue();
1282  }
1283  else if(getType()!='b')
1284  {
1285  return getBooleanValue();
1286  }
1287  else if(getType()!='c')
1288  {
1289  return (char)value[0];
1290  }
1291  return this;
1292  }*/
1293  string getValueStr()
1294  {
1295  return value;
1296  }
1297  void pushChar(char v)
1298  {
1299  this->value.push_back(v);
1300  }
1301  void setValue(char value[])
1302  {
1303  int len = strlen(value);
1304  for (int var = 0; var < len; var++) {
1305  this->value.push_back(value[var]);
1306  }
1307  delete[] value;
1308  }
1309  void setValue(char *value,int len)
1310  {
1311  this->value.append(value,len);
1312  }
1313  void setValue(const string& value)
1314  {
1315  this->value = value;
1316  }
1317 
1321  bool getBoolValue()
1322  {
1323  if(type=='b')
1324  return (value[0]=='1'?true:false);
1325  else
1326  return false;
1327  }
1328 
1332  int getIntValue()
1333  {
1334  if(type=='n' || type=='w' || type=='r' || type=='i')
1335  {
1336  return charArrayToInt(value);
1337  }
1338  else
1339  return -1;
1340  }
1341 
1345  short getShortValue()
1346  {
1347  if(type=='n' || type=='w' || type=='r' || type=='i')
1348  {
1349  return (short)charArrayToInt(value);
1350  }
1351  else
1352  return -1;
1353  }
1354 
1358  double getDoubleValue()
1359  {
1360  if(type=='u')
1361  return (CastUtil::lexical_cast<double>(getValueStr()));
1362  else
1363  return -1;
1364  }
1365 
1369  float getFloatValue()
1370  {
1371  if(type=='u')
1372  return (CastUtil::lexical_cast<float>(getValueStr()));
1373  else
1374  return -1;
1375  }
1376 
1380  long getLongValue()
1381  {
1382  if(type=='f' || type=='x' || type=='e' || type=='l')
1383  {
1384  return charArrayToLong(value);
1385  }
1386  else
1387  return -1;
1388  }
1389 
1390  long getNumericValue()
1391  {
1392  if(type=='f' || type=='x' || type=='e' || type=='l'
1393  || type=='n' || type=='w' || type=='r' || type=='i')
1394  {
1395  return charArrayToLong(value);
1396  }
1397  else
1398  return -1;
1399  }
1400 
1404  /*Date getDateValue()
1405  {
1406  if(type=='b')
1407  {
1408  try
1409  {
1410  return new SimpleDateFormat("ddMMyyyy HHmmss").parse(new string(value));
1411  }
1412  catch (ParseException e)
1413  {
1414  return new Date();
1415  }
1416  }
1417  else
1418  return new Date();
1419  }*/
1420 
1421  string tostring()
1422  {
1423  return displayObject("");
1424  }
1425 
1426  string displayObject(string tab)
1427  {
1428  string displ = "";
1429  for (int i=0;i<(int)getPackets().size();i++)
1430  {
1431  AMEFObject* obj = getPackets().at(i);
1432  displ += tab + "Object Type = ";
1433  displ += obj->type;
1434  displ += "\n" + tab + "Object Name = " + obj->name + "\n";
1435  displ += tab + "Object Value = ";
1436  if(obj->isStringOrNullString() || obj->isFloatingPoint() || obj->isDate())
1437  displ += (obj->getValueStr()) + "\n";
1438  else if(obj->isChar())
1439  {
1440  if(obj->type=='b')
1441  displ += CastUtil::lexical_cast<string>(obj->getBoolValue()) + "\n";
1442  else
1443  displ += (char)obj->value[0] + "\n";
1444  }
1445  else if(obj->isNumberOrNullNumber())
1446  {
1447  displ += CastUtil::lexical_cast<string>(obj->getNumericValue()) + "\n";
1448  }
1449  if(obj->type=='o' || obj->type=='p' || obj->type=='q' || obj->type=='m')
1450  {
1451  displ += obj->displayObject(tab+"\t");
1452  }
1453  }
1454  return displ;
1455  }
1456 
1457  void addStaticPacket(AMEFObject *obj)
1458  {
1459  packets.push_back(obj);
1460  }
1461 };
1462 
1463 
1464 #endif /* AMEFOBJECT_H_ */