Motion Master
Loading...
Searching...
No Matches
od_cache.cc File Reference
#include "od_cache.h"
#include <cctype>
#include <string_view>
#include <system_error>
#include <utility>
#include "util.h"
Include dependency graph for od_cache.cc:

Functions

std::string sanitizeFirmwareVersion (std::string_view version)
 Reduces a firmware version to the characters a file name may hold.
std::string trimObjectString (std::string_view raw)
 Removes the padding of a string read from the object dictionary.
std::filesystem::path odCacheDirectory (const std::filesystem::path &data_dir)
 The folder of the object dictionary cache: <data-dir>/od-cache.
std::filesystem::path odCacheFilePath (const std::filesystem::path &data_dir, const OdCacheIdentity &identity)
 Returns the path of the object dictionary cache file of a device.
nlohmann::json makeOdCacheDocument (const OdCacheIdentity &identity, nlohmann::json entries)
 Builds the content of a cache file.
bool odCacheDocumentMatches (const nlohmann::json &document, const OdCacheIdentity &identity)
 Tells whether a cache file applies to a device.
OdCacheClearResult clearOdCache (const std::filesystem::path &data_dir)
 Deletes every object dictionary cache file in the data directory.

Function Documentation

◆ clearOdCache()

OdCacheClearResult clearOdCache ( const std::filesystem::path & data_dir)

Deletes every object dictionary cache file in the data directory.

Deleted are the files in <data-dir>/od-cache whose names have the layout of odCacheFilePath(), and the files of the earlier layout <data-dir>/0x<vendor>/0x<product>/0x<revision>.json. Other files are not touched. A folder that is empty afterwards is deleted as well.

Parameters
data_dirThe data directory, see –data-dir. A directory that does not exist holds no cache, and the result is empty.
Returns
The deleted files and the files that could not be deleted.

◆ makeOdCacheDocument()

nlohmann::json makeOdCacheDocument ( const OdCacheIdentity & identity,
nlohmann::json entries )

Builds the content of a cache file.

Parameters
identityWhat the dictionary was read from.
entriesThe dictionary entries, as serialized.
Returns
The document with the format version, the identity and the entries.

◆ odCacheDirectory()

std::filesystem::path odCacheDirectory ( const std::filesystem::path & data_dir)

The folder of the object dictionary cache: <data-dir>/od-cache.

◆ odCacheDocumentMatches()

bool odCacheDocumentMatches ( const nlohmann::json & document,
const OdCacheIdentity & identity )

Tells whether a cache file applies to a device.

It applies when it has the current format version and the same vendor ID, product code and full firmware version. A file in an older format, such as a bare list of entries, does not apply.

Parameters
documentThe parsed cache file.
identityThe identity of the device.
Returns
Whether the entries of the file may be used for the device.

◆ odCacheFilePath()

std::filesystem::path odCacheFilePath ( const std::filesystem::path & data_dir,
const OdCacheIdentity & identity )

Returns the path of the object dictionary cache file of a device.

The file is <data-dir>/od-cache/0x<vendor>0x<product><version>.json, with the vendor ID and the product code written as 8 lowercase hex digits and the version reduced by sanitizeFirmwareVersion(). The SII revision is not part of the name, because on Jasper firmware it does not follow the dictionary.

Parameters
data_dirThe data directory, see –data-dir.
identityThe device identity and the firmware version.
Returns
The path of the cache file. The file need not exist.

◆ sanitizeFirmwareVersion()

std::string sanitizeFirmwareVersion ( std::string_view version)

Reduces a firmware version to the characters a file name may hold.

Every character other than A-Z, a-z, 0-9, '.', '' and '-' becomes '', and the result is at most 64 characters long. Two versions can reduce to the same name, for example "v10.0.4+41" and "v10.0.4_41", so the cache file also stores the full version, see odCacheDocumentMatches().

Parameters
versionThe firmware version, as read from 0x100A.
Returns
The version as it appears in the file name.

◆ trimObjectString()

std::string trimObjectString ( std::string_view raw)

Removes the padding of a string read from the object dictionary.

A device can pad a visible string with null bytes or spaces. The string ends at the first null byte, and trailing white space is removed.

Parameters
rawThe bytes of the string as read.
Returns
The string without its padding.