Add util module

This commit is contained in:
Rico Tiongson 2019-02-07 16:32:04 +08:00
parent 85aa6c1c0f
commit be9d711d6f
4 changed files with 194 additions and 0 deletions

View File

@ -0,0 +1,28 @@
#ifndef __COMFORTABLE_SWIPE__util_conf_filename__
#define __COMFORTABLE_SWIPE__util_conf_filename__
#include <string> // std::string
#include <unistd.h> // getenv
namespace comfortable_swipe::util
{
/**
* The path where the autostart configuration is located.
*/
const char* autostart_filename()
{
static std::string filename;
if (filename.empty()) {
const char* xdg_config = getenv("XDG_CONFIG_HOME");
std::string config(
xdg_config == NULL
? std::string(getenv("HOME")) + "/.config"
: xdg_config
);
filename = config + "/autostart/comfortable-swipe.desktop";
}
return filename.data();
}
}
#endif /* __COMFORTABLE_SWIPE__util_conf_filename__ */

View File

@ -0,0 +1,17 @@
#ifndef __COMFORTABLE_SWIPE__util_conf_filename__
#define __COMFORTABLE_SWIPE__util_conf_filename__
#include "../index.hpp"
namespace comfortable_swipe::util
{
/**
* The path where the configuration file is located.
*/
constexpr const char* conf_filename()
{
return __COMFORTABLE_SWIPE__CONFIG__;
}
}
#endif /* __COMFORTABLE_SWIPE__util_conf_filename__ */

View File

@ -0,0 +1,78 @@
#include <map> // std::map
#include <string> // std::string
#include <fstream> // std::ifstream
#include <iostream> // std::cerr, std::endl, std::getline
#include <cstdlib> // exit
#include <cctype> // std::isspace
namespace comfortable_swipe::util
{
/**
* A utility method for reading the config file.
*
* @param filename (const char*) the path of the config file.
*/
std::map<std::string, std::string> read_config_file(const char* filename)
{
std::map<std::string, std::string> conf;
std::ifstream fin(filename);
if (!fin.is_open())
{
std::cerr << "file \"" << filename << "\" does not exist!" << std::endl;
exit(1);
}
static std::string line, token[2];
int line_number = 0;
while (std::getline(fin, line))
{
++line_number;
token[0].clear();
token[1].clear();
int length = line.length();
int equal_flag = 0;
// tokenize comfig config
for (int i = 0; i < length; ++i)
{
if (line[i] == '#') // skip comments
break;
if (line[i] == '=') // flag equal sign
{
if (++equal_flag > 1)
{
std::cerr << "error in conf file " << filename << std::endl;
std::cerr << "multiple equal signs in line " << line_number << std::endl;
exit(1);
}
}
else if (!std::isspace(line[i]))
{
// add to buffer
token[equal_flag].push_back(line[i]);
}
}
// ignore empty lines
if (equal_flag == 0 && token[0].length() == 0)
continue;
// no equal sign found in non-empty line
if (equal_flag == 0)
{
std::cerr << "error in conf file: " << filename << std::endl;
std::cerr << "equal sign expected in line " << line_number << std::endl;
exit(1);
}
// equal sign found, add to tokens
if (token[1].length() > 0)
conf[token[0]] = token[1];
}
return conf;
}
}

View File

@ -0,0 +1,71 @@
#ifndef __COMFORTABLE_SWIPE__util_regex__
#define __COMFORTABLE_SWIPE__util_regex__
namespace comfortable_swipe::util
{
/**
* Regex pattern for the libinput entry for start of swipe.
* Extracts one match for the number of fingers used during the swipe.
*
* eg. event15 GESTURE_SWIPE_BEGIN +34.33s 3
* ^
* fingers
*/
const char* GESTURE_SWIPE_BEGIN_REGEX_PATTERN =
"^" // start of string
"[ -]event\\d+" // event
"\\s+GESTURE_SWIPE_BEGIN" // gesture
"\\s+\\S+" // timestamp
"\\s+(\\d+)" // fingers
"\\s*$" // end of string
;
/**
* Regex pattern for the libinput entry for the end of swipe.
* Extracts one match for the number of fingers used during the swipe.
*
* eg. event15 GESTURE_SWIPE_END +35.03s 3
* ^
* fingers
*/
const char* GESTURE_SWIPE_END_REGEX_PATTERN =
"^" // start of string
"[ -]event\\d+" // event
"\\s+GESTURE_SWIPE_END" // gesture
"\\s+\\S+" // timestamp
"\\s+(\\d+)" // fingers
"\\s*$" // end of string
;
// matches signed decimal numbers (eg. "6.02" "-1.1")
#define CF_NUMBER_REGEX "-?\\d+(?:\\.\\d+)"
// matches and extracts a space-prefixed signed fraction (eg. "-3.00/ 5.12")
#define CF_NUMBER_DIVISION "\\s*(" CF_NUMBER_REGEX ")/\\s*(" CF_NUMBER_REGEX ")"
/**
* Regex pattern for the libinput entry for during a swipe.
* Extracts number of fingers used and the speed (normal and accelerated) of the swipe.
*
* eg. event15 GESTURE_SWIPE_UPDATE +34.70s 3 -0.12/ 4.99 (-0.33/13.50 unaccelerated)
* ^ ^ ^ ^ ^
* fingers dx dy udx udy
*/
const char* GESTURE_SWIPE_UPDATE_REGEX_PATTERN =
"^" // start of string
"[ -]event\\d+" // event
"\\s+GESTURE_SWIPE_UPDATE" // gesture
"\\s+\\S+" // timestamp
"\\s+(\\d+)" // fingers
"\\s+" CF_NUMBER_DIVISION // speed (dx/dy)
"\\s+\\(" CF_NUMBER_DIVISION "\\s+unaccelerated\\)" // unaccelerated speed (udx/udy)
"\\s*$" // end of string
;
// delete macros
#undef CF_NUMBER_DIVISION
#undef CF_NUMBER_EXTRACT
#undef CF_NUMBER_REGEX
}
#endif /* __COMFORTABLE_SWIPE__util_regex__ */