ACE3/extensions/breakLine/ace_breakLine.cpp

80 lines
1.8 KiB
C++
Raw Normal View History

2015-04-12 01:22:19 +00:00
/*
* ace_breakLine.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 "ace_common.h"
#include <sstream>
#include <vector>
#include <string>
2015-04-12 02:39:06 +00:00
#define MAXCHARACTERS 14
2015-04-12 01:22:19 +00:00
static char version[] = "1.0";
extern "C" {
__declspec (dllexport) void __stdcall RVExtension(char *output, int outputSize, const char *function);
};
std::vector<std::string> splitString(std::string input) {
std::istringstream ss(input);
std::string token;
std::vector<std::string> output;
2015-04-12 02:39:06 +00:00
while (std::getline(ss, token, ' ')) {
2015-04-12 01:22:19 +00:00
output.push_back(token);
}
return output;
}
2015-04-12 02:39:06 +00:00
std::string addLineBreaks(const std::vector<std::string> &words) {
2015-04-12 01:22:19 +00:00
std::stringstream sstream;
int numChar = 0;
2015-04-12 02:39:06 +00:00
int i = 0;
while (i < words.size()) {
if (numChar == 0) {
sstream << words[i];
numChar += words[i].size();
i++;
2015-04-12 01:22:19 +00:00
} else {
2015-04-12 02:39:06 +00:00
if (numChar + 1 + words[i].size() > MAXCHARACTERS) {
sstream << "<br/>";
numChar = 0;
} else {
sstream << " " << words[i];
numChar += 1 + words[i].size();
i++;
}
2015-04-12 01:22:19 +00:00
}
}
return sstream.str();
}
// i like to live dangerously. jk, fix strncpy sometime pls.
#pragma warning( push )
#pragma warning( disable : 4996 )
void __stdcall RVExtension(char *output, int outputSize, const char *function) {
2015-04-12 02:39:06 +00:00
//strncpy(output, function, outputSize);
2015-04-12 01:22:19 +00:00
if (!strcmp(function, "version")) {
strncpy(output, version, outputSize);
} else {
2015-04-12 02:39:06 +00:00
strcpy(output, addLineBreaks(splitString(function)).c_str());
2015-04-12 01:22:19 +00:00
output[outputSize - 1] = '\0';
}
}
#pragma warning( pop )