refactor: move some useful functions into misc header

This commit is contained in:
Curve 2021-03-12 17:05:50 +01:00
parent c05715ed7d
commit eb43b0c9da
No known key found for this signature in database
GPG Key ID: 460F6C466BD35813
4 changed files with 77 additions and 44 deletions

View File

@ -1,29 +1,19 @@
#include "audio.hpp"
#include "../../core/global/globals.hpp"
#include "../../ui/ui.hpp"
#include "../misc/misc.hpp"
#include <fancy.hpp>
#include <optional>
#define MINIAUDIO_IMPLEMENTATION
#include <miniaudio.h>
#if defined(_WIN32)
#include <stringapiset.h>
std::wstring widen(const std::string &s)
{
int wsz = MultiByteToWideChar(65001, 0, s.c_str(), -1, nullptr, 0);
if (!wsz)
return std::wstring();
std::wstring out(wsz, 0);
MultiByteToWideChar(65001, 0, s.c_str(), -1, &out[0], wsz);
out.resize(wsz - 1);
return out;
}
#endif
namespace Soundux::Objects
{
#if defined(_WIN32)
using Soundux::Helpers::widen;
#endif
void Audio::setup()
{
refreshAudioDevices();

View File

@ -1,42 +1,17 @@
#if defined(__linux__)
#include "pulse.hpp"
#include "../../misc/misc.hpp"
#include <fancy.hpp>
#include <mutex>
#include <optional>
#include <regex>
#include <sstream>
#include <string>
bool exec(const std::string &command, std::string &result)
{
result.clear();
std::array<char, 128> buffer;
auto *pipe = popen(command.c_str(), "r");
if (!pipe)
{
throw std::runtime_error("popen failed");
}
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
{
result += buffer.data();
}
return pclose(pipe) == 0;
}
auto splitByNewLine(const std::string &str)
{
std::vector<std::string> result;
std::stringstream ss(str);
for (std::string line; std::getline(ss, line, '\n');)
{
result.emplace_back(line);
}
return result;
}
namespace Soundux::Objects
{
using Soundux::Helpers::exec;
using Soundux::Helpers::splitByNewLine;
bool Pulse::setModuleId(const std::string &command, std::uint32_t &id)
{
static const std::regex idRegex(R"((\d+))");

46
src/helper/misc/misc.cpp Normal file
View File

@ -0,0 +1,46 @@
#include "misc.hpp"
#include <sstream>
namespace Soundux::Helpers
{
std::vector<std::string> splitByNewLine(const std::string &str)
{
std::vector<std::string> result;
std::stringstream ss(str);
for (std::string line; std::getline(ss, line, '\n');)
{
result.emplace_back(line);
}
return result;
}
#if defined(_WIN32)
std::wstring widen(const std::string &s)
{
int wsz = MultiByteToWideChar(65001, 0, s.c_str(), -1, nullptr, 0);
if (!wsz)
return std::wstring();
std::wstring out(wsz, 0);
MultiByteToWideChar(65001, 0, s.c_str(), -1, &out[0], wsz);
out.resize(wsz - 1);
return out;
}
#endif
bool exec(const std::string &command, std::string &result)
{
result.clear();
std::array<char, 128> buffer;
auto *pipe = popen(command.c_str(), "r");
if (!pipe)
{
throw std::runtime_error("popen failed");
}
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
{
result += buffer.data();
}
return pclose(pipe) == 0;
}
} // namespace Soundux::Helpers

22
src/helper/misc/misc.hpp Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <array>
#include <string>
#include <vector>
#if defined(_WIN32)
#include <stringapiset.h>
#endif
namespace Soundux
{
namespace Helpers
{
#if defined(_WIN32)
std::wstring widen(const std::string &s);
#endif
#if defined(__linux__)
bool exec(const std::string &command, std::string &result);
#endif
std::vector<std::string> splitByNewLine(const std::string &str);
} // namespace Helpers
} // namespace Soundux