~ubuntu-branches/ubuntu/raring/sunpinyin/raring

« back to all changes in this revision

Viewing changes to src/slm/tslmendian/writer.h

  • Committer: Package Import Robot
  • Author(s): YunQiang Su
  • Date: 2012-03-30 15:31:55 UTC
  • mfrom: (1.1.3) (1.2.7 sid)
  • Revision ID: package-import@ubuntu.com-20120330153155-qgls77sogzgtg9zp
Tags: 2.0.3+git20120222-1
* Team upload: git snapshot 20120222.
   - fix breaks if LDFLAGS in environment contains
       multiple words (Closese #646001).
   - rm patches merged to upstream:
       append-os-environ-toenv.patch
       fix-ftbfs-on-sh.patch
       remove-10-candidate-words-limitation.patch
   - refresh disable-lm-dict-compile.patch.
* Bump stardard version to 3.9.3: no modify needed.
* add libsunpinyin3-dbg and python-sunpinyin packages.
* debian/compat to 9, multiarch it.
* rewrite debian/rules with dh 7 format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
// change the byte order of given variable
22
22
template <typename Type>
23
 
Type change_byte_order(const Type& v)
24
 
{
 
23
Type change_byte_order(const Type& v){
25
24
    Type t = v;
26
25
    const size_t size = sizeof(v);
27
 
    uint8_t* first = (uint8_t*)(&t);
28
 
    uint8_t* last  = first+size-1;
 
26
    uint8_t* first = (uint8_t *) (&t);
 
27
    uint8_t* last = first + size - 1;
29
28
    while (first < last) {
30
29
        std::swap(*first++, *last--);
31
30
    }
32
31
    return t;
33
32
}
34
 
    
 
33
 
35
34
template <typename T>
36
35
class OtherEndian
37
36
{
38
37
public:
39
38
    typedef T TargetType;
40
 
    static TargetType create(const T& from)
41
 
    {
 
39
    static TargetType create(const T& from){
42
40
        return from;
43
41
    }
44
42
};
45
43
 
46
44
#if WORDS_BIGENDIAN
47
 
#define DEFINE_OTHER_TYPE(__T__) typedef __T__##_BE TargetType
 
45
#define DEFINE_OTHER_TYPE(__T__) typedef __T__ ## _BE TargetType
48
46
#else
49
 
#define DEFINE_OTHER_TYPE(__T__) typedef __T__##_LE TargetType
 
47
#define DEFINE_OTHER_TYPE(__T__) typedef __T__ ## _LE TargetType
50
48
#endif
51
49
 
52
50
//
53
 
// we always defined a reversed layout of big-endian and little-endian 
 
51
// we always defined a reversed layout of big-endian and little-endian
54
52
// bit-field struct, so such kind of struct need to be reverted if host
55
53
// arch is different from build arch.
56
54
//
57
55
template <typename T>
58
 
bool revert_write(const T& t, FILE *fp)
59
 
{
 
56
bool revert_write(const T& t, FILE *fp){
60
57
    T reverted = change_byte_order(t);
61
58
    typename OtherEndian<T>::TargetType o =
62
59
        OtherEndian<T>::create(reverted);
72
69
{
73
70
public:
74
71
    Writer(FILE *fp, bool doRevert)
75
 
        : m_fp (fp), m_doRevert(doRevert)
 
72
        : m_fp(fp), m_doRevert(doRevert)
76
73
    {}
77
 
    
 
74
 
78
75
    template <typename T>
79
 
    bool write(const T& t)
80
 
    {
 
76
    bool write(const T& t){
81
77
        if (m_doRevert)
82
78
            return revert_write(t, m_fp);
83
79
        else
84
80
            return fwrite(&t, sizeof(t), 1, m_fp) == 1;
85
81
    }
86
 
    
87
 
    
 
82
 
 
83
 
88
84
    template <typename T>
89
 
    bool write(const T* t, size_t len)
90
 
    {
 
85
    bool write(const T* t, size_t len){
91
86
        for (unsigned i = 0; i < len; i++) {
92
87
            if (!write(t[i]))
93
88
                return false;