ini.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/ArgsParser.h>
  7. #include <LibCore/ConfigFile.h>
  8. #include <LibCore/File.h>
  9. #include <LibCore/System.h>
  10. #include <LibMain/Main.h>
  11. ErrorOr<int> serenity_main(Main::Arguments arguments)
  12. {
  13. TRY(Core::System::pledge("stdio rpath wpath cpath"));
  14. const char* path = nullptr;
  15. const char* group = nullptr;
  16. const char* key = nullptr;
  17. const char* value_to_write = nullptr;
  18. Core::ArgsParser args_parser;
  19. args_parser.add_positional_argument(path, "Path to INI file", "path");
  20. args_parser.add_positional_argument(group, "Group name", "group");
  21. args_parser.add_positional_argument(key, "Key name", "key");
  22. args_parser.add_positional_argument(value_to_write, "Value to write", "value", Core::ArgsParser::Required::No);
  23. args_parser.parse(arguments);
  24. if (!Core::File::exists(path)) {
  25. warnln("File does not exist: '{}'", path);
  26. return 1;
  27. }
  28. auto config = TRY(Core::ConfigFile::open(path, value_to_write ? Core::ConfigFile::AllowWriting::Yes : Core::ConfigFile::AllowWriting::No));
  29. if (value_to_write) {
  30. config->write_entry(group, key, value_to_write);
  31. TRY(config->sync());
  32. return 0;
  33. }
  34. auto value = config->read_entry(group, key);
  35. if (!value.is_empty())
  36. outln("{}", value);
  37. return 0;
  38. }