ACE3/extensions/break_line/ace_break_line.cpp
PabstMirror c426103f23 Extension changes for Medical Rewrite and Linux compilation (#6909)
* Fix Medical HandleDamageWounds memory leak (#6809)

* Use strncpy and disable MSVC unsafe warnings (#7171)
* Set 64-bit correctly from generator string, Disable SSE2 flag on 64-bit (non-existent)

* Tweaks for Linux extensions (#5762)
* Tweak CMakeLists for Linux compilation
* Conform SQF extensions check for Linux server extensions support
* Add *.so to tools
* Split extension check into Windows and Linux
* Disable Medical extension loading for now
* Add client/server separation to extension loading
* Add Arma config documentation on extension creation
2019-09-28 23:03:55 +02:00

73 lines
1.7 KiB
C++

/*
* ace_break_line.cpp
*
* Takes a string and insert as many line breaks as needed so it fits a given width
*
* Takes:
* Localized string as string
* Example: "Check weapon temperature"
*
* Returns:
* String with line breaks
*/
#include "shared.hpp"
#include <stdlib.h>
#include <sstream>
#include <vector>
#include <string>
#define MAXCHARACTERS 14
extern "C" {
EXPORT void __stdcall RVExtension(char *output, int outputSize, const char *function);
}
std::vector<std::string> splitString(const std::string & input) {
std::istringstream ss(input);
std::string token;
std::vector<std::string> output;
while (std::getline(ss, token, ' ')) {
output.push_back(token);
}
return output;
}
std::string addLineBreaks(const std::vector<std::string> &words) {
std::stringstream sstream;
size_t numChar = 0;
size_t i = 0;
while (i < words.size()) {
if (numChar == 0) {
sstream << words[i];
numChar += words[i].size();
i++;
} else {
if (numChar + 1 + words[i].size() > MAXCHARACTERS) {
sstream << "<br/>";
numChar = 0;
} else {
sstream << " " << words[i];
numChar += 1 + words[i].size();
i++;
}
}
}
return sstream.str();
}
void __stdcall RVExtension(char *output, int outputSize, const char *function) {
ZERO_OUTPUT();
if (!strcmp(function, "version")) {
strncpy(output, ACE_FULL_VERSION_STR, outputSize);
} else {
strncpy(output, addLineBreaks(splitString(function)).c_str(), outputSize);
}
EXTENSION_RETURN();
}