pEp_utility.cpp
author Volker Birk <vb@pep.foundation>
Thu, 26 Apr 2018 15:30:43 +0200
branchCOM-87
changeset 282 3b92433f8da7
parent 269 26c8597fe860
child 302 b3444780fa9f
child 324 ef317c83c07a
permissions -rw-r--r--
adding input flags
vb@10
     1
#include "stdafx.h"
vb@33
     2
#include "pEp_utility.h"
vb@33
     3
vb@33
     4
using namespace ATL;
vb@10
     5
vb@10
     6
namespace pEp {
vb@10
     7
    namespace utility {
vb@10
     8
        pEp_identity_cpp::pEp_identity_cpp(const ::pEp_identity *_ident)
vb@10
     9
        {
markus@269
    10
            if (!_ident)
markus@269
    11
                return;
vb@212
    12
vb@10
    13
            if (_ident->address)
vb@10
    14
                address = _ident->address;
vb@10
    15
            if (_ident->fpr)
vb@10
    16
                fpr = _ident->fpr;
vb@10
    17
            if (_ident->user_id)
vb@10
    18
                user_id = _ident->user_id;
vb@10
    19
            if (_ident->username)
vb@10
    20
                username = _ident->username;
markus@269
    21
            comm_type = (pEpComType)_ident->comm_type;
vb@10
    22
            lang = _ident->lang;
markus@269
    23
            flags = (int)_ident->flags;
vb@10
    24
        }
vb@10
    25
markus@172
    26
        pEp_identity_cpp::pEp_identity_cpp(const pEpIdentity *_ident)
vb@10
    27
        {
markus@269
    28
            if (!_ident)
markus@269
    29
                return;
vb@212
    30
markus@269
    31
            if (_ident->Address)
markus@180
    32
                address = utf8_string(_ident->Address);
markus@180
    33
            if (_ident->Fpr)
markus@180
    34
                fpr = utf8_string(_ident->Fpr);
markus@180
    35
            if (_ident->UserId)
markus@180
    36
                user_id = utf8_string(_ident->UserId);
markus@180
    37
            if (_ident->UserName)
markus@180
    38
                username = utf8_string(_ident->UserName);
markus@180
    39
            comm_type = _ident->CommType;
markus@180
    40
            if (_ident->Lang)
markus@180
    41
                lang = utf8_string(_ident->Lang);
markus@269
    42
            flags = (int)_ident->Flags;
vb@10
    43
        }
vb@10
    44
vb@10
    45
        pEp_identity * pEp_identity_cpp::to_pEp_identity()
vb@10
    46
        {
vb@10
    47
            ::pEp_identity *_ident = ::new_identity(this->address.c_str(), this->fpr.c_str(), this->user_id.c_str(), this->username.c_str());
vb@10
    48
            assert(_ident);
vb@10
    49
            if (_ident == NULL)
vb@10
    50
                throw bad_alloc();
vb@10
    51
vb@10
    52
            _ident->comm_type = (::PEP_comm_type) this->comm_type;
vb@10
    53
vb@10
    54
            assert(this->lang.size() == 0 || this->lang.size() == 2);
vb@10
    55
vb@10
    56
            if (this->lang.size()) {
vb@10
    57
                _ident->lang[0] = this->lang[0];
vb@10
    58
                _ident->lang[1] = this->lang[1];
vb@10
    59
            }
vb@10
    60
markus@269
    61
            _ident->flags = (identity_flags) this->flags;
vb@217
    62
vb@10
    63
            return _ident;
vb@10
    64
        }
vb@10
    65
markus@172
    66
        pEpIdentity * pEp_identity_cpp::to_pEp_identity_s()
vb@10
    67
        {
markus@269
    68
            pEpIdentity *_ident = (pEpIdentity *)calloc(1, sizeof(pEpIdentity));
vb@10
    69
            assert(_ident);
vb@10
    70
            if (_ident == NULL)
vb@10
    71
                throw bad_alloc();
vb@10
    72
markus@180
    73
            _ident->Address = utf16_bstr(this->address);
markus@180
    74
            _ident->CommType = this->comm_type;
markus@180
    75
            _ident->Fpr = utf16_bstr(this->fpr);
markus@180
    76
            _ident->Lang = utf16_bstr(this->lang);
markus@180
    77
            _ident->UserName = utf16_bstr(this->username);
markus@180
    78
            _ident->UserId = utf16_bstr(this->user_id);
markus@269
    79
            _ident->Flags = (pEpIdentityFlags) this->flags;
vb@10
    80
vb@10
    81
            return _ident;
vb@10
    82
        }
vb@10
    83
markus@172
    84
        void copy_identity(pEpIdentity * ident_s, const pEp_identity * ident)
vb@10
    85
        {
vb@10
    86
            assert(ident_s);
markus@269
    87
            if (!ident_s)
markus@269
    88
                throw invalid_argument("ident_s");
vb@10
    89
markus@172
    90
            ::memset(ident_s, 0, sizeof(pEpIdentity));
vb@10
    91
            if (ident) {
vb@10
    92
                if (ident->address)
markus@180
    93
                    ident_s->Address = utf16_bstr(ident->address);
vb@10
    94
                if (ident->fpr)
markus@180
    95
                    ident_s->Fpr = utf16_bstr(ident->fpr);
vb@10
    96
                if (ident->user_id)
markus@180
    97
                    ident_s->UserId = utf16_bstr(ident->user_id);
vb@10
    98
                if (ident->username)
markus@180
    99
                    ident_s->UserName = utf16_bstr(ident->username);
markus@269
   100
                ident_s->CommType = (pEpComType)ident->comm_type;
vb@10
   101
                if (ident->lang)
markus@180
   102
                    ident_s->Lang = utf16_bstr(ident->lang);
markus@269
   103
                ident_s->Flags = (pEpIdentityFlags)ident->flags;
vb@10
   104
            }
vb@10
   105
        }
vb@10
   106
markus@172
   107
        ::pEp_identity *new_identity(const pEpIdentity * ident)
vb@10
   108
        {
vb@37
   109
            if (ident == NULL)
vb@37
   110
                return NULL;
vb@37
   111
vb@10
   112
            ::pEp_identity *_ident;
vb@10
   113
vb@10
   114
            string _address;
vb@10
   115
            string _fpr;
vb@10
   116
            string _user_id;
vb@10
   117
            string _username;
vb@10
   118
markus@180
   119
            if (ident->Address)
markus@180
   120
                _address = utf8_string(ident->Address);
markus@180
   121
            if (ident->Fpr) {
markus@180
   122
                _fpr = utf8_string(ident->Fpr);
vb@10
   123
                for (auto p = _fpr.begin(); p != _fpr.end(); ++p) {
vb@10
   124
                    if (*p >= 'A' && *p <= 'Z')
vb@10
   125
                        continue;
vb@10
   126
                    if (*p >= '0' && *p <= '9')
vb@10
   127
                        continue;
vb@10
   128
                    throw invalid_argument("invalid hex digits in fingerprint");
vb@10
   129
                }
vb@10
   130
            }
markus@180
   131
            if (ident->UserId)
markus@180
   132
                _user_id = utf8_string(ident->UserId);
markus@180
   133
            if (ident->UserName)
markus@180
   134
                _username = utf8_string(ident->UserName);
vb@10
   135
vb@10
   136
            _ident = ::new_identity(_address.c_str(), _fpr.c_str(), _user_id.c_str(), _username.c_str());
vb@10
   137
            assert(_ident);
vb@10
   138
            if (_ident == NULL)
vb@10
   139
                throw bad_alloc();
vb@10
   140
markus@269
   141
            _ident->comm_type = (PEP_comm_type)ident->CommType;
vb@10
   142
markus@180
   143
            if (ident->Lang) {
markus@180
   144
                string _lang = utf8_string(ident->Lang);
vb@10
   145
                if (_lang.length() != 0) {
vb@10
   146
                    if (_lang.length() != 2) {
vb@10
   147
                        ::free_identity(_ident);
vb@10
   148
                        throw invalid_argument("invalid language code");
vb@10
   149
                    }
vb@10
   150
                    if (_lang[0] < 'a' || _lang[0] > 'z') {
vb@10
   151
                        ::free_identity(_ident);
vb@10
   152
                        throw invalid_argument("invalid language code");
vb@10
   153
                    }
vb@10
   154
                    if (_lang[1] < 'a' || _lang[1] > 'z') {
vb@10
   155
                        ::free_identity(_ident);
vb@10
   156
                        throw invalid_argument("invalid language code");
vb@10
   157
                    }
vb@10
   158
                    _ident->lang[0] = _lang[0];
vb@10
   159
                    _ident->lang[1] = _lang[1];
vb@10
   160
                }
vb@10
   161
            }
vb@10
   162
markus@269
   163
            _ident->flags = (identity_flags_t)ident->Flags;
vb@217
   164
vb@10
   165
            return _ident;
vb@10
   166
        }
vb@33
   167
vb@33
   168
        template< class T2, class T > T2 from_C(T *tl);
vb@33
   169
vb@41
   170
        BSTR bstr(char *s)
vb@33
   171
        {
vb@42
   172
            if (s == NULL)
vb@33
   173
                return _bstr_t(L"").Detach();
vb@42
   174
vb@42
   175
            return utf16_bstr(s);
vb@33
   176
        }
vb@33
   177
markus@172
   178
        template<> Blob *from_C< Blob *, bloblist_t >(bloblist_t *tl)
vb@33
   179
        {
markus@269
   180
            assert(tl);
vb@212
   181
vb@33
   182
            CComSafeArray<BYTE> sa;
markus@269
   183
            if (tl) {
markus@269
   184
                sa.Create(tl->size);
markus@269
   185
                if (tl->size) {
markus@269
   186
                    char *data;
markus@269
   187
                    SafeArrayAccessData(sa, (void **)&data);
markus@269
   188
                    memcpy(data, tl->value, tl->size);
markus@269
   189
                    SafeArrayUnaccessData(sa);
markus@269
   190
                }
markus@269
   191
            }
markus@269
   192
            else {
markus@269
   193
                sa.Create((ULONG)0);
markus@269
   194
            }
vb@33
   195
markus@172
   196
            Blob *_blob = new Blob();
vb@33
   197
vb@33
   198
            _blob->value = sa.Detach();
markus@180
   199
            _blob->MimeType = bstr(tl->mime_type);
markus@180
   200
            _blob->Filename = bstr(tl->filename);
vb@33
   201
vb@33
   202
            return _blob;
vb@33
   203
        }
vb@33
   204
vb@33
   205
        template< class T > int length(T *);
vb@33
   206
vb@33
   207
        template< class T2, class T > SAFEARRAY * array_from_C(T *tl)
vb@33
   208
        {
vb@39
   209
            if (tl == NULL)
vb@39
   210
                return newSafeArray<T2>(0);
vb@39
   211
vb@33
   212
            int len = length<T>(tl);
vb@33
   213
vb@33
   214
            LPSAFEARRAY sa = newSafeArray<T2>(len);
vb@33
   215
            LONG lbound, ubound;
vb@33
   216
            SafeArrayGetLBound(sa, 1, &lbound);
vb@33
   217
            SafeArrayGetUBound(sa, 1, &ubound);
vb@33
   218
vb@33
   219
            T *_tl = tl;
vb@38
   220
            for (LONG i = lbound; i <= ubound; _tl = _tl->next, i++)
vb@33
   221
                SafeArrayPutElement(sa, &i, from_C<T2 *, T>(_tl));
vb@33
   222
vb@33
   223
            return sa;
vb@33
   224
        }
vb@33
   225
markus@172
   226
        void clear_identity_s(pEpIdentity& ident)
vb@41
   227
        {
markus@180
   228
            SysFreeString(ident.Address);
markus@180
   229
            SysFreeString(ident.Fpr);
markus@180
   230
            SysFreeString(ident.Lang);
markus@180
   231
            SysFreeString(ident.UserName);
markus@180
   232
            SysFreeString(ident.UserId);
vb@41
   233
markus@172
   234
            memset(&ident, 0, sizeof(pEpIdentity));
vb@41
   235
        }
vb@41
   236
markus@172
   237
        template<> pEpIdentity from_C< pEpIdentity, pEp_identity >(pEp_identity *tl)
vb@33
   238
        {
markus@172
   239
            pEpIdentity _ident;
vb@41
   240
            memset(&_ident, 0, sizeof(_ident));
vb@41
   241
vb@39
   242
            if (tl)
vb@39
   243
                copy_identity(&_ident, tl);
vb@33
   244
            return _ident;
vb@33
   245
        }
vb@33
   246
markus@172
   247
        pEpIdentity identity_s(pEp_identity *ident)
vb@33
   248
        {
markus@172
   249
            return from_C< pEpIdentity, pEp_identity >(ident);
vb@33
   250
        }
vb@33
   251
markus@172
   252
        template<> pEpIdentity *from_C< pEpIdentity *, identity_list >(identity_list *il)
vb@33
   253
        {
markus@172
   254
            pEpIdentity *ident = new pEpIdentity();
markus@172
   255
            memset(ident, 0, sizeof(pEpIdentity));
vb@41
   256
vb@39
   257
            if (il)
vb@39
   258
                copy_identity(ident, il->ident);
vb@33
   259
            return ident;
vb@33
   260
        }
vb@33
   261
markus@171
   262
        template<> StringPair *from_C< StringPair *, stringpair_list_t >(stringpair_list_t * sp)
vb@33
   263
        {
markus@171
   264
            StringPair *fld = new StringPair();
vb@39
   265
            if (sp) {
markus@180
   266
                fld->Name = bstr(sp->value->key);
markus@180
   267
                fld->Value = bstr(sp->value->value);
vb@39
   268
            }
vb@33
   269
            return fld;
vb@33
   270
        }
vb@33
   271
vb@33
   272
        template<> int length<identity_list>(identity_list *tl)
vb@33
   273
        {
vb@33
   274
            return identity_list_length(tl);
vb@33
   275
        }
vb@33
   276
vb@33
   277
        template<> int length<bloblist_t>(bloblist_t *tl)
vb@33
   278
        {
vb@33
   279
            return bloblist_length(tl);
vb@33
   280
        }
vb@33
   281
vb@33
   282
        template<> int length<stringpair_list_t>(stringpair_list_t *tl)
vb@33
   283
        {
vb@33
   284
            return stringpair_list_length(tl);
vb@33
   285
        }
vb@33
   286
markus@172
   287
        void clear_text_message(TextMessage *msg)
vb@41
   288
        {
markus@269
   289
            assert(msg);
markus@269
   290
            if (!msg)
markus@269
   291
                return;
vb@210
   292
markus@180
   293
            SysFreeString(msg->Id);
markus@182
   294
            SysFreeString(msg->ShortMsg);
markus@182
   295
            SysFreeString(msg->LongMsg);
markus@182
   296
            SysFreeString(msg->LongMsgFormatted);
markus@180
   297
            SafeArrayDestroy(msg->Attachments);
markus@180
   298
            clear_identity_s(msg->From);
markus@180
   299
            SafeArrayDestroy(msg->To);
markus@180
   300
            clear_identity_s(msg->RecvBy);
markus@180
   301
            SafeArrayDestroy(msg->Cc);
markus@180
   302
            SafeArrayDestroy(msg->Bcc);
markus@180
   303
            SafeArrayDestroy(msg->ReplyTo);
markus@180
   304
            SafeArrayDestroy(msg->References);
markus@180
   305
            SafeArrayDestroy(msg->Keywords);
markus@180
   306
            SysFreeString(msg->Comments);
markus@180
   307
            SafeArrayDestroy(msg->OptFields);
vb@41
   308
markus@172
   309
            memset(msg, 0, sizeof(TextMessage));
vb@41
   310
        }
vb@41
   311
markus@172
   312
        void text_message_from_C(TextMessage *msg2, const ::message *msg)
vb@33
   313
        {
vb@37
   314
            assert(msg2);
vb@33
   315
            assert(msg);
vb@33
   316
markus@269
   317
            if (!msg2) {
markus@269
   318
                msg2 = (TextMessage *)calloc(1, sizeof(TextMessage));
markus@269
   319
                assert(msg2);
markus@269
   320
                if (!msg2)
markus@269
   321
                    throw bad_alloc();
markus@269
   322
            }
markus@269
   323
            else {
markus@269
   324
                clear_text_message(msg2);
markus@269
   325
            }
vb@211
   326
markus@269
   327
            if (!msg)
markus@269
   328
                return;
vb@41
   329
markus@269
   330
            msg2->Dir = (pEpMsgDirection)msg->dir;
markus@180
   331
            msg2->Id = bstr(msg->id);
markus@182
   332
            msg2->ShortMsg = bstr(msg->shortmsg);
markus@182
   333
            msg2->LongMsg = bstr(msg->longmsg);
markus@182
   334
            msg2->LongMsgFormatted = bstr(msg->longmsg_formatted);
markus@180
   335
            msg2->Attachments = array_from_C<Blob, bloblist_t>(msg->attachments);
vb@39
   336
            if (msg->sent)
markus@180
   337
                msg2->Sent = mktime(msg->sent);
vb@39
   338
            if (msg->recv)
markus@180
   339
                msg2->Recv = mktime(msg->recv);
markus@180
   340
            msg2->From = identity_s(msg->from);
markus@180
   341
            msg2->To = array_from_C<pEpIdentity, identity_list>(msg->to);
markus@180
   342
            msg2->RecvBy = identity_s(msg->recv_by);
markus@180
   343
            msg2->Cc = array_from_C<pEpIdentity, identity_list>(msg->cc);
markus@180
   344
            msg2->Bcc = array_from_C<pEpIdentity, identity_list>(msg->bcc);
markus@180
   345
            msg2->ReplyTo = array_from_C<pEpIdentity, identity_list>(msg->reply_to);
markus@180
   346
            msg2->References = string_array(msg->references);
markus@180
   347
            msg2->Keywords = string_array(msg->keywords);
markus@180
   348
            msg2->Comments = bstr(msg->comments);
markus@180
   349
            msg2->OptFields = array_from_C<StringPair, stringpair_list_t>(msg->opt_fields);
vb@33
   350
        }
vb@33
   351
vb@37
   352
        char * str(BSTR s)
vb@33
   353
        {
vb@37
   354
            string str = utf8_string(s);
vb@37
   355
            char *_s = _strdup(str.c_str());
vb@33
   356
            if (_s == NULL)
vb@33
   357
                throw bad_alloc();
vb@33
   358
vb@37
   359
            return _s;
vb@33
   360
        }
vb@33
   361
markus@172
   362
        void clear_blob(Blob& b)
vb@41
   363
        {
markus@180
   364
            SysFreeString(b.Filename);
markus@180
   365
            SysFreeString(b.MimeType);
vb@41
   366
            SafeArrayDestroy(b.value);
markus@172
   367
            memset(&b, 0, sizeof(Blob));
vb@41
   368
        }
vb@41
   369
vb@33
   370
        bloblist_t *bloblist(SAFEARRAY *sa)
vb@33
   371
        {
vb@37
   372
            if (sa == NULL)
vb@37
   373
                return NULL;
vb@37
   374
vb@33
   375
            LONG lbound, ubound;
vb@33
   376
            SafeArrayGetLBound(sa, 1, &lbound);
vb@33
   377
            SafeArrayGetUBound(sa, 1, &ubound);
vb@33
   378
vb@33
   379
            size_t size = ubound - lbound + 1;
vb@33
   380
            if (size <= 0)
vb@33
   381
                return NULL;
vb@33
   382
vb@33
   383
            bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL);
vb@33
   384
            if (bl == NULL)
vb@33
   385
                throw bad_alloc();
vb@33
   386
vb@33
   387
            bloblist_t *_bl = bl;
vb@33
   388
            for (LONG i = lbound; i <= ubound; i++) {
markus@172
   389
                Blob b;
markus@172
   390
                memset(&b, 0, sizeof(Blob));
vb@33
   391
                SafeArrayGetElement(sa, &i, &b);
vb@33
   392
vb@38
   393
                LONG _lbound, _ubound;
vb@38
   394
                SafeArrayGetLBound(b.value, 1, &_lbound);
vb@38
   395
                SafeArrayGetUBound(b.value, 1, &_ubound);
vb@38
   396
                size_t size = _ubound - _lbound + 1;
vb@38
   397
vb@136
   398
                char *buffer;
vb@136
   399
                if (size) {
markus@269
   400
                    buffer = (char *)malloc(size + 1);
vb@136
   401
                    if (buffer == NULL)
vb@136
   402
                        throw bad_alloc();
vb@33
   403
vb@136
   404
                    char *data;
vb@33
   405
markus@269
   406
                    SafeArrayAccessData(b.value, (void **)&data);
vb@136
   407
                    memcpy(buffer, data, size);
markus@269
   408
                    buffer[size] = 0; // safeguard
vb@136
   409
                    SafeArrayUnaccessData(sa);
vb@136
   410
                }
vb@136
   411
                else {
vb@136
   412
                    buffer = _strdup("");
vb@136
   413
                    if (buffer == NULL)
vb@136
   414
                        throw bad_alloc();
vb@33
   415
vb@136
   416
                }
markus@180
   417
                _bl = bloblist_add(_bl, buffer, size, str(b.MimeType), str(b.Filename));
vb@33
   418
                if (_bl == NULL) {
vb@42
   419
                    free(buffer);
vb@41
   420
                    clear_blob(b);
vb@33
   421
                    free_bloblist(bl);
vb@33
   422
                    throw bad_alloc();
vb@33
   423
                }
vb@33
   424
vb@41
   425
                clear_blob(b);
vb@33
   426
            }
vb@33
   427
vb@33
   428
            return bl;
vb@33
   429
        }
vb@33
   430
vb@33
   431
        identity_list *identities(SAFEARRAY * sa)
vb@33
   432
        {
vb@37
   433
            if (sa == NULL)
vb@37
   434
                return NULL;
vb@37
   435
vb@33
   436
            LONG lbound, ubound;
vb@33
   437
            SafeArrayGetLBound(sa, 1, &lbound);
vb@33
   438
            SafeArrayGetUBound(sa, 1, &ubound);
vb@33
   439
vb@33
   440
            size_t size = ubound - lbound + 1;
vb@33
   441
            if (size <= 0)
vb@33
   442
                return NULL;
vb@33
   443
vb@33
   444
            identity_list *il = new_identity_list(NULL);
vb@33
   445
vb@33
   446
            identity_list *_il = il;
vb@33
   447
            for (LONG i = lbound; i <= ubound; i++) {
markus@172
   448
                pEpIdentity s;
markus@172
   449
                memset(&s, 0, sizeof(pEpIdentity));
vb@33
   450
                SafeArrayGetElement(sa, &i, &s);
vb@33
   451
vb@33
   452
                pEp_identity *ident;
vb@33
   453
                try {
vb@33
   454
                    ident = new_identity(&s);
vb@33
   455
                }
vb@33
   456
                catch (bad_alloc&) {
vb@41
   457
                    clear_identity_s(s);
vb@33
   458
                    throw bad_alloc();
vb@33
   459
                }
vb@33
   460
vb@41
   461
                clear_identity_s(s);
vb@33
   462
vb@33
   463
                _il = identity_list_add(_il, ident);
vb@33
   464
                if (_il == NULL) {
vb@33
   465
                    free_identity_list(il);
vb@33
   466
                    throw bad_alloc();
vb@33
   467
                }
vb@33
   468
            }
vb@33
   469
vb@33
   470
            return il;
vb@33
   471
        }
vb@33
   472
markus@171
   473
        stringpair_t *new_stringpair(StringPair *fld)
vb@33
   474
        {
markus@269
   475
            stringpair_t *pair;
vb@212
   476
markus@269
   477
            if (!fld) {
markus@269
   478
                pair = ::new_stringpair(NULL, NULL);
markus@269
   479
            }
markus@269
   480
            else {
markus@269
   481
                pair = ::new_stringpair(str(fld->Name), str(fld->Value));
markus@269
   482
            }
vb@33
   483
            if (pair == NULL)
vb@33
   484
                throw bad_alloc();
vb@33
   485
vb@33
   486
            return pair;
vb@33
   487
        }
vb@33
   488
markus@171
   489
        void clear_opt_field(StringPair& f)
vb@41
   490
        {
markus@180
   491
            SysFreeString(f.Name);
markus@180
   492
            SysFreeString(f.Value);
markus@171
   493
            memset(&f, 0, sizeof(StringPair));
vb@41
   494
        }
vb@41
   495
vb@33
   496
        stringpair_list_t *stringpair_list(SAFEARRAY * sa)
vb@33
   497
        {
vb@37
   498
            if (sa == NULL)
vb@37
   499
                return NULL;
vb@37
   500
vb@33
   501
            LONG lbound, ubound;
vb@33
   502
            SafeArrayGetLBound(sa, 1, &lbound);
vb@33
   503
            SafeArrayGetUBound(sa, 1, &ubound);
vb@33
   504
vb@33
   505
            size_t size = ubound - lbound + 1;
vb@33
   506
            if (size <= 0)
vb@33
   507
                return NULL;
vb@33
   508
vb@33
   509
            stringpair_list_t *il = new_stringpair_list(NULL);
vb@33
   510
vb@33
   511
            stringpair_list_t *_il = il;
vb@33
   512
            for (LONG i = lbound; i <= ubound; i++) {
markus@171
   513
                StringPair s;
markus@171
   514
                memset(&s, 0, sizeof(StringPair));
vb@33
   515
                SafeArrayGetElement(sa, &i, &s);
vb@33
   516
vb@33
   517
                stringpair_t *pair;
vb@33
   518
                try {
vb@33
   519
                    pair = new_stringpair(&s);
vb@33
   520
                }
vb@33
   521
                catch (bad_alloc&) {
vb@41
   522
                    clear_opt_field(s);
vb@33
   523
                    throw bad_alloc();
vb@33
   524
                }
vb@33
   525
vb@41
   526
                clear_opt_field(s);
vb@33
   527
vb@33
   528
                _il = stringpair_list_add(_il, pair);
vb@33
   529
                if (_il == NULL) {
vb@33
   530
                    free_stringpair_list(il);
vb@33
   531
                    throw bad_alloc();
vb@33
   532
                }
vb@33
   533
            }
vb@33
   534
vb@33
   535
            return il;
vb@33
   536
        }
markus@269
   537
markus@172
   538
        ::message * text_message_to_C(TextMessage *msg)
vb@33
   539
        {
vb@33
   540
            assert(msg);
markus@269
   541
            if (!msg)
markus@269
   542
                throw invalid_argument("msg");
vb@33
   543
markus@269
   544
            ::message * msg2 = new_message((PEP_msg_direction)msg->Dir);
vb@37
   545
            if (msg2 == NULL)
vb@37
   546
                throw bad_alloc();
vb@33
   547
markus@180
   548
            msg2->id = str(msg->Id);
markus@182
   549
            msg2->shortmsg = str(msg->ShortMsg);
markus@182
   550
            msg2->longmsg = str(msg->LongMsg);
markus@182
   551
            msg2->longmsg_formatted = str(msg->LongMsgFormatted);
markus@180
   552
            msg2->attachments = bloblist(msg->Attachments);
markus@180
   553
            msg2->sent = new_timestamp(msg->Sent);
markus@180
   554
            msg2->recv = new_timestamp(msg->Recv);
markus@180
   555
            msg2->from = new_identity(&msg->From);
markus@180
   556
            msg2->to = identities(msg->To);
markus@180
   557
            msg2->recv_by = new_identity(&msg->RecvBy);
markus@180
   558
            msg2->cc = identities(msg->Cc);
markus@180
   559
            msg2->bcc = identities(msg->Bcc);
markus@180
   560
            msg2->reply_to = identities(msg->ReplyTo);
markus@180
   561
            msg2->references = new_stringlist(msg->References);
markus@180
   562
            msg2->keywords = new_stringlist(msg->Keywords);
markus@180
   563
            msg2->comments = str(msg->Comments);
markus@180
   564
            msg2->opt_fields = stringpair_list(msg->OptFields);
vb@33
   565
vb@33
   566
            return msg2;
vb@33
   567
        }
markus@177
   568
markus@269
   569
        void opt_field_array_from_C(stringpair_list_t* spair_list, LPSAFEARRAY* pair_list_out) {
markus@269
   570
            assert(spair_list);
markus@269
   571
            assert(pair_list_out);
krista@157
   572
markus@269
   573
            if (!spair_list)
markus@269
   574
                return;
markus@177
   575
markus@269
   576
            *pair_list_out = array_from_C<StringPair, stringpair_list_t>(spair_list);
markus@269
   577
        }
markus@269
   578
markus@269
   579
        void clear_opt_field_array(LPSAFEARRAY* opt_field_array) {
markus@269
   580
            if (opt_field_array) {
markus@269
   581
                SafeArrayDestroy(*opt_field_array);
markus@269
   582
                *opt_field_array = NULL;
markus@269
   583
            }
markus@269
   584
        }
vb@10
   585
    }
vb@10
   586
}