Skip to content

Commit

Permalink
Works under KTouch
Browse files Browse the repository at this point in the history
  • Loading branch information
richelbilderbeek committed Oct 31, 2015
1 parent d109de3 commit 3dd425d
Show file tree
Hide file tree
Showing 15 changed files with 1,193 additions and 351 deletions.
Binary file added KTouchCppLessonsCreatorLessonInKTouch1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added KTouchCppLessonsCreatorLessonInKTouch2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added KTouchCppLessonsCreatorLessonInKTouch3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# KTouchCppLessonsCreator
Create C++ lessons for KTouch

Create C++ lessons for KTouch. In C++, a lot of non-letter characters are used.
This course builds up similar to a normal course without ignoring symbols
and punctuation.

* [Download the course file](cpp.xml)

This is how a lesson looks like:

![Do the course](KTouchCppLessonsCreatorLessonInKTouch3.png)

This is how the preview of the course looks like:

![Import course](KTouchCppLessonsCreatorLessonInKTouch2.png)

This is how it looks like when the course is added to the KTouch courses:

![Adding the course](KTouchCppLessonsCreatorLessonInKTouch1.png)
1 change: 1 addition & 0 deletions courses
1,219 changes: 944 additions & 275 deletions cpp.xml

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions ktouchcpplessonscreatorcourse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

