分享一些工作中常用的脚本工具~
1,Bag包的合并
2,CallBack的使用
3,#define 中声明类
4,获取当前进程PID
5,C调用shell返回结果
6,根据指定字符分割字符串
7,统计文本文件时间戳
一,rosBag的合并
代码语言:javascript复制#!/usr/bin/env python
import sys
import argparse
from fnmatch import fnmatchcase
from rosbag import Bag
def main():
parser = argparse.ArgumentParser(description='Merge one or more bag files with the possibilities of filtering topics.')
parser.add_argument('outputbag',
help='output bag file with topics merged')
parser.add_argument('inputbag', nargs=' ',
help='input bag files')
parser.add_argument('-v', '--verbose', action="store_true", default=False,
help='verbose output')
parser.add_argument('-t', '--topics', default="*",
help='string interpreted as a list of topics (wildcards '*' and '?' allowed) to include in the merged bag file')
args = parser.parse_args()
topics = args.topics.split(' ')
total_included_count = 0
total_skipped_count = 0
if (args.verbose):
print("Writing bag file: " args.outputbag)
print("Matching topics against patters: '%s'" % ' '.join(topics))
with Bag(args.outputbag, 'w') as o:
for ifile in args.inputbag:
matchedtopics = []
included_count = 0
skipped_count = 0
if (args.verbose):
print("> Reading bag file: " ifile)
with Bag(ifile, 'r') as ib:
for topic, msg, t in ib:
if any(fnmatchcase(topic, pattern) for pattern in topics):
if not topic in matchedtopics:
matchedtopics.append(topic)
if (args.verbose):
print("Including matched topic '%s'" % topic)
o.write(topic, msg, t)
included_count = 1
else:
skipped_count = 1
total_included_count = included_count
total_skipped_count = skipped_count
if (args.verbose):
print("< Included %d messages and skipped %d" % (included_count, skipped_count))
if (args.verbose):
print("Total: Included %d messages and skipped %d" % (total_included_count, total_skipped_count))
if __name__ == "__main__":
main()
二,CallBack的使用
代码语言:javascript复制using FunACallback = std::function<int(const std::string)>;
using FunBCallback = std::function<double(const int)>;
typedef struct Callbacks
{
FunACallback funacallback;
FunBCallback funbcallback;
}CALLBACKS;
Callbacks m_callbacks;
bool callbackRegister(const Callbacks& callbacks)
{
m_callbacks.funacallback = callbacks.funacallback;
m_callbacks.funbcallback = callbacks.funbcallback;
return true;
}
int FunA(const std::string str_)
{
//可以连接到其他函数接口
std::cout<<"FUNA: "<<str_<<std::endl;
return 1;
}
double FunB(const int str_)
{
std::cout<<"FUNB: "<<str_<<std::endl;
return 1.0;
}
bool Init()
{
Callbacks callbacks;
callbacks.funacallback = std::bind(&FunA,std::placeholders::_1);
callbacks.funbcallback = std::bind(&FunB,std::placeholders::_1);
callbackRegister(callbacks);
return true;
}
bool Process()
{
m_callbacks.funacallback("lililili");
m_callbacks.funbcallback(1);
}
int main()
{
Init();
Process();
}
三,#define 中声明类
代码语言:javascript复制class Base {
public:
virtual void f() {
std::cout << "this is Base";
};
};
class A : public Base {
public:
void f() {
std::cout << "this is A";
}
};
typedef std::shared_ptr<Base> (*CreatefeedFunction)();
typedef std::unordered_map<std::string, CreatefeedFunction> feedMap;
feedMap g_feed_map;
#define REGISTER_DATAFEED_CLASS(feed_class)
namespace {
std::shared_ptr<Base> Creator_##feed_class() {
return std::shared_ptr<Base>(new feed_class);
}
class __Registerer_##feed_class {
public:
__Registerer_##feed_class() {
g_feed_map[#feed_class] = &Creator_##feed_class;
}
};
__Registerer_##feed_class g_registerer_##feed_class;
} // namespace
REGISTER_DATAFEED_CLASS(A); // 函数体内部不能进行函数的定义
int main()
{
std::shared_ptr<Base> p = g_feed_map["A"]();
p->f();
return 0;
}
四,通过名字获取当前进程PID
代码语言:javascript复制pid_t getProcessPidByName(const char *proc_name)
{
FILE *fp;
char buf[100];
char cmd[200] = {'