Browse Source

RULE: In C++ NEVER take c-datatypes by reference!

Because c has pointer semantics which can lead to nullptr when dereferenced to obtain a reference.
pull/15/head
heck 3 years ago
parent
commit
65b04152a1
  1. 325
      src/utils.cc
  2. 32
      src/utils.hh

325
src/utils.cc

@ -10,10 +10,10 @@ using namespace pEp;
namespace pEp { namespace pEp {
namespace Utils { namespace Utils {
std::vector<::pEp_identity*> to_cxx(const ::identity_list& idl) std::vector<::pEp_identity *> to_cxx(const ::identity_list &idl)
{ {
vector<pEp_identity*> ret{}; vector<pEp_identity *> ret{};
for (const ::identity_list* curr = &idl; curr != nullptr; curr = curr->next) { for (const ::identity_list *curr = &idl; curr != nullptr; curr = curr->next) {
if (curr->ident) { if (curr->ident) {
ret.push_back(curr->ident); ret.push_back(curr->ident);
} }
@ -21,190 +21,229 @@ namespace pEp {
return ret; return ret;
} }
string to_string(const ::pEp_identity& ident, bool full, int indent) string to_string(const ::pEp_identity * ident, bool full, int indent)
{ {
stringstream builder; stringstream builder;
if (full) { if (ident != nullptr) {
builder << endl; if (full) {
builder << std::string(indent, '\t') << "{" << endl; builder << endl;
indent++; builder << std::string(indent, '\t') << "{" << endl;
builder << std::string(indent, '\t') << "address: '" indent++;
<< (ident.address != nullptr ? ident.address : "NULL") << "'" << endl; builder << std::string(indent, '\t') << "address: '"
builder << std::string(indent, '\t') << "user_id: '" << (ident->address != nullptr ? ident->address : "NULL") << "'" << endl;
<< (ident.user_id != nullptr ? ident.user_id : "NULL") << "'" << endl; builder << std::string(indent, '\t') << "user_id: '"
builder << std::string(indent, '\t') << "username: '" << (ident->user_id != nullptr ? ident->user_id : "NULL") << "'" << endl;
<< (ident.username != nullptr ? ident.username : "NULL") << "'" << endl; builder << std::string(indent, '\t') << "username: '"
builder << std::string(indent, '\t') << "fpr: '" << (ident->username != nullptr ? ident->username : "NULL") << "'"
<< (ident.fpr != nullptr ? ident.fpr : "NULL") << "'" << endl; << endl;
builder << std::string(indent, '\t') << "comm_type: " << ident.comm_type << endl; builder << std::string(indent, '\t') << "fpr: '"
builder << std::string(indent, '\t') << "lang: '" << static_cast<string>(ident.lang) << (ident->fpr != nullptr ? ident->fpr : "NULL") << "'" << endl;
<< "'" << endl; builder << std::string(indent, '\t') << "comm_type: " << ident->comm_type << endl;
builder << std::string(indent, '\t') << "me: " << ident.me << endl; builder << std::string(indent, '\t') << "lang: '"
builder << std::string(indent, '\t') << "major_ver: " << ident.major_ver << endl; << static_cast<string>(ident->lang) << "'" << endl;
builder << std::string(indent, '\t') << "minor_ver: " << ident.minor_ver << endl; builder << std::string(indent, '\t') << "me: " << ident->me << endl;
builder << std::string(indent, '\t') << "enc_format: " << ident.enc_format << endl; builder << std::string(indent, '\t') << "major_ver: " << ident->major_ver << endl;
builder << std::string(indent, '\t') << "flags: " << ident.flags << endl; builder << std::string(indent, '\t') << "minor_ver: " << ident->minor_ver << endl;
indent--; builder << std::string(indent, '\t') << "enc_format: " << ident->enc_format
builder << std::string(indent, '\t') << "}"; << endl;
builder << std::string(indent, '\t') << "flags: " << ident->flags << endl;
indent--;
builder << std::string(indent, '\t') << "}";
} else {
builder << std::string(indent, '\t') << "{ '"
<< (ident->address != nullptr ? ident->address : "NULL") << "' / '"
<< (ident->user_id != nullptr ? ident->user_id : "NULL") << "' / '"
<< (ident->username != nullptr ? ident->username : "NULL") << "' / '"
<< (ident->fpr != nullptr ? ident->fpr : "NULL") << "' }";
}
} else { } else {
builder << std::string(indent, '\t') << "{ '" builder << "NULL";
<< (ident.address != nullptr ? ident.address : "NULL") << "' / '"
<< (ident.user_id != nullptr ? ident.user_id : "NULL") << "' / '"
<< (ident.username != nullptr ? ident.username : "NULL") << "' / '"
<< (ident.fpr != nullptr ? ident.fpr : "NULL") << "' }";
} }
return builder.str(); return builder.str();
} }
std::string to_string(const ::bloblist_t& blob, bool full, int indent) std::string to_string(const ::bloblist_t * blob, bool full, int indent)
{ {
stringstream builder; stringstream builder;
builder << endl; if (blob != nullptr) {
builder << std::string(indent, '\t') << "[" << endl; builder << endl;
indent++; builder << std::string(indent, '\t') << "[" << endl;
for (const ::bloblist_t* curr = &blob; curr != nullptr; curr = curr->next) { indent++;
if (full) { for (const ::bloblist_t *curr = blob; curr != nullptr; curr = curr->next) {
builder << std::string(indent, '\t') << "{" << endl; if (full) {
indent++; builder << std::string(indent, '\t') << "{" << endl;
builder << std::string(indent, '\t') << "mime_type: '" indent++;
<< (curr->mime_type != nullptr ? std::string(curr->mime_type) : "NULL") builder << std::string(indent, '\t') << "mime_type: '"
<< "'" << endl; << (curr->mime_type != nullptr ? std::string(curr->mime_type) : "NULL")
builder << std::string(indent, '\t') << "filename: '" << "'" << endl;
<< (curr->filename != nullptr ? curr->filename : "NULL") << "'" << endl; builder << std::string(indent, '\t') << "filename: '"
builder << std::string(indent, '\t') << "size: " << curr->size << endl; << (curr->filename != nullptr ? curr->filename : "NULL") << "'"
builder << std::string(indent, '\t') << "value: '" << endl;
<< (curr->value != nullptr ? Utils::tldr(std::string(curr->value), 300) builder << std::string(indent, '\t') << "size: " << curr->size << endl;
: "NULL") builder << std::string(indent, '\t') << "value: '"
<< "'" << endl; << (curr->value != nullptr
indent--; ? Utils::tldr(std::string(curr->value), 300)
builder << std::string(indent, '\t') << "}" << endl; : "NULL")
} else { << "'" << endl;
builder << std::string(indent, '\t'); indent--;
builder << "{ '" builder << std::string(indent, '\t') << "}" << endl;
<< (curr->mime_type != nullptr ? std::string(curr->mime_type) : "NULL"); } else {
builder << "' / '" << (curr->filename != nullptr ? curr->filename : "NULL"); builder << std::string(indent, '\t');
builder << "' / '" << curr->size << "'"; builder << "{ '"
builder << " }" << endl; << (curr->mime_type != nullptr ? std::string(curr->mime_type)
: "NULL");
builder << "' / '" << (curr->filename != nullptr ? curr->filename : "NULL");
builder << "' / '" << curr->size << "'";
builder << " }" << endl;
}
} }
indent--;
builder << std::string(indent, '\t') << "]" << endl;
} else {
builder << "NULL";
} }
indent--;
builder << std::string(indent, '\t') << "]" << endl;
return builder.str(); return builder.str();
} }
std::string to_string(const ::stringpair_list_t& spl, bool full, int indent) std::string to_string(const ::stringpair_list_t * spl, bool full, int indent)
{ {
stringstream builder; stringstream builder;
builder << endl; if (spl != nullptr) {
builder << std::string(indent, '\t') << "[" << endl; builder << endl;
indent++; builder << std::string(indent, '\t') << "[" << endl;
for (const ::stringpair_list_t* curr = &spl; curr != nullptr; curr = curr->next) { indent++;
builder << std::string(indent, '\t') << "{ '"; for (const ::stringpair_list_t *curr = spl; curr != nullptr; curr = curr->next) {
if (curr->value != nullptr) { builder << std::string(indent, '\t') << "{ '";
builder << (curr->value->key ? curr->value->key : "NULL"); if (curr->value != nullptr) {
builder << "' : '"; builder << (curr->value->key ? curr->value->key : "NULL");
builder << (curr->value->value ? curr->value->value : "NULL"); builder << "' : '";
builder << (curr->value->value ? curr->value->value : "NULL");
}
builder << "' }" << endl;
} }
builder << "' }" << endl; indent--;
builder << std::string(indent, '\t') << "]" << endl;
} else {
builder << "NULL";
} }
indent--;
builder << std::string(indent, '\t') << "]" << endl;
return builder.str(); return builder.str();
} }
std::string to_string(const ::message& msg, bool full, int indent) std::string to_string(const ::message * msg, bool full, int indent)
{ {
stringstream builder; stringstream builder;
builder << endl; if (msg != nullptr) {
builder << std::string(indent, '\t') << "{" << endl; builder << endl;
indent++; builder << std::string(indent, '\t') << "{" << endl;
builder << std::string(indent, '\t') << "from: " indent++;
<< (msg.from != nullptr ? to_string(*msg.from, full, indent) : "NULL") << endl; builder << std::string(indent, '\t') << "from: "
builder << std::string(indent, '\t') << (msg->from != nullptr ? to_string(msg->from, full, indent) : "NULL")
<< "to: " << (msg.to != nullptr ? to_string(*msg.to, full, indent) : "NULL") << endl;
<< endl; builder << std::string(indent, '\t') << "to: "
builder << std::string(indent, '\t') << "shortmsg: '" << (msg->to != nullptr ? to_string(msg->to, full, indent) : "NULL") << endl;
<< (msg.shortmsg != nullptr ? msg.shortmsg : "NULL") << "'" << endl; builder << std::string(indent, '\t') << "shortmsg: '"
builder << std::string(indent, '\t') << "longmsg: '" << (msg->shortmsg != nullptr ? msg->shortmsg : "NULL") << "'" << endl;
<< (msg.longmsg != nullptr ? msg.longmsg : "NULL") << "'" << endl; builder << std::string(indent, '\t') << "longmsg: '"
builder << std::string(indent, '\t') << "enc_format: " << msg.enc_format << endl; << (msg->longmsg != nullptr ? msg->longmsg : "NULL") << "'" << endl;
builder << std::string(indent, '\t') builder << std::string(indent, '\t') << "enc_format: " << msg->enc_format << endl;
<< "dir: " << (msg.dir == 0 ? "incomming" : "outgoing") << endl; builder << std::string(indent, '\t')
builder << std::string(indent, '\t') << "id: '" << (msg.id != nullptr ? msg.id : "NULL") << "dir: " << (msg->dir == 0 ? "incomming" : "outgoing") << endl;
<< "'" << endl; builder << std::string(indent, '\t') << "id: '"
builder << std::string(indent, '\t') << "opt_fields: " << (msg->id != nullptr ? msg->id : "NULL") << "'" << endl;
<< (msg.opt_fields ? to_string(*msg.opt_fields, full, indent) : "NULL") << endl; builder << std::string(indent, '\t') << "opt_fields: "
builder << std::string(indent, '\t') << "attachments: " << (msg->opt_fields ? to_string(msg->opt_fields, full, indent) : "NULL")
<< (msg.attachments ? to_string(*msg.attachments, full, indent) : "NULL") << endl;
<< endl; builder << std::string(indent, '\t') << "attachments: "
indent--; << (msg->attachments ? to_string(msg->attachments, full, indent) : "NULL")
builder << std::string(indent, '\t') << "}" << endl; << endl;
indent--;
builder << std::string(indent, '\t') << "}" << endl;
} else {
builder << "NULL";
}
return builder.str(); return builder.str();
} }
std::string to_string(const ::identity_list& idl, bool full, int indent) std::string to_string(const ::identity_list * idl, bool full, int indent)
{ {
stringstream builder; stringstream builder;
builder << endl; if (idl != nullptr) {
builder << std::string(indent, '\t') << "[" << endl; builder << endl;
indent++; builder << std::string(indent, '\t') << "[" << endl;
for (const ::identity_list* curr = &idl; curr != nullptr; curr = curr->next) { indent++;
builder << to_string(*curr->ident, full, indent) << endl; for (const ::identity_list *curr = idl; curr != nullptr; curr = curr->next) {
builder << to_string(curr->ident, full, indent) << endl;
}
indent--;
builder << std::string(indent, '\t') << "]";
} else {
builder << "NULL";
} }
indent--;
builder << std::string(indent, '\t') << "]";
return builder.str(); return builder.str();
} }
string to_string(const ::pEp_member& member, bool full, int indent) string to_string(const ::pEp_member * member, bool full, int indent)
{ {
stringstream builder; stringstream builder;
builder << std::string(indent, '\t') << "{" << endl; if (member != nullptr) {
indent++; builder << std::string(indent, '\t') << "{" << endl;
builder << std::string(indent, '\t') indent++;
<< "ident: " << to_string(*member.ident, full, indent) << endl; builder << std::string(indent, '\t')
builder << std::string(indent, '\t') << "joined: " << member.joined << endl; << "ident: " << to_string(member->ident, full, indent) << endl;
indent--; builder << std::string(indent, '\t') << "joined: " << member->joined << endl;
builder << std::string(indent, '\t') << "}"; indent--;
builder << std::string(indent, '\t') << "}";
} else {
builder << "NULL";
}
return builder.str(); return builder.str();
} }
string to_string(const ::member_list& mbl, bool full, int indent) string to_string(const ::member_list * mbl, bool full, int indent)
{ {
stringstream builder; stringstream builder;
builder << endl; if (mbl != nullptr) {
builder << std::string(indent, '\t') << "[" << endl; builder << endl;
indent++; builder << std::string(indent, '\t') << "[" << endl;
for (const member_list* curr = &mbl; curr != nullptr; curr = curr->next) { indent++;
builder << to_string(*curr->member, full, indent) << endl; for (const member_list *curr = mbl; curr != nullptr; curr = curr->next) {
builder << to_string(curr->member, full, indent) << endl;
}
indent--;
builder << std::string(indent, '\t') << "]";
} else {
builder << "NULL";
} }
indent--;
builder << std::string(indent, '\t') << "]";
return builder.str(); return builder.str();
} }
string to_string(const ::pEp_group& group, bool full, int indent) string to_string(const ::pEp_group * group, bool full, int indent)
{ {
stringstream builder; stringstream builder;
builder << endl; if (group != nullptr) {
builder << std::string(indent, '\t') << "{" << endl; builder << endl;
indent++; builder << std::string(indent, '\t') << "{" << endl;
builder << std::string(indent, '\t') indent++;
<< "group_identity: " << to_string(*group.group_identity, full, indent) << endl; builder << std::string(indent, '\t')
builder << std::string(indent, '\t') << "group_identity: " << to_string(group->group_identity, full, indent)
<< "manager: " << to_string(*group.manager, full, indent) << endl; << endl;
builder << std::string(indent, '\t') << "active: " << group.active << endl; builder << std::string(indent, '\t')
builder << std::string(indent, '\t') << "manager: " << to_string(group->manager, full, indent) << endl;
<< "members: " << to_string(*group.members, full, indent) << endl; builder << std::string(indent, '\t') << "active: " << group->active << endl;
indent--; builder << std::string(indent, '\t')
builder << std::string(indent, '\t') << "]"; << "members: " << to_string(group->members, full, indent) << endl;
indent--;
builder << std::string(indent, '\t') << "]";
} else {
builder << "NULL";
}
return builder.str(); return builder.str();
} }
@ -220,35 +259,35 @@ namespace pEp {
} // namespace Utils } // namespace Utils
} // namespace pEp } // namespace pEp
std::ostream& operator<<(std::ostream& o, const ::pEp_identity& pEp_c_dt) std::ostream& operator<<(std::ostream& o, const ::pEp_identity* pEp_c_dt)
{ {
return o << pEp::Utils::to_string(pEp_c_dt); return o << pEp::Utils::to_string(pEp_c_dt);
} }
std::ostream& operator<<(std::ostream& o, const ::bloblist_t& pEp_c_dt) std::ostream& operator<<(std::ostream& o, const ::bloblist_t* pEp_c_dt)
{ {
return o << pEp::Utils::to_string(pEp_c_dt); return o << pEp::Utils::to_string(pEp_c_dt);
} }
std::ostream& operator<<(std::ostream& o, const ::stringpair_list_t& pEp_c_dt) std::ostream& operator<<(std::ostream& o, const ::stringpair_list_t* pEp_c_dt)
{ {
return o << pEp::Utils::to_string(pEp_c_dt); return o << pEp::Utils::to_string(pEp_c_dt);
} }
std::ostream& operator<<(std::ostream& o, const ::message& pEp_c_dt) std::ostream& operator<<(std::ostream& o, const ::message* pEp_c_dt)
{ {
return o << pEp::Utils::to_string(pEp_c_dt); return o << pEp::Utils::to_string(pEp_c_dt);
} }
std::ostream& operator<<(std::ostream& o, const ::identity_list& pEp_c_dt) std::ostream& operator<<(std::ostream& o, const ::identity_list* pEp_c_dt)
{ {
return o << pEp::Utils::to_string(pEp_c_dt); return o << pEp::Utils::to_string(pEp_c_dt);
} }
std::ostream& operator<<(std::ostream& o, const ::pEp_member& pEp_c_dt) std::ostream& operator<<(std::ostream& o, const ::pEp_member* pEp_c_dt)
{ {
return o << pEp::Utils::to_string(pEp_c_dt); return o << pEp::Utils::to_string(pEp_c_dt);
} }
std::ostream& operator<<(std::ostream& o, const ::member_list& pEp_c_dt) std::ostream& operator<<(std::ostream& o, const ::member_list* pEp_c_dt)
{ {
return o << pEp::Utils::to_string(pEp_c_dt); return o << pEp::Utils::to_string(pEp_c_dt);
} }
std::ostream& operator<<(std::ostream& o, const ::pEp_group& pEp_c_dt) std::ostream& operator<<(std::ostream& o, const ::pEp_group* pEp_c_dt)
{ {
return o << pEp::Utils::to_string(pEp_c_dt); return o << pEp::Utils::to_string(pEp_c_dt);
} }

32
src/utils.hh

@ -18,14 +18,14 @@ namespace pEp {
std::vector<::pEp_identity*> to_cxx(const ::identity_list& idl); std::vector<::pEp_identity*> to_cxx(const ::identity_list& idl);
// pEpEngine datatypes to string // pEpEngine datatypes to string
std::string to_string(const ::pEp_identity& ident, bool full = true, int indent = 0); std::string to_string(const ::pEp_identity* ident, bool full = true, int indent = 0);
std::string to_string(const ::bloblist_t& ident, bool full = true, int indent = 0); std::string to_string(const ::bloblist_t* ident, bool full = true, int indent = 0);
std::string to_string(const ::stringpair_list_t& spl, bool full = true, int indent = 0); std::string to_string(const ::stringpair_list_t* spl, bool full = true, int indent = 0);
std::string to_string(const ::message& msg, bool full = true, int indent = 0); std::string to_string(const ::message* msg, bool full = true, int indent = 0);
std::string to_string(const ::identity_list& idl, bool full = true, int indent = 0); std::string to_string(const ::identity_list* idl, bool full = true, int indent = 0);
std::string to_string(const ::pEp_member& member, bool full = true, int indent = 0); std::string to_string(const ::pEp_member* member, bool full = true, int indent = 0);
std::string to_string(const ::member_list& mbl, bool full = true, int indent = 0); std::string to_string(const ::member_list* mbl, bool full = true, int indent = 0);
std::string to_string(const ::pEp_group& group, bool full = true, int indent = 0); std::string to_string(const ::pEp_group* group, bool full = true, int indent = 0);
// Misc // Misc
std::string readKey(); // TODO: Move to std_utils std::string readKey(); // TODO: Move to std_utils
@ -34,13 +34,13 @@ namespace pEp {
} // namespace pEp } // namespace pEp
// ostream inserters // ostream inserters
std::ostream& operator<<(std::ostream& o, const ::pEp_identity& pEp_c_dt); std::ostream& operator<<(std::ostream& o, const ::pEp_identity* pEp_c_dt);
std::ostream& operator<<(std::ostream& o, const ::bloblist_t& pEp_c_dt); std::ostream& operator<<(std::ostream& o, const ::bloblist_t* pEp_c_dt);
std::ostream& operator<<(std::ostream& o, const ::stringpair_list_t& pEp_c_dt); std::ostream& operator<<(std::ostream& o, const ::stringpair_list_t* pEp_c_dt);
std::ostream& operator<<(std::ostream& o, const ::message& pEp_c_dt); std::ostream& operator<<(std::ostream& o, const ::message* pEp_c_dt);
std::ostream& operator<<(std::ostream& o, const ::identity_list& pEp_c_dt); std::ostream& operator<<(std::ostream& o, const ::identity_list* pEp_c_dt);
std::ostream& operator<<(std::ostream& o, const ::pEp_member& pEp_c_dt); std::ostream& operator<<(std::ostream& o, const ::pEp_member* pEp_c_dt);
std::ostream& operator<<(std::ostream& o, const ::member_list& pEp_c_dt); std::ostream& operator<<(std::ostream& o, const ::member_list* pEp_c_dt);
std::ostream& operator<<(std::ostream& o, const ::pEp_group& pEp_c_dt); std::ostream& operator<<(std::ostream& o, const ::pEp_group* pEp_c_dt);
#endif // LIBPEPADAPTER_UTILS_HH #endif // LIBPEPADAPTER_UTILS_HH
Loading…
Cancel
Save