lipsum-cpp 0.5.4
A basic library written in C++ for generating placeholder Lorem Ipsum text.
Loading...
Searching...
No Matches
source.inl
Go to the documentation of this file.
1
12#pragma once
13
14#include "internal.hpp"
15#include "sample.inl"
16
17#define LPSM_SOURCE_IPSUM(ipsum) \
18 if (lazy) \
19 { \
20 ++idx; \
21 idx %= ipsum.size(); \
22 return {ipsum.at(idx)}; \
23 } \
24 std::uniform_int_distribution<size_t> dist(0, ipsum.size() - 1); \
25 return {ipsum.at(dist(gen))};
26
27#define LPSM_SOURCE_CUSTOM_IPSUM(ipsum, name) \
28 if (currentLoaded == name) \
29 { \
30 LPSM_SOURCE_IPSUM(ipsum) \
31 }
32
33namespace lipsum
34{
35 Source::Source(const std::string& path)
36 {
37 load(path);
38 }
39
40 std::string Source::random_word(std::mt19937& gen, bool lazy)
41 {
42#ifndef LIPSUM_MIN_BUILD
43 if (m_Words.empty())
44 {
45 LPSM_SOURCE_CUSTOM_IPSUM(CAT_IPSUM, "cat")
46 LPSM_SOURCE_CUSTOM_IPSUM(DOG_IPSUM, "dog")
47 LPSM_SOURCE_CUSTOM_IPSUM(CORPO_IPSUM, "corpo")
48 LPSM_SOURCE_IPSUM(LIPSUM_VEC)
49 }
50 LPSM_SOURCE_IPSUM(m_Words)
51#else
52 std::uniform_int_distribution<size_t> dist(0, LIPSUM_VEC.size() - 1);
53 return {LIPSUM_VEC.at(dist(gen))};
54#endif
55 }
56
57 void Source::load([[maybe_unused]] const std::string& path)
58 {
59 LPSM_VERBOSE_LOG(Info, "Loading source ", path);
60 m_Words.clear();
61#ifndef LIPSUM_MIN_BUILD
62 if (path == "default" || path == "lorem")
63 {
64 currentLoaded = "default";
65 return;
66 }
67 if (path == "cat")
68 {
69 currentLoaded = "cat";
70 return;
71 }
72 if (path == "dog" || path == "doggo")
73 {
74 currentLoaded = "dog";
75 return;
76 }
77 if (path == "corpo" || path == "corporate")
78 {
79 currentLoaded = "corpo";
80 return;
81 }
82 std::ifstream file(path);
83 std::unordered_set<std::string> unique;
84 std::string word;
85 char letter;
86 if (!file.is_open())
87 {
88 internal::LogWarn(internal::LogType::Warn,
89 "lpsm::Source::load(): Could not open file ",
90 path,
91 "; falling back to standard lorem-ipsum source");
92 return;
93 }
94 while (file.get(letter))
95 {
96 if (LPSM_SAFE_CCTYPE(bool, std::isalnum, letter) || letter == '_' ||
97 letter == '-' || letter == '\'')
98 {
99 word += LPSM_SAFE_CCTYPE(char, std::tolower, letter);
100 }
101 else if (!word.empty())
102 {
103 unique.insert(word);
104 word.clear();
105 }
106 }
107 if (!word.empty())
108 {
109 unique.insert(word);
110 }
111 std::copy(unique.begin(), unique.end(), std::back_inserter(m_Words));
112#else
113 currentLoaded = "default";
114#endif
115 }
116} // namespace lipsum
Source()=default
Default constructor.
std::string currentLoaded
Definition source.hpp:87
std::string random_word(std::mt19937 &gen, bool lazy=false)
Select a random word.
Definition source.inl:40
std::vector< std::string > m_Words
The internal list of words stored.
Definition source.hpp:90
void load(const std::string &path)
Load a source and fill the vector.
Definition source.inl:57
#define LPSM_SAFE_CCTYPE(type, func, arg)
Simplify safe cctype usage.
Definition core.hpp:36
#define LPSM_VERBOSE_LOG(type,...)
Verbose log messages.
Definition core.hpp:49
Declaration of lipsum::internal.
void LogWarn(LogType type, const Args &... args)
Log a warning.
Definition internal.hpp:126
This is the main namespace of lipsum-cpp.
File containing all of the words used by lipsum::Source.