<commit_before>//
// PersistentStringSetting.h
//
#ifndef __PERSISTENT_STRING_SETTING_H__
#define __PERSISTENT_STRING_SETTING_H__

//==================================================================================================
// PersistentStringSetting
//==================================================================================================

template <int _cchValue>
struct PersistentStringSetting : PersistentSetting, NotifyableSetting<LPCSTR>
{
    //----------------------------------------------------------------------------------------------
    // State
    //----------------------------------------------------------------------------------------------

    String  _defaultValue;
    char    _rgchValue[_cchValue];  // always null terminated

    //----------------------------------------------------------------------------------------------
    // Construction
    //----------------------------------------------------------------------------------------------

    PersistentStringSetting() : PersistentStringSetting("")
    {
    }
    PersistentStringSetting(const String& defaultValue) : PersistentStringSetting(defaultValue.c_str())
    {
    }
    PersistentStringSetting(LPCSTR defaultValue)
        : _defaultValue(defaultValue)
    {
        zero();
        loadDefault();
        PersistentSettings::theInstance->addSetting(this);    // note: we lay out in declaration order!
    }
    void zero()
    {
        ::zero(&_rgchValue[0], _cchValue);
    }
    bool loadDefault() override
    {
        zero();
        safeStrncpy(_rgchValue, _cchValue, _defaultValue.c_str());
        INFO("PersistentStringSetting: loaded default: %s", valueAsString().c_str());
        return true;
    }
    bool load(void* pv, int cb) override
    {
        zero();
        memcpy(&_rgchValue[0], pv, min(cb, _cchValue-1));  // ALWAYS null terminate
        INFO("PersistentStringSetting: loaded: %s", valueAsString().c_str());
        return true;
    }

    //----------------------------------------------------------------------------------------------
    // Accessing
    //----------------------------------------------------------------------------------------------

    String valueAsString() override
    {
        return value();
    }
    LPCSTR value() override
    {
        return &_rgchValue[0];
    }
    void setValue(const LPCSTR& sz) override
    {
        if (strcmp(sz, value()) != 0)
        {
            String oldValue(value());
            zero();
            safeStrncpy(_rgchValue, _cchValue, sz);
            save();
            notifyChanged(oldValue.c_str());
        }
    }
    int setValueString(const String& value) override
    {
        setValue(value.c_str());
        return value.length();
    }

    int size() override
    {
        return _cchValue;
    }
    void* pointer() override
    {
        return &_rgchValue[0];
    }
    void save()
    {
        PersistentSettings::theInstance->save(this);
    }
};

#endif
<commit_msg>comment only<commit_after>//
// PersistentStringSetting.h
//
#ifndef __PERSISTENT_STRING_SETTING_H__
#define __PERSISTENT_STRING_SETTING_H__

//==================================================================================================
// PersistentStringSetting
//==================================================================================================

template <int _cchValue>
struct PersistentStringSetting : PersistentSetting, NotifyableSetting<LPCSTR>
{
    //----------------------------------------------------------------------------------------------
    // State
    //----------------------------------------------------------------------------------------------

    String  _defaultValue;
    char    _rgchValue[_cchValue];  // always null terminated, include that in your count!

    //----------------------------------------------------------------------------------------------
    // Construction
    //----------------------------------------------------------------------------------------------

    PersistentStringSetting() : PersistentStringSetting("")
    {
    }
    PersistentStringSetting(const String& defaultValue) : PersistentStringSetting(defaultValue.c_str())
    {
    }
    PersistentStringSetting(LPCSTR defaultValue)
        : _defaultValue(defaultValue)
    {
        zero();
        loadDefault();
        PersistentSettings::theInstance->addSetting(this);    // note: we lay out in declaration order!
    }
    void zero()
    {
        ::zero(&_rgchValue[0], _cchValue);
    }
    bool loadDefault() override
    {
        zero();
        safeStrncpy(_rgchValue, _cchValue, _defaultValue.c_str());
        INFO("PersistentStringSetting: loaded default: %s", valueAsString().c_str());
        return true;
    }
    bool load(void* pv, int cb) override
    {
        zero();
        memcpy(&_rgchValue[0], pv, min(cb, _cchValue-1));  // ALWAYS null terminate
        INFO("PersistentStringSetting: loaded: %s", valueAsString().c_str());
        return true;
    }

    //----------------------------------------------------------------------------------------------
    // Accessing
    //----------------------------------------------------------------------------------------------

    String valueAsString() override
    {
        return value();
    }
    LPCSTR value() override
    {
        return &_rgchValue[0];
    }
    void setValue(const LPCSTR& sz) override
    {
        if (strcmp(sz, value()) != 0)
        {
            String oldValue(value());
            zero();
            safeStrncpy(_rgchValue, _cchValue, sz);
            save();
            notifyChanged(oldValue.c_str());
        }
    }
    int setValueString(const String& value) override
    {
        setValue(value.c_str());
        return value.length();
    }

    int size() override
    {
        return _cchValue;
    }
    void* pointer() override
    {
        return &_rgchValue[0];
    }
    void save()
    {
        PersistentSettings::theInstance->save(this);
    }
};

#endif

{'fraction_non_alphanumeric': 0.34161696487740223, 'fraction_numerical': 0.0019880715705765406, 'mean_word_length': 1.3984902661899086, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5984495', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1402, 'n_words': 266}