wlib/general.h
#pragma once
using namespace std;
#define IS_DEBUG_MODE 1
#if IS_DEBUG_MODE
#define DEBUG(text) debug(text)
#define DEBUG_EX(text,end) debug(text,end)
#else
#define DEBUG(str)
#define DEBUG_EX(text,end)
#endif
void debug(
const char * text,
const char * end = "\n"
);
void debug(
string text,
const char * end = "\n"
);
string stringf(
const char * text,
...
);
wlib/general.cpp
#include <iostream>
#include <windows.h>
//#include <stdio.h>
//#include <stdlib.h>
//#include <stdarg.h>
//#include <string>
#include <regex>
using namespace std;
void debug(
const char * text,
const char * end = "\n"
)
{
OutputDebugStringA((text + string(end)).c_str());
}
void debug(
string text,
const char * end = "\n"
)
{
debug(text.data(), end);
}
string stringf
(
const char * text,
...
)
{
va_list args_ptr;
va_start(args_ptr, text);
string text_s = (string)text;
regex reg(R"---(\{\d+\})---");
sregex_iterator begin(text_s.begin(), text_s.end(), reg);
sregex_iterator end;
string ret_text(text);
{
int offset = 0;
int i = 0;
char* arg;
for (sregex_iterator it = begin; it != end; ++it)
{
if (it->str() != string("{") + to_string(i) + string("}")) {
throw string("wrong format ({") + to_string(i) + string("})");
}
arg = va_arg(args_ptr, char*);
ret_text = ret_text.replace(it->position()+ offset, it->length(), arg);
offset += strlen(arg) - it->length();
++i;
}
}
//smatch match;
//bool matched = regex_search(text_s, match, reg); //有误
//string ret_text(text);
//cout << match.size() << endl;
//for (int i = 0; i < match.size(); i++) {
// if (match.str(i) != string("{") + to_string(i) + string("}")) {
// throw string("wrong format ({") + to_string(i) + string("})");
// }
// ret_text = ret_text.replace(match.position(i), match.length(i), va_arg(args_ptr, char*));
//}
va_end(args_ptr);
return ret_text;
}
main.cpp
#include <iostream>
#include <windows.h>
#include <stdio.h>
//#include <stdlib.h>
#include <stdarg.h>
#include "wlib/general.h"
#include <string>
int main(int argc, char *argv[])
{
for (int i = 0; i < argc; i++) {
DEBUG(stringf("命令行参数 {0} :{1} ", to_string(i).data(), argv[i]));
}
DEBUG(stringf("hello world {0} a {1} !", "cpp", "hhh"));
}
网友评论