handle empty results from libraries
[zkdns:zkdns.git] / lib-cpp / RecordProvider.cpp
1 #include "RecordProvider.h"
2
3 #include <assert.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 extern "C"
8 {
9         #include "../lib-c/recordprovider.h"
10 }
11
12 // Initialize factory as null
13 RecordProvider::Factory RecordProvider::m_factory = 0;
14
15 // *********************
16 // ***** C <=> C++ *****
17 // *********************
18
19 RecordProvider* handleToRecordProvider(void* handle)
20 {
21         assert(handle);
22         RecordProvider* recordProvider = static_cast<RecordProvider*>(handle);
23         return recordProvider;
24 }
25
26 void* rp_initialize(const char* zone)
27 {
28         assert(RecordProvider::factory());
29         return (*RecordProvider::factory())(std::string(zone));
30 }
31
32 int rp_shutdown(void* handle)
33 {
34         delete handleToRecordProvider(handle);
35 }
36
37 const char* rp_get_a_record(void* handle, const char* zone)
38 {
39         const std::string result = handleToRecordProvider(handle)->aRecord(std::string(zone));
40         if(result.empty())
41         {
42                 return 0;
43         }
44         const size_t size = result.size() + 1; // null byte
45
46         char* out = static_cast<char*>(malloc(size));
47         ::bzero(out, size);
48         ::memcpy(out, result.data(), size);
49         return out;
50 }
51
52 // **********************************************
53 // ***** RecordProvider C++ Implementation ******
54 // **********************************************
55
56 RecordProvider::Factory RecordProvider::factory()
57 {
58         return m_factory;
59 }
60
61 void RecordProvider::setFactory(Factory factory)
62 {
63         m_factory = factory;
64 }
65
66 RecordProvider::RecordProvider()
67 {
68 }
69
70 RecordProvider::~RecordProvider()
71 {
72 }