Crossfire Server, Trunk
LicenseManager.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 2021-2022 the Crossfire Development Team
5  *
6  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
7  * welcome to redistribute it under certain conditions. For details, please
8  * see COPYING and LICENSE.
9  *
10  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
11  */
12 
13 #include "LicenseManager.h"
14 
15 #include "global.h"
16 #include "bufferreader.h"
17 #include <memory>
18 
20 }
21 
23 }
24 
26  char *c;
27  std::unique_ptr<char, void(*)(void*)> dup(strdup(filename), free);
28 
29  c = strrchr(dup.get(), '.');
30  if (!c) {
31  LOG(llevError, "LicenseManager: invalid filename %s\n", filename);
32  return;
33  }
34  (*c) = '\0';
35  c = strrchr(dup.get(), '.');
36  if (!c) {
37  LOG(llevError, "LicenseManager: invalid filename %s\n", filename);
38  return;
39  }
40  (*c) = '\0';
41  c++;
42 
43  std::string faceset(c);
44 
45  c = strrchr(dup.get(), '/');
46  if (!c) {
47  LOG(llevError, "LicenseManager: invalid filename %s\n", filename);
48  return;
49  }
50  c++;
51 
52  auto &licenses = m_licenses[c];
53  auto &items = licenses[faceset];
54  parseLicenseFile(reader, filename, items);
55 }
56 
57 void LicenseManager::parseLicenseFile(BufferReader *reader, const char *filename, std::vector<LicenseItem> &items) {
58  char *line, *c;
59  while ((line = bufferreader_next_line(reader)) != nullptr) {
60  if (line[0] == '\0')
61  continue;
62  c = strchr(line, ' ');
63  if (!c) {
64  LOG(llevError, "LicenseManager: malformed line '%s' in %s:%zu\n", line, filename, bufferreader_current_line(reader));
65  continue;
66  }
67 
68  (*c) = '\0';
69  c++;
70 
71  items.push_back(std::make_pair(line, c));
72  }
73 }
74 
76  auto search = m_licenses.find(licenseNameFromFaceName(face));
77  if (search != m_licenses.end()) {
78  return search->second;
79  }
80  return LicenseItems();
81 }
82 
83 std::string LicenseManager::licenseNameFromFaceName(const std::string &face) {
84  auto dot = face.find('.');
85  if (dot == std::string::npos)
86  {
87  return std::string();
88  }
89  return face.substr(0, dot);
90 }
global.h
bufferreader_current_line
size_t bufferreader_current_line(BufferReader *br)
Definition: bufferreader.cpp:140
llevError
@ llevError
Definition: logger.h:11
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.cpp:58
LicenseManager::m_licenses
std::map< std::string, LicenseItems > m_licenses
Definition: LicenseManager.h:71
c
static event_registration c
Definition: citylife.cpp:425
LicenseManager::LicenseItems
std::map< std::string, std::vector< LicenseItem > > LicenseItems
Definition: LicenseManager.h:29
npc_dialog.filename
filename
Definition: npc_dialog.py:99
LicenseManager.h
is_valid_types_gen.line
line
Definition: is_valid_types_gen.py:34
LicenseManager::LicenseManager
LicenseManager()
Definition: LicenseManager.cpp:19
LicenseManager::licenseNameFromFaceName
static std::string licenseNameFromFaceName(const std::string &face)
Definition: LicenseManager.cpp:83
bufferreader.h
LicenseManager::readLicense
void readLicense(BufferReader *reader, const char *filename)
Definition: LicenseManager.cpp:25
LicenseManager::~LicenseManager
virtual ~LicenseManager()
Definition: LicenseManager.cpp:22
LicenseManager::getForFace
LicenseItems getForFace(const std::string &face)
Definition: LicenseManager.cpp:75
nlohmann::detail::void
j template void())
Definition: json.hpp:4099
LicenseManager::parseLicenseFile
static void parseLicenseFile(BufferReader *reader, const char *filename, std::vector< LicenseItem > &items)
Definition: LicenseManager.cpp:57
BufferReader
Definition: bufferreader.cpp:21
bufferreader_next_line
char * bufferreader_next_line(BufferReader *br)
Definition: bufferreader.cpp:102