Commit 3b979f85 authored by Jan Köster's avatar Jan Köster
Browse files

test

parent ff253193
Loading
Loading
Loading
Loading
+32 −121
Original line number Diff line number Diff line
@@ -286,143 +286,54 @@ namespace confplus {
                continue;
            }

            std::string subKeyPath = currentPath;
            std::string valueName = localKeyName;
            // Every confplus path segment becomes its own registry key.
            // /zaehler2mqtt/http/addr → zaehler2mqtt\ → http\ → addr\
            std::string subKeyPath = currentPath.empty()
                ? localKeyName
                : (currentPath + "\\" + localKeyName);

            if (key->childs()) {
                if (!subKeyPath.empty()) {
                    subKeyPath += "\\";
                }
                subKeyPath += localKeyName;
                
                // Prüfe ob alle Kinder parallele Arrays sind (sequence-of-mappings).
                // Wenn ja: alle haben Elements > 1 und keine Kinder.
                bool isSeqOfMappings = true;
                size_t maxElements = 0;
                for (const Config::ConfigData* child = key->getChild(); child; child = child->next()) {
                    if (child->childs() || child->getElements() <= 1) {
                        isSeqOfMappings = false;
                        break;
                    }
                    if (child->getElements() > maxElements) {
                        maxElements = child->getElements();
                    }
                }
            HKEY hSubKey = nullptr;
            LONG lRes = RegCreateKeyExA(
                hRootKey, subKeyPath.c_str(), 0, NULL,
                REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &hSubKey, NULL);

                if (isSeqOfMappings && maxElements > 1) {
                    // Schreibe als numerische Subkeys: PARENT\0\KEY=val, PARENT\1\KEY=val
                    for (size_t idx = 0; idx < maxElements; ++idx) {
                        std::string idxSubKey = subKeyPath + "\\" + std::to_string(idx);
                        HKEY hIdxKey;
                        LONG lRes = RegCreateKeyExA(hRootKey, idxSubKey.c_str(), 0, NULL,
                                                    REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &hIdxKey, NULL);
            if (lRes != ERROR_SUCCESS) {
                            std::cerr << "Failed to create subkey: " << idxSubKey << std::endl;
                std::cerr << "Failed to create subkey: " << subKeyPath << std::endl;
                continue;
            }

                        for (const Config::ConfigData* child = key->getChild(); child; child = child->next()) {
                            if (idx >= child->getElements()) continue;
                            
                            std::string cvalue = conf->getValue(child, idx);
                            std::string childName = child->getKey();
                            
                            if (isInteger(cvalue)) {
                                try {
                                    DWORD dwValue = std::stoul(cvalue);
                                    RegSetValueExA(hIdxKey, childName.c_str(), 0, REG_DWORD,
                                                   (const BYTE*)&dwValue, sizeof(dwValue));
                                } catch (...) {
                                    RegSetValueExA(hIdxKey, childName.c_str(), 0, REG_SZ,
                                                   (const BYTE*)cvalue.c_str(), cvalue.length() + 1);
                                }
                            } else {
                                RegSetValueExA(hIdxKey, childName.c_str(), 0, REG_SZ,
                                               (const BYTE*)cvalue.c_str(), cvalue.length() + 1);
                            }
                        }
                        
                        RegCloseKey(hIdxKey);
                    }
                } else {
                    // Normale Rekursion für Kinder
                    HKEY hSubKey;
                    LONG lRes = RegCreateKeyExA(hRootKey, subKeyPath.c_str(), 0, NULL,
                                                REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &hSubKey, NULL);
                    if (lRes == ERROR_SUCCESS) {
            if (key->childs()) {
                saveKeyRecursive(hRootKey, subKeyPath, key->getChild(), conf);
                        RegCloseKey(hSubKey);
                    }
                }
            } else {
                if (!subKeyPath.empty()) {
                    subKeyPath += "\\";
                }
                subKeyPath += localKeyName;

                std::string tempPath;
                splitConfigPath(subKeyPath, tempPath, valueName);
                subKeyPath = tempPath;
            }
            
            HKEY hSubKey;
            LONG lRes = RegCreateKeyExA(hRootKey, subKeyPath.c_str(), 0, NULL, 
                                        REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &hSubKey, NULL);
            
            if (lRes == ERROR_SUCCESS) {
                
                // Leaf: store each value as positional registry value "0", "1", ...
                size_t elements = key->getElements();
                
                for (size_t pos = 0; pos < elements; ++pos) {
                    
                    std::string cvalue = conf->getValue(key, pos);
                    DWORD dwType = REG_SZ;
                    std::string currentRegValueName = valueName;
                    
                    if (elements > 1) {
                        currentRegValueName = (valueName.empty() ? "" : valueName + "_") + std::to_string(pos);
                    } else if (valueName.empty()) {
                        currentRegValueName = "";
                    }
                    std::string vname = std::to_string(pos);

                    if (isInteger(cvalue)) {
                        dwType = REG_DWORD;
                        try {
                            DWORD dwValue = std::stoul(cvalue);
                            lRes = RegSetValueExA(hSubKey, currentRegValueName.c_str(), 0, dwType, 
                            lRes = RegSetValueExA(hSubKey, vname.c_str(), 0, REG_DWORD,
                                                  (const BYTE*)&dwValue, sizeof(dwValue));
                        } catch (const std::exception&) {
                            dwType = REG_SZ;
                            lRes = RegSetValueExA(hSubKey, currentRegValueName.c_str(), 0, dwType, 
                                                  (const BYTE*)cvalue.c_str(), cvalue.length() + 1);
                        } catch (...) {
                            lRes = RegSetValueExA(hSubKey, vname.c_str(), 0, REG_SZ,
                                                  (const BYTE*)cvalue.c_str(), (DWORD)(cvalue.length() + 1));
                        }
                    } else {
                        lRes = RegSetValueExA(hSubKey, currentRegValueName.c_str(), 0, dwType, 
                                              (const BYTE*)cvalue.c_str(), cvalue.length() + 1);
                        lRes = RegSetValueExA(hSubKey, vname.c_str(), 0, REG_SZ,
                                              (const BYTE*)cvalue.c_str(), (DWORD)(cvalue.length() + 1));
                    }

                    if (lRes != ERROR_SUCCESS) {
                        std::cerr << "Failed to set registry value for: " << subKeyPath << "\\" << currentRegValueName << std::endl;
                        std::cerr << "Failed to set registry value: " << subKeyPath << "\\" << vname << std::endl;
                    }
                }

                if (!key->childs()) {
                    RegCloseKey(hSubKey);
            }
            } else {
                std::cerr << "Failed to create subkey: " << subKeyPath << std::endl;
            }
            
            if (key->childs()) { 
                std::string nextPath = subKeyPath;

                if (lRes == ERROR_SUCCESS) {
                    saveKeyRecursive(hRootKey, nextPath, key->getChild(), conf);
            RegCloseKey(hSubKey);
        }
    }
        }
    }
    
    void Registry::saveConfig(const char *path, const Config *conf){