File: PluginIPCUtils.cpp

package info (click to toggle)
audacity 3.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 125,252 kB
  • sloc: cpp: 358,238; ansic: 75,458; lisp: 7,761; sh: 3,410; python: 1,503; xml: 1,385; perl: 854; makefile: 122
file content (160 lines) | stat: -rw-r--r-- 3,935 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**********************************************************************

  Audacity: A Digital Audio Editor

  @file PluginIPCUtils.cpp

  @author Vitaly Sverchinsky
  
  Part of lib-module-manager library.

**********************************************************************/

#include "PluginIPCUtils.h"

#include <cstring>

#include "PluginDescriptor.h"

#include "IPCChannel.h"
#include "XMLWriter.h"

using namespace detail;

namespace {
   using HeaderBlock = size_t;
   constexpr auto HeaderBlockSize = sizeof(HeaderBlock);

   constexpr auto NodePlugin = "Plugin";
   constexpr auto NodeError = "Error";
   constexpr auto AttrErrorMessage = "msg";
}

bool detail::ParseRequestString(const wxString& req, wxString& providerId, wxString& pluginPath)
{
   auto strings = wxSplit(req, ';');
   if(strings.size() == 2)
   {
      providerId = strings[0];
      pluginPath = strings[1];
      return true;
   }
   return false;
}

wxString detail::MakeRequestString(const wxString& providerId, const wxString& pluginPath)
{
   return wxJoin(wxArrayStringEx {providerId, pluginPath}, ';');
}

void detail::PutMessage(IPCChannel& channel, const wxString& value)
{
   auto utf8 = value.ToUTF8();
   const HeaderBlock length = utf8.length();
   channel.Send(&length, HeaderBlockSize);
   if(length > 0)
      channel.Send(utf8.data(), length);
}

void InputMessageReader::ConsumeBytes(const void* bytes, size_t length)
{
   auto from = mBuffer.size();
   mBuffer.resize(from + length);
   std::memcpy(&mBuffer[from], bytes, length);
}

bool InputMessageReader::CanPop() const noexcept
{
   if(mBuffer.size() >= HeaderBlockSize)
   {
      auto expectedSize = *reinterpret_cast<const HeaderBlock*>(mBuffer.data());
      return mBuffer.size() >= expectedSize + HeaderBlockSize;
   }
   return false;
}

wxString InputMessageReader::Pop()
{
   wxString message;
   const auto length = *reinterpret_cast<const HeaderBlock*>(mBuffer.data());
   assert(mBuffer.size() >= length + HeaderBlockSize);
   if(length > 0)
      message = wxString::FromUTF8(mBuffer.data() + HeaderBlockSize, length);
   mBuffer.erase(mBuffer.begin(), mBuffer.begin() + HeaderBlockSize + length);
   return message;
}

bool PluginValidationResult::IsValid() const noexcept
{
   return !mHasError && !mDescriptors.empty();
}

bool PluginValidationResult::HasError() const noexcept
{
   return mHasError;
}

const wxString& PluginValidationResult::GetErrorMessage() const noexcept
{
   return mErrorMessage;
}

void PluginValidationResult::Add(PluginDescriptor&& desc)
{
   mDescriptors.push_back(std::move(desc));
}

void PluginValidationResult::SetError(const wxString& msg)
{
   mHasError = true;
   mErrorMessage = msg;
}

const std::vector<PluginDescriptor>& PluginValidationResult::GetDescriptors() const noexcept
{
   return mDescriptors;
}

bool PluginValidationResult::HandleXMLTag(const std::string_view& tag, const AttributesList& attrs)
{
   if(tag == NodeError)
   {
      mHasError = true;
      for(auto& p : attrs)
      {
         auto key = wxString(p.first.data(), p.first.length());
         auto& attr = p.second;
         if(key == AttrErrorMessage)
            mErrorMessage = attr.ToWString();
      }
   }
   return true;
}
void PluginValidationResult::HandleXMLEndTag(const std::string_view&) { }

XMLTagHandler* PluginValidationResult::HandleXMLChild(const std::string_view& tag)
{
   if(tag == PluginDescriptor::XMLNodeName)
   {
      mDescriptors.resize(mDescriptors.size() + 1);
      return &mDescriptors.back();
   }
   return nullptr;
}

void PluginValidationResult::WriteXML(XMLWriter& writer) const
{
   if(mHasError)
   {
      writer.StartTag(NodeError);
      writer.WriteAttr(AttrErrorMessage, mErrorMessage);
      writer.EndTag(NodeError);
   }
   if(!mDescriptors.empty())
   {
      writer.StartTag(NodePlugin);
      for(auto& desc : mDescriptors)
         desc.WriteXML(writer);
      writer.EndTag(NodePlugin);
   }
}