source: trunk/FACT++/src/Converter.cc@ 11014

Last change on this file since 11014 was 10666, checked in by tbretz, 14 years ago
Make sure that also zeroes are correctly extracted even if the last character in the stream is a zero.
File size: 26.4 KB
Line 
1// **************************************************************************
2/** @class Converter
3
4@brief A compiler for the DIM data format string
5
6The Converter class interprets arguments in a string accoring to the
7given format definition and produces a corresponding memory block from it
8which can be attached to an event later.
9
10The format is given according to the Dim format description:
11
12 The format parameter specifies the contents of the structure in the
13 form T:N[;T:N]*[;T] where T is the item type: (I)nteger, (C)haracter,
14 (L)ong, (S)hort, (F)loat, (D)ouble, X(tra long) and N is the
15 number of such items. The type alone at the end means all following items
16 are of the same type. Example: "I:3;F:2;C" means 3 Integers, 2 Floats and
17 characters until the end. The format parameter is used for
18 communicating between different platforms.
19
20Note, that the strange notation T:N[;T:N]*[;T] is meant to be a regular
21expression. An Xtra-long is a 'long long'.
22
23Since Dim itself never really interpretes the format string, the programmer
24is responsible to make sure that the delivered data and the interpretation
25is consistent. Therefore the provided class can be of some help.
26
27For example:
28
29\code
30 Converter c(cout, "I:1;F:2;I:2", );
31 vector<char> v = c.GetVector("COMMAND 1 2.5 4.2 3 4");
32\endcode
33
34would produce a 20 byte data block with the integers 1, the floats
352.5 and 4.2, and the intergers 3 and 4, in this order.
36
37The opposite direction is also possible
38
39\code
40 Converter c(cout, "I:1;F:2;I:2");
41 cout << c.GetString(pointer, size) << endl;
42 \endcode
43
44Other conversion functions also exist.
45
46To check if the compilation of the format string was successfull
47the valid() member functio is provided.
48
49The format parameter \b W(ord) is dedicated to this kind of conversion and
50not understood by Dim. In addition there are \b O(ptions) which are like
51Words but can be omitted. They should only be used at the end of the string.
52Both can be encapsulated in quotationmarks '"'. Nested quotationmarks
53are not supported. \b B(ool) is also special. It evaluates true/false,
54yes/no, on/off, 1/0.
55
56The non-DIM like format options can be switched on and off by using the
57strict argument in the constructor. In general DimCommands can use these
58options, but DimServices not.
59
60@remark Note that all values are interpreted as signed, except the single
61char (e.g. C:5)
62
63*/
64// **************************************************************************
65#include "Converter.h"
66
67#include <iostream>
68#include <iomanip>
69#include <sstream>
70
71#include <cctype> // std::tolower
72#include <algorithm> // std::transform
73
74#include <boost/regex.hpp>
75
76#include "Readline.h"
77#include "WindowLog.h"
78
79using namespace std;
80
81// --------------------------------------------------------------------------
82//
83//! This function is supposed to remove all whitespaces from the format
84//! string to allow easier regular expressions later.
85//!
86//! @param s
87//! string to be cleaned
88//!
89//! @returns
90//! string cleaned from whitespaces
91//
92std::string Converter::Clean(std::string s)
93{
94 while (1)
95 {
96 const size_t pos = s.find_last_of(' ');
97 if (pos==string::npos)
98 break;
99 s.erase(pos, pos+1);
100 }
101
102 return s;
103}
104
105// --------------------------------------------------------------------------
106//
107//! This is just a simplification. For the time being it is used to output
108//! the interpreted contents to the logging stream. Its main purpose
109//! is to add the contents of val in a binary representation to the
110//! vector v
111//!
112//! @tparam
113//! data type of the variable which should be added
114//!
115//! @param val
116//! reference to the data
117//!
118//! @param v
119//! vector<char> to which the binary copy should be added
120//!
121template <class T>
122void Converter::GetBinImp(std::vector<char> &v, const T &val) const
123{
124 wout << " (" << val << ")";
125
126 v.insert(v.end(),
127 reinterpret_cast<const char*>(&val),
128 reinterpret_cast<const char*>(&val+1));
129}
130
131// --------------------------------------------------------------------------
132//
133//! This is just a simplification. For the time being it is used to output
134//! the interpreted contents to the logging stream. Its main purpose
135//! is to add the contents of val as a boost::any object to the
136//! vector v
137//!
138//! @tparam
139//! data type of the variable which should be added
140//!
141//! @param val
142//! reference to the data
143//!
144//! @param v
145//! vector<boost::any> to which the value should be added
146//!
147template <class T>
148void Converter::GetBinImp(std::vector<boost::any> &v, const T &val) const
149{
150 wout << " (" << val << ")";
151
152 v.push_back(val);
153}
154
155// --------------------------------------------------------------------------
156//
157//! This is just a simplification. For the time being it is used to output
158//! the interpreted contents to the logging stream. Its main purpose
159//! is to add the contents of the provided string at the end of the vector v.
160//! vector v
161//!
162//! @param val
163//! reference to the string
164//!
165//! @param v
166//! vector<char> to which the value should be added
167//!
168void Converter::GetBinString(std::vector<char> &v, const string &val) const
169{
170 wout << " (" << val << ")";
171
172 v.insert(v.end(), val.begin(), val.end()+1);
173}
174
175// --------------------------------------------------------------------------
176//
177//! This is just a simplification. For the time being it is used to output
178//! the interpreted contents to the logging stream. Its main purpose
179//! is to add the contents of the provided string at the end of the vector v.
180//! vector v
181//!
182//! @param val
183//! reference to the string
184//!
185//! @param v
186//! vector<boost::any> to which the value should be added
187//!
188void Converter::GetBinString(std::vector<boost::any> &v, const string &val) const
189{
190 wout << " (" << val << ")";
191
192 v.push_back(val);
193 v.push_back('\n');
194}
195
196// --------------------------------------------------------------------------
197//
198//! Converts from the stringstream into the provided type.
199//!
200//! @param line
201//! reference to the stringstream from which the data should be
202//! interpreted
203//!
204//! @tparam
205//! Type of the data to be returned
206//!
207//! @returns
208//! The interpreted data
209//!
210template <class T>
211T Converter::Get(std::stringstream &line) const
212{
213 char c;
214 line >> c;
215
216 if (c=='0')
217 {
218 if (line.peek()==-1)
219 {
220 line.clear(ios::eofbit);
221 return 0;
222 }
223
224 if (line.peek()=='x')
225 {
226 line >> c;
227 line >> hex;
228 }
229 else
230 {
231 line.unget();
232 line >> oct;
233 }
234 }
235 else
236 {
237 line.unget();
238 line >> dec;
239 }
240
241
242 T val;
243 line >> val;
244 return val;
245}
246
247// --------------------------------------------------------------------------
248//
249//! Converts from the stringstream into bool. It allows to use lexical
250//! boolean representations like yes/no, on/off, true/false and of
251//! course 0/1. If the conversion fails the failbit is set.
252//!
253//! @param line
254//! reference to the stringstream from which the data should be
255//! interpreted
256//!
257//! @returns
258//! The boolean. 0 in case of failure
259//!
260bool Converter::GetBool(std::stringstream &line) const
261{
262 string buf;
263 line >> buf;
264 transform(buf.begin(), buf.end(), buf.begin(), (int(*)(int)) std::tolower);
265
266 if (buf=="yes" || buf=="true" || buf=="on" || buf=="1")
267 return true;
268
269 if (buf=="no" || buf=="false" || buf=="off" || buf=="0")
270 return false;
271
272 line.clear(ios::failbit);
273
274 return false;
275}
276
277// --------------------------------------------------------------------------
278//
279//! Converts from the stringstream into a string. Leading whitespaces are
280//! skipped. Everything up to the next whitespace is returned.
281//! strings can be encapsulated into escape characters ("). Note, that
282//! they cannot be nested.
283//!
284//! @param line
285//! reference to the stringstream from which the data should be
286//! interpreted
287//!
288//! @returns
289//! The string
290//!
291string Converter::GetString(std::stringstream &line) const
292{
293 while (line.peek()==' ')
294 line.get();
295
296 string buf;
297 if (line.peek()=='\"')
298 {
299 line.get();
300 getline(line, buf, '\"');
301 if (line.peek()==-1)
302 line.clear(ios::eofbit);
303 }
304 else
305 line >> buf;
306
307 return buf;
308}
309
310// --------------------------------------------------------------------------
311//
312//! Converts from the stringstream into a string. Leading whitespaces are
313//! skipped. Everything until the end-of-line is returned. A trailing
314//! \0 is added.
315//!
316//! @param line
317//! reference to the stringstream from which the data should be
318//! interpreted
319//!
320//! @returns
321//! The string
322//!
323string Converter::GetStringEol(stringstream &line) const
324{
325 // Remove leading whitespaces
326 while (line.peek()==' ')
327 line.get();
328
329 line >> noskipws;
330
331 const istream_iterator<char> eol; // end-of-line iterator
332 const string s(istream_iterator<char>(line), eol);
333 return s + '\0';
334}
335
336// --------------------------------------------------------------------------
337//
338//! Converts from a binary block into a string. The type of the expected
339//! value is defined by the template parameter.
340//!
341//! @param ptr
342//! A refrenece to the pointer of the binary representation to be
343//! interpreted. The pointer is incremented by the sizeof the type.
344//!
345//! @tparam T
346//! Expected type
347//!
348//! @returns
349//! The string
350//!
351template<class T>
352string Converter::GetString(const char* &ptr) const
353{
354 const T &t = *reinterpret_cast<const T*>(ptr);
355
356 ostringstream stream;
357 stream << t;
358 ptr += sizeof(T);
359
360 return stream.str();
361}
362
363// --------------------------------------------------------------------------
364//
365//! Convert the pointer using GetString into a string and add it (prefixed
366//! by a whaitespace) to the given string.
367//!
368//! @param str
369//! Reference to the string to which the ptr should be added
370//!
371//! @param ptr
372//! Pointer to the binary representation. It will be incremented
373//! according to the sze of the template argument
374//!
375//! @tparam T
376//! Type as which the binary data should be interpreted
377//!
378template<class T>
379void Converter::Add(string &str, const char* &ptr) const
380{
381 str += ' ' + GetString<T>(ptr);
382}
383
384// --------------------------------------------------------------------------
385//
386//! Convert the pointer into a boost::any object and add it to the
387//! provided vector
388//!
389//! @param vec
390//! Vector to which the boost::any object should be added
391//!
392//! @param ptr
393//! Pointer to the binary representation. It will be incremented
394//! according to the size of the template argument
395//!
396//! @tparam T
397//! Type as which the binary data should be interpreted
398//!
399template<class T>
400void Converter::Add(vector<boost::any> &vec, const char* &ptr) const
401{
402 vec.push_back(*reinterpret_cast<const T*>(ptr));
403 ptr += sizeof(T);
404}
405
406// --------------------------------------------------------------------------
407//
408//! Add the string pointed to by ptr to the given string.
409//!
410//! @param str
411//! Reference to the string to which the ptr should be added
412//!
413//! @param ptr
414//! Pointer to the binary representation. It will be incremented
415//! according to the size of the template argument
416//!
417void Converter::AddString(string &str, const char* &ptr) const
418{
419 const string txt(ptr);
420 str += ' '+txt;
421 ptr += txt.length()+1;
422}
423
424// --------------------------------------------------------------------------
425//
426//! Add the string pointed to by ptr as boost::any to the provided vector
427//!
428//! @param vec
429//! Vector to which the boost::any object should be added
430//!
431//! @param ptr
432//! Pointer to the binary representation. It will be incremented
433//! according to the size of the template argument
434//!
435void Converter::AddString(vector<boost::any> &vec, const char* &ptr) const
436{
437 const string txt(ptr);
438 vec.push_back(txt);
439 ptr += txt.length()+1;
440}
441
442// --------------------------------------------------------------------------
443//
444//! Compiles the format string into fList. See Compile() for more details.
445//!
446//! @param out
447//! Output stream to which possible logging is redirected
448//!
449//! @param fmt
450//! Format to be compiled. For details see class reference
451//!
452//! @param strict
453//! Setting this to true allows non DIM options, whiel false
454//! will restrict the possible format strings to the ones also
455//! understood by DIM.
456//!
457Converter::Converter(std::ostream &out, const std::string &fmt, bool strict)
458: wout(out), fFormat(Clean(fmt)), fList(Compile(out, fmt, strict))
459{
460}
461
462// --------------------------------------------------------------------------
463//
464//! Compiles the format string into fList.
465//!
466//! Output by default is redirected to cout.
467//!
468//! @param fmt
469//! Format to be compiled. For details see class reference
470//!
471//! @param strict
472//! Setting this to true allows non DIM options, whiel false
473//! will restrict the possible format strings to the ones also
474//! understood by DIM.
475//!
476Converter::Converter(const std::string &fmt, bool strict)
477: wout(cout), fFormat(Clean(fmt)), fList(Compile(fmt, strict))
478{
479}
480
481// --------------------------------------------------------------------------
482//
483//! Converts the provided format string into a vector.
484//!
485//! @tparam T
486//! Kind of data to be returned. This can either be boost::any objects
487//! or a bnary data-block (char).
488//!
489//! @param str
490//! Data to be converted. For details see class reference
491//!
492//! @returns
493//! A vector of the given template type containing the arguments. In
494//! case of failure an empty vector is returned.
495//!
496//! @throws
497//! std::runtime_error if the conversion was not successfull
498//!
499template <class T>
500vector<T> Converter::Get(const std::string &str) const
501{
502 if (!valid())
503 throw runtime_error("Compiled format invalid!");
504
505 // If the format is empty we are already done
506 if (empty() && str.empty())
507 {
508 wout << endl;
509 return vector<T>();
510 }
511
512 int arg = 0;
513 stringstream line(str);
514
515 vector<T> data;
516
517 for (Converter::FormatList::const_iterator i=fList.begin(); i<fList.end()-1; i++)
518 {
519 if (*i->first.first == typeid(string))
520 {
521 GetBinString(data, GetStringEol(line));
522 line.clear(ios::eofbit);
523 continue;
524 }
525
526 // Get as many items from the input line as requested
527 for (int j=0; j<i->second.first; j++)
528 {
529 switch (i->first.first->name()[0])
530 {
531 case 'b': GetBinImp(data, GetBool(line)); break;
532 case 's': GetBinImp(data, Get<short> (line)); break;
533 case 'i': GetBinImp(data, Get<int> (line)); break;
534 case 'l': GetBinImp(data, Get<long> (line)); break;
535 case 'f': GetBinImp(data, Get<float> (line)); break;
536 case 'd': GetBinImp(data, Get<double> (line)); break;
537 case 'x': GetBinImp(data, Get<long long>(line)); break;
538 case 'c':
539 if (line.peek()==-1)
540 {
541 line.clear(ios::failbit|ios::eofbit);
542 break;
543 }
544 GetBinImp(data, Get<unsigned char>(line));
545 if (line.peek()==-1)
546 line.clear(ios::eofbit);
547 break;
548 case 'N':
549 GetBinString(data, GetString(line));
550 if (*i->first.first == typeid(O))
551 line.clear(ios::goodbit|(line.rdstate()&ios::eofbit));
552 break;
553 default:
554 // This should never happen!
555 throw runtime_error("Format '"+string(i->first.first->name())+" not supported!");
556 }
557
558 arg++;
559 }
560
561 if (!line)
562 break;
563 }
564 wout << endl;
565
566 // Something wrong with the conversion (e.g. 5.5 for an int)
567 if (line.fail() && !line.eof())
568 {
569 line.clear(); // This is necesasary to get a proper response from tellg()
570
571 ostringstream err;
572 err << "Error converting argument at " << arg << " [fmt=" << fFormat << "]!\n";
573 err << line.str() << "\n";
574 err << setw(int(line.tellg())) << " " << "^\n";
575 throw runtime_error(err.str());
576 }
577
578 // Not enough arguments, we have not reached the end
579 if (line.fail() && line.eof())
580 {
581 line.clear();
582
583 ostringstream err;
584 err << "Not enough arguments [fmt=" << fFormat << "]!\n";
585 err << line.str() << "\n";
586 err << setw(int(line.tellg())+1) << " " << "^\n";
587 throw runtime_error(err.str());
588 }
589
590 // Too many arguments, we have not reached the end
591 // Unfortunately, this can also mean that there is something
592 // wrong with the last argument
593 if (line.good() && !line.eof())
594 {
595 ostringstream err;
596 err << "More arguments available than expected [fmt=" << fFormat << "]!\n";
597 err << line.str() << "\n";
598 err << setw(int(line.tellg())+1) << " " << "^\n";
599 throw runtime_error(err.str());
600 }
601
602 return data;
603
604}
605
606std::vector<boost::any> Converter::GetAny(const std::string &str) const
607{
608 return Get<boost::any>(str);
609}
610
611std::vector<char> Converter::GetVector(const std::string &str) const
612{
613 return Get<char>(str);
614}
615
616// --------------------------------------------------------------------------
617//
618//! Converts the provided data block into a vector of boost::any or
619//! a string.
620//!
621//! @tparam T
622//! Kind of data to be returned. This can either be boost::any objects
623//! or a string
624//!
625//! @returns
626//! A vector of the given template type containing the arguments. In
627//! case of failure an empty vector is returned.
628//!
629//! @throws
630//! std::runtime_error if the conversion was not successfull
631//!
632template<class T>
633T Converter::Get(const void *dat, size_t size) const
634{
635 if (!valid())
636 throw runtime_error("Compiled format invalid!");
637
638 if (dat==0)
639 throw runtime_error("Data pointer == NULL!");
640
641 const char *ptr = reinterpret_cast<const char *>(dat);
642
643 T text;
644 for (Converter::FormatList::const_iterator i=fList.begin(); i<fList.end()-1; i++)
645 {
646 if (ptr-size>dat)
647 {
648 ostringstream err;
649 err << "Format description [fmt=" << fFormat << "] exceeds available data size (" << size << ")";
650 throw runtime_error(err.str());
651 }
652
653 if (*i->first.first == typeid(string))
654 {
655 if (size>0)
656 AddString(text, ptr);
657 if (ptr-size<=dat)
658 return text;
659 break;
660 }
661
662 // Get as many items from the input line as requested
663 for (int j=0; j<i->second.first; j++)
664 {
665 switch (i->first.first->name()[0])
666 {
667 case 'b': Add<bool> (text, ptr); break;
668 case 'c': Add<char> (text, ptr); break;
669 case 's': Add<short> (text, ptr); break;
670 case 'i': Add<int> (text, ptr); break;
671 case 'l': Add<long> (text, ptr); break;
672 case 'f': Add<float> (text, ptr); break;
673 case 'd': Add<double> (text, ptr); break;
674 case 'x': Add<long long>(text, ptr); break;
675 case 'N': AddString(text, ptr); break;
676
677 case 'v':
678 // This should never happen!
679 throw runtime_error("Type 'void' not supported!");
680 default:
681 throw runtime_error("TypeId '"+string(i->first.first->name())+"' not known!");
682 }
683 }
684 }
685
686 if (ptr-size!=dat)
687 {
688 ostringstream err;
689 err << "Data block size (" << size << ") doesn't fit format description [fmt=" << fFormat << "]";
690 throw runtime_error(err.str());
691 }
692
693 return text;
694}
695
696std::vector<boost::any> Converter::GetAny(const void *dat, size_t size) const
697{
698 return Get<vector<boost::any>>(dat, size);
699}
700
701std::vector<char> Converter::GetVector(const void *dat, size_t size) const
702{
703 const string ref = GetString(dat, size);
704
705 vector<char> data;
706 data.insert(data.begin(), ref.begin()+1, ref.end());
707 data.push_back(0);
708
709 return data;
710}
711
712string Converter::GetString(const void *dat, size_t size) const
713{
714 const string s = Get<string>(dat, size);
715 return s.empty() ? s : s.substr(1);
716}
717
718template<class T>
719Converter::Type Converter::GetType()
720{
721 Type t;
722 t.first = &typeid(T);
723 t.second = sizeof(T);
724 return t;
725}
726
727template<class T>
728Converter::Type Converter::GetVoid()
729{
730 Type t;
731 t.first = &typeid(T);
732 t.second = 0;
733 return t;
734}
735
736// --------------------------------------------------------------------------
737//
738//! static function to compile a format string.
739//!
740//! @param out
741//! Output stream to which possible logging is redirected
742//!
743//! @param fmt
744//! Format to be compiled. For details see class reference
745//!
746//! @param strict
747//! Setting this to true allows non DIM options, whiel false
748//! will restrict the possible format strings to the ones also
749//! understood by DIM.
750//!
751Converter::FormatList Converter::Compile(std::ostream &out, const std::string &fmt, bool strict)
752{
753 ostringstream text;
754
755 // Access both, the data and the format through a stringstream
756 stringstream stream(fmt);
757
758 // For better performance we could use sregex
759 static const boost::regex expr1("^([CSILFDXBOW])(:([1-9]+[0-9]*))?$");
760 static const boost::regex expr2("^([CSILFDX])(:([1-9]+[0-9]*))?$");
761
762 FormatList list;
763 Format format;
764
765 // Tokenize the format
766 string buffer;
767 while (getline(stream, buffer, ';'))
768 {
769 boost::smatch what;
770 if (!boost::regex_match(buffer, what, strict?expr2:expr1))
771 {
772 out << kRed << "Wrong format string '" << buffer << "'!" << endl;
773 return FormatList();
774 }
775
776 const string t = what[1]; // type id
777 const string n = what[3]; // counter
778
779 const int cnt = atoi(n.c_str());
780
781 // if the :N part was not given assume 1
782 format.second.first = cnt == 0 ? 1 : cnt;
783
784 if (strict && t[0]=='C' && cnt>0)
785 {
786 out << kRed << "Dim doesn't support the format C with N>0!" << endl;
787 return FormatList();
788 }
789
790 // Check if the format is just C (without a number)
791 // That would mean that it is a \0 terminated string
792 if (t[0]=='C' && cnt==0)
793 {
794 format.first = GetType<string>();
795 list.push_back(format);
796 format.second.second = 0; // end position not known
797 break;
798 }
799
800 // Get as many items from the input line as requested
801 switch (t[0])
802 {
803 case 'B': format.first = GetType<bool>(); break;
804 case 'C': format.first = GetType<char>(); break;
805 case 'S': format.first = GetType<short>(); break;
806 case 'I': format.first = GetType<int>(); break;
807 case 'L': format.first = GetType<long>(); break;
808 case 'F': format.first = GetType<float>(); break;
809 case 'D': format.first = GetType<double>(); break;
810 case 'X': format.first = GetType<long long>(); break;
811 case 'O': format.first = GetVoid<O>(); break;
812 case 'W': format.first = GetVoid<W>(); break;
813 default:
814 // This should never happen!
815 out << kRed << "Format '" << t[0] << " not known!" << endl;
816 return list;
817 }
818
819 list.push_back(format);
820 format.second.second += format.first.second * format.second.first;
821 }
822
823 format.first = GetVoid<void>();
824 format.second.first = 0;
825
826 list.push_back(format);
827
828 return list;
829}
830
831// --------------------------------------------------------------------------
832//
833//! Same as Compile(ostream&,string&,bool) but cout is used as the default
834//! output stream.
835//!
836//!
837Converter::FormatList Converter::Compile(const std::string &fmt, bool strict)
838{
839 return Compile(cout, fmt, strict);
840}
841
842vector<string> Converter::Regex(const string &expr, const string &line)
843{
844 const boost::regex reg(expr);
845
846 boost::smatch what;
847 if (!boost::regex_match(line, what, reg, boost::match_extra))
848 return vector<string>();
849
850 vector<string> ret;
851 for (unsigned int i=0; i<what.size(); i++)
852 ret.push_back(what[i]);
853
854 return ret;
855}
856
857
858void Converter::ToFits(void *dest, const void *src, size_t size) const
859{
860 // crawl through the src buffer and copy the data appropriately to the
861 // destination buffer
862 // Assumption: the string is always last. This way we
863 // use the provided size to determine the number
864 // of character to copy
865
866 char *charDest = static_cast<char*>(dest);
867 const char *charSrc = static_cast<const char*>(src);
868
869 for (Converter::FormatList::const_iterator i=fList.begin(); i!=fList.end(); i++)
870 {
871//ETIENNE this check fails for very fine cases. Disabled it
872 // FIXME: This is still not really safe
873// if (charDest-size>=dest)
874// {
875// ostringstream err;
876// err << "Format description [fmt=" << fFormat << "] exceeds available data size (" << size << ")";
877// throw runtime_error(err.str());
878// }
879
880 const char type = i->first.first->name()[0];
881
882 // string types
883 if (type=='S' || type=='O' || type=='W')
884 {
885 // copy string until termination
886 while (*charSrc)
887 *charDest++ = *charSrc++;
888
889 // Copy \0-termination
890//ETIENNE: Do not copy the \0 as it must not be written to fits files. just increment charSrc instead
891 // *charDest++ = *charSrc++;
892 charSrc++;
893 continue;
894 }
895
896 const int s = i->first.second; // size of element
897 const int n = i->second.first; // number of elements
898
899 // for all elements of this column
900 for (int j=0; j<n; j++)
901 {
902 reverse_copy(charSrc, charSrc+s, charDest);
903
904 charSrc += s;
905 charDest += s;
906 }
907 }
908
909 if (charDest-size!=dest)
910 {
911 ostringstream err;
912 err << "Data block size (" << size << ") doesn't fit format description [fmt=" << fFormat << "]";
913 throw runtime_error(err.str());
914 }
915}
916
917vector<char> Converter::ToFits(const void *src, size_t size) const
918{
919 vector<char> dest(size);
920 ToFits(&dest[0], src, size);
921 return dest;
922}
923
924
925void Converter::Print(std::ostream &out) const
926{
927 for (FormatList::const_iterator i=fList.begin(); i!=fList.end(); i++)
928 {
929 out << "Type=" << i->first.first->name() << "[" << i->first.second << "] ";
930 out << "N=" << i->second.first << " ";
931 out << "offset=" << i->second.second << endl;
932 }
933}
934
935void Converter::Print() const
936{
937 return Print(cout);
938}
Note: See TracBrowser for help on using the repository browser.