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 *line, *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 &item = licenses[faceset];
54 
55  while ((line = bufferreader_next_line(reader)) != nullptr) {
56  if (line[0] == '\0')
57  continue;
58  c = strchr(line, ' ');
59  if (!c) {
60  LOG(llevError, "LicenseManager: malformed line '%s' in %s:%zu\n", line, filename, bufferreader_current_line(reader));
61  continue;
62  }
63 
64  (*c) = '\0';
65  c++;
66 
67  item.push_back(std::make_pair(line, c));
68  }
69 }
70 
72  auto search = m_licenses.find(licenseNameFromFaceName(face));
73  if (search != m_licenses.end()) {
74  return search->second;
75  }
76  return LicenseItems();
77 }
78 
79 std::string LicenseManager::licenseNameFromFaceName(const std::string &face) {
80  auto dot = face.find('.');
81  if (dot == std::string::npos)
82  {
83  return std::string();
84  }
85  return face.substr(0, dot);
86 }
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:51
LicenseManager::m_licenses
std::map< std::string, LicenseItems > m_licenses
Definition: LicenseManager.h:69
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:79
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:71
nlohmann::detail::void
j template void())
Definition: json.hpp:4099
item
Definition: item.py:1
BufferReader
Definition: bufferreader.cpp:21
bufferreader_next_line
char * bufferreader_next_line(BufferReader *br)
Definition: bufferreader.cpp:102