ribi::ktclc::course::course(const int rng_seed) noexcept
: m_description("KTouch lessons file created by KTouchCppLessonsCreator (using seed " + std::to_string(rng_seed) + "), www.richelbilderbeek.nl/ToolKTouchCppLessonsCreator.htm"),
m_levels(create_levels(rng_seed)),
m_lessons(create_levels(rng_seed)),
m_title("C++")
{
#ifndef NDEBUG
Expand Down Expand Up @@ -74,7 +74,7 @@ void ribi::ktclc::course::test() noexcept
const lessons a(rng_engine);
assert(!a.to_xml().empty());
}
const test_timer my_test_timer(__func__,__FILE__,1.0);
const test_timer my_test_timer(__func__,__FILE__,10.0);
{
constexpr int rng_seed = 42;
const course a(rng_seed);
Expand All @@ -92,12 +92,13 @@ std::vector<std::string> ribi::ktclc::course::to_xml() const noexcept
v.push_back(" <title>"+m_title+"</title>");
v.push_back(" <description>" + m_description + "</description>");
v.push_back(" <keyboardLayout>us</keyboardLayout>");
const std::vector<std::string> w = m_levels.to_xml();
std::transform(std::begin(w),std::end(w),std::back_inserter(v),
[](const std::string& s)
{
return std::string(" ") + s;
}
//m_lessons must supply the correct indentation. Note that
//XML text tag has an indentation level of zero
const std::vector<std::string> w = m_lessons.to_xml();
std::copy(
std::begin(w),
std::end(w),
std::back_inserter(v)
);
v.push_back("</course>");
return v;
Expand Down
3 changes: 2 additions & 1 deletion ktouchcpplessonscreatorcourse.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
namespace ribi {
namespace ktclc {

///Can create the XML of a KTouch course
struct course
{
course(const int rng_seed) noexcept;
Expand All @@ -40,7 +41,7 @@ struct course

private:
const std::string m_description;
const lessons m_levels;
const lessons m_lessons;
const std::string m_title;

static lessons create_levels(const int rng_seed) noexcept;
Expand Down
24 changes: 23 additions & 1 deletion ktouchcpplessonscreatorhelper.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ktouchcpplessonscreatorhelper.h"

#include <set>
#include <sstream>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
Expand Down Expand Up @@ -48,6 +49,21 @@ std::vector<std::string> ribi::ktclc::helper::get_version_history() noexcept
};
}

bool ribi::ktclc::helper::has_forbidden(const std::string& s) const noexcept
{
const std::set<char> forbidden = { '<', '>', '&', '/', '"', '\'' };

const auto iter = std::find_if(
std::begin(s),
std::end(s),
[forbidden](const char c)
{
return forbidden.count(c) == 1;
}
);
return iter != std::end(s);
}

#ifndef NDEBUG
void ribi::ktclc::helper::test() noexcept
{
Expand All @@ -58,7 +74,7 @@ void ribi::ktclc::helper::test() noexcept
}
const test_timer my_test_timer(__func__,__FILE__,1.0);
const helper h;
//DoesFit
//does_fit
{
assert( h.does_fit("a","a"));
assert(!h.does_fit("b","a"));
Expand All @@ -67,5 +83,11 @@ void ribi::ktclc::helper::test() noexcept
assert( h.does_fit("a","ba"));
assert( h.does_fit("b","ba"));
}
//has_forbidden
{
assert(!h.has_forbidden("static_cast"));
assert( h.has_forbidden("1 < 2"));
assert( h.has_forbidden("1 > 2"));
}
}
#endif
2 changes: 2 additions & 0 deletions ktouchcpplessonscreatorhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ struct helper
static std::string get_version() noexcept;
static std::vector<std::string> get_version_history() noexcept;

bool has_forbidden(const std::string& s) const noexcept;

template <class T>
T sort(T t) const noexcept
{
Expand Down
121 changes: 97 additions & 24 deletions ktouchcpplessonscreatorlesson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
//From http://www.richelbilderbeek.nl/ToolKTouchCppLessonsCreator.htm
//---------------------------------------------------------------------------
#include "ktouchcpplessonscreatorlesson.h"

#include <algorithm>
#include <cassert>
#include <numeric>
#include <set>
#include <sstream>

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
Expand All @@ -36,27 +39,38 @@ ribi::ktclc::lesson::lesson(
const std::string& title,
std::mt19937& rng_engine
) noexcept
: m_line{create_line(chars,rng_engine)},
: m_lines{create_lines(chars,rng_engine)},
m_new_chars{new_chars},
m_title{title}
m_title{title},
m_uuid{helper().create_uuid()}
{
#ifndef NDEBUG
test();
#endif
assert(!helper().has_forbidden(m_uuid));
assert(!helper().has_forbidden(m_title));
assert(!helper().has_forbidden(m_new_chars));
}

std::string ribi::ktclc::lesson::create_line(
const std::string& chars,
std::mt19937& rng_engine
) noexcept
{
const std::vector<std::string> v
{
const std::vector<std::string> v_raw = {
" ",
"==",
"<=",
">=",
"!=",
"^=",
"<<",
">>",
"&&",
"||",
// "//",
// "/* */",
"//",
"/*",
"*/",
"++a",
"++b",
"a!=b",
Expand Down Expand Up @@ -142,7 +156,7 @@ std::string ribi::ktclc::lesson::create_line(
"operator==",
"operator-=",
"operator--",
// "operator/=",
"operator/=",
"operator",
"operator*=",
"operator+=",
Expand Down Expand Up @@ -344,6 +358,18 @@ std::string ribi::ktclc::lesson::create_line(
"wchar_t",
"while"
};
#ifdef KTOUCH_CAN_HANDLE_BRACKETS
const std::vector<std::string> v = v_raw;
#else
std::vector<std::string> v;
std::copy_if(
std::begin(v_raw),
std::end(v_raw),
std::back_inserter(v),
[](const std::string& s) { return !helper().has_forbidden(s); }
);
#endif

//Collect all fitting words
std::vector<std::string> w;
std::copy_if(std::begin(v),std::end(v),std::back_inserter(w),
Expand All @@ -360,18 +386,17 @@ std::string ribi::ktclc::lesson::create_line(
int sum = 0;
for (int i=0; i!=sz; ++i)
{
sum += static_cast<int>(w.at(i).size());
assert(i >= 0);
assert(i < static_cast<int>(w.size()));
sum += static_cast<int>(w[i].size());
if (sum > 30)
{
w.resize(i);
break;
break;
}
}
}
//Create histogram of used chars
//const std::vector<std::pair<char,int> > histogram = Tally(w,chars);

//Add words until 60 chars is reached
//Add words until n_characters_per_line chars is reached
const int n_chars_used = std::accumulate(
std::begin(w),
std::end(w),
Expand All @@ -381,8 +406,8 @@ std::string ribi::ktclc::lesson::create_line(
return init + static_cast<int>(s.size());
}
);
// - a lesson have about n_characters_per_lesson chars
const int n_chars_extra = n_characters_per_lesson - n_chars_used;
// - a lesson have about n_characters_per_line chars
const int n_chars_extra = n_characters_per_line - n_chars_used;
// - level = number_of_chars / 2
const int level = static_cast<int>(chars.size());
for (int i=0; i!=n_chars_extra; ++i)
Expand All @@ -407,7 +432,19 @@ std::string ribi::ktclc::lesson::create_line(
);
//Remove trailing whitespace
result.resize(result.size()-1);
return result;

return result;
}

std::vector<std::string> ribi::ktclc::lesson::create_lines(
const std::string& chars,
std::mt19937& rng_engine
) noexcept
{
std::vector<std::string> v;
const int n_lines = n_lines_per_lesson;
for (int i=0; i!=n_lines; ++i) { v.push_back(create_line(chars,rng_engine)); }
return v;
}

std::string ribi::ktclc::lesson::get_version() noexcept
Expand All @@ -425,13 +462,49 @@ std::vector<std::string> ribi::ktclc::lesson::get_version_history() noexcept

std::vector<std::string> ribi::ktclc::lesson::to_xml() const noexcept
{
assert(!helper().has_forbidden(m_uuid));
assert(!helper().has_forbidden(m_title));
assert(!helper().has_forbidden(m_new_chars));

std::vector<std::string> v;
v.push_back("<lesson>");
v.push_back("<id>" + helper().create_uuid() + "</id>");
v.push_back("<title>" + m_title + "</title>");
v.push_back("<newCharacters>" + m_new_chars + "</newCharacters>");
v.push_back("<text>" + m_line + "</text>");
v.push_back("</lesson>");
v.push_back(" <lesson>");
v.push_back(" <id>" + m_uuid + "</id>");
v.push_back(" <title>" + m_title + "</title>");
v.push_back(" <newCharacters>" + m_new_chars + "</newCharacters>");
{
//There must be no spaces in the text:
//
// BAD:
// <lesson>
// <text>
// fjfj fjfj fjfj fjfj
// fjfj fjfj fjfj fjfj
// </text>
// </lesson>
//
// GOOD:
// <lesson>
// <text>fjfj fjfj fjfj fjfj
// fjfj fjfj fjfj fjfj</text>
// </lesson>
//
// If the space is in the text,
// KTouch interprets it as a long
// series of spaces that need to be typed
std::stringstream s;
s << " <text>";
std::copy(
std::begin(m_lines),
std::end(m_lines),
std::ostream_iterator<std::string>(s,"\n")
);
std::string text = s.str();
assert(!text.empty());
text.pop_back(); //Pop the trailing '\n' to direct append the </text>
text += "</text>";
v.push_back(text);
}
v.push_back(" </lesson>");
return v;
}

Expand All @@ -449,8 +522,8 @@ void ribi::ktclc::lesson::test() noexcept
constexpr int rng_seed = 42;
std::mt19937 rng_engine(rng_seed);
const lesson a(
"ab",
"cd",
"abcdefghijklmnopqrstuvwxyz",
"AB",
"test title",
rng_engine
);
Expand Down
15 changes: 14 additions & 1 deletion ktouchcpplessonscreatorlesson.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
namespace ribi {
namespace ktclc {

///Can create the XML of a KTouch lesson
struct lesson
{
///chars: character that can be assumed to be mastered
///new_chars: new character that are to be mastered in this lesson
///title: title of this lesson
///rng_engine: RNG engine
lesson(
const std::string& chars,
const std::string& new_chars,
Expand All @@ -43,16 +48,24 @@ struct lesson
std::vector<std::string> to_xml() const noexcept;

private:
const std::string m_line;
const std::vector<std::string> m_lines;
const std::string m_new_chars;
const std::string m_title;
const std::string m_uuid;

static std::string create_line(
const std::string& chars,
std::mt19937& rng_engine
) noexcept;

static std::vector<std::string> create_lines(
const std::string& chars,
std::mt19937& rng_engine
) noexcept;

constexpr static const int n_characters_per_line = 60;
constexpr static const int n_characters_per_lesson = 1000;
constexpr static const int n_lines_per_lesson = n_characters_per_lesson / n_characters_per_line;

#ifndef NDEBUG
static void test() noexcept;
Expand Down
Loading

0 comments on commit 3dd425d

Please sign in to comment.