Snap! Websites
An Open Source CMS System in C++
The snapdev project includes a set of base classes and functions that are header only (a.k.a. inline). A form of std library, if you you wish, but mostly geared toward helper functions we use all over the place.
The following are the features available at time of writing. It is not unlikely to grow every now and then.
Change a file owner and group by name.
Compute the intersection of two sets and return true if empty. The idea is that there is an algorithm to very quickly compute such a result without having to do any copy. Computing an intersection and then checking whether the set is empty would be much longer in most cases.
This helper class loads the contents of a file in a buffer (a string) and also has the ability to save that buffer back to that file or another file (a backup).
The file can be create in a directory which doesn't exist yet.
The file can be marked as temporary in which case the destruction of the file_contents object unlinks the file.
Run the glob() function and return a list of strings. This is much more C++ friendly than the basic glob() function which allocates data that you'd have to take care off. It also automatically handles errors for you.
Dealing with many hashing codes such as MD5 or SHA1 and we often need to convert them to or from strings. This files includes a couple of functions to do the conversions for us.
Since C++11, we can initialize constexpr structures at compile time. With C++14 and C++17, many features were added allowing even more code to run and make sure that your structures are valid.
This header includes basic templates that help with defining functions that help with creating structures that are constexpr (fully created at compile time) and fully verified.
This template is used to join strings with a separator and optimized as it allocates the output buffer once.
A way to lock/unlock a file with an RAII class.
Compute the log base 2 or an integer. This function uses the find first bit available on the amd64 processors and it returns a number from 0 to 63. It's equivalent to:
floor(log(n) / log(2));
Just a lot faster.
A matrix implementation. It includes color matrices for many different types of monitors and TVs and some specialization for 4x4 matrices.
An equivalent to "mkdir -p ..." in C++. It also has the ability to change the owner and group of the child folder or any created folder.
When writing a function, you may know of areas that should never be reached and if so, that's an error. This allows you to mark that part of the code as never reached:
snap::NOT_REACHED()
Then if you algorithm changes or you made a mistake to start with and that line of code is reached, it throws an error.
We have the warnings turned on in case you do not use some or all of the parameters of a function. That means you cannot compile that code by default.
If you clearly receive parameters you do not need, you can mark them as not used with this:
snap::NOT_USED(a, b, c, ...);
This way, we can clearly see that was the intend.
Transforms a string to a number at compile time.
Print an __int128 number in a standard output stream.
Output a stream to a buffer so it can be tested or used in some other way.
We use these feature a lot in our tests.
Many old C functions are very dangerous. You may know how to use strncat() (you probably don't), but that has created so many bugs, it's just not conceivable that it is still available in the C library.
So here we poison such functions. That includes sprintf() and strcpy() and variants.
We try to always include this file in all of our .cpp files from all of our projects that depend on snapdev (directly or indirectly). This way we avoid many issues.
Allow for comparing QString like strings in a case insensitive mode.
You can do the same with std::string using our libutf8 library (which also handles UTF-8 in various ways).
An RAII generic deleter, as the name says... It allows you to build smart pointers for things other than simple C++ pointers. For example, we offer the raii_fd_t type which allows you to handle a file descriptor where it automatically gets closed on destruction of your variable.
It works with std::unique_ptr<> and std::shared_ptr<>.
Reverse a string (i.e first character is swaped with the last, the second with the one before last, etc.).
Setting an environment variable is notoriously unsafe. The C library added some better functions, but it still is not that great. You have things to be watching out for such as never freeing the string buffer unless you delete the variable.
This class allows you to make it safe without having to think too hard abot it.
In many situation, you'd like to change a variable, run some code, and restore the variable to its old value. This can now be done safely.
WARNING: it is safe within one thread. If you have multiple threads handling that one variable, you probably need something much more complex.
Various functions to handle paths. This may become much less useful as the C++ library now includes many such functions.
At this time, we have a function which very quickly extracts the basename found in a path. It can remove the path, a prefix and a suffix.
To replace many strings within a string, we use this function. This will replace all instances we find and you can specify any number of needles (strings to find) and replacements (found strings get replaced by this).
For example, you can replace all of the special HTML characters with a corresponding entity in one go:
snap::string_replace_many(html, { { "<", "<" }, { ">", ">" } });
Templates used to compute Unix timestamps at compile-time.
Break strings at separators. For example, you can transform a file path in segments.
Generate a list of user group names.
Snap! Websites
An Open Source CMS System in C++