1 Star 2 Fork 0

Andy/stcHeader

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
convertHeader.cpp 3.48 KB
一键复制 编辑 原始数据 按行查看 历史
Andy 提交于 2020-04-28 14:07 . add source code.
/*
* =====================================================================================
*
* Filename: convertHeader.cpp
*
* Description:
*
* Version: 1.0
* Created: 04/28/2020 10:04:07
* Revision: none
* Compiler: gcc
*
* Author: Andy (), andy_y_li@163.com
* Company:
*
* =====================================================================================
*/
#include <unistd.h>
#include <fstream>
#include <iostream>
#include <string>
#include "out.h"
using namespace std;
#define _BITSET(x) ((1) << (x))
#define OPT_SET_SRC_FILE _BITSET(0)
#define MAX_PATH_LENGTH 256
int main(int argc, char* argv[]) {
int flag = 0;
int opt;
char filename[MAX_PATH_LENGTH];
char destfilename[MAX_PATH_LENGTH];
destfilename[0] = '\0';
while ((opt = getopt(argc, argv, "s:d:h")) != -1) {
switch (opt) {
case 's':
sprintf(filename, "%s", optarg);
flag |= OPT_SET_SRC_FILE;
break;
case 'd':
sprintf(destfilename, "%s", optarg);
break;
case 'h':
printf(
"version: stcHeader/1.0.0\n"
"Usage: stcHeader [-h] [ -s source_header] [ -d "
"dest_header] \n"
"-h\t\t\t: this help\n"
"-s\t\t\t: source header\n"
"-d\t\t\t: dest header(default: stdout)\n"
"\n\n");
return 0;
case '?':
printf("invalid option: %c\n", optopt);
return -1;
}
}
if (!(flag & OPT_SET_SRC_FILE)) {
fprintf(stderr, "no source header file\n");
exit(1);
}
ifstream file_in(filename);
Out outfile = Out(destfilename);
string s;
string previous_addr;
while (getline(file_in, s)) {
if (s[0] == '/' || s[0] == '#')
outfile << s << "\n";
else if (s.length() > 4) {
string::size_type type_end = s.find_first_of(' ');
string type = s.substr(0, type_end);
string::size_type name_begin = s.find_first_not_of(' ', type_end);
string::size_type name_end = s.find_first_of(' ', name_begin);
string name = s.substr(name_begin, name_end - name_begin);
string::size_type addr_begin = s.find_first_not_of(" =", name_end);
string::size_type addr_end = s.find_first_of(" ;", addr_begin);
string addr = s.substr(addr_begin, addr_end - addr_begin);
string comment;
bool haveComment = true;
if (addr_end == s.length() - 1) {
haveComment = false;
}
if (haveComment) {
string::size_type comment_begin =
s.find_first_not_of("; ", addr_end);
comment = s.substr(comment_begin);
}
if (type == "sfr") {
outfile << "SFR(" << name << ", " << addr << ");";
if (haveComment) outfile << comment;
outfile << "\n";
previous_addr = addr;
} else if (type == "sbit") {
string bit_offset = addr.substr(addr.length() - 1);
outfile << "SBIT(" << name << ", " << previous_addr << ", "
<< bit_offset << ");";
if (haveComment) outfile << comment;
outfile << "\n";
}
}
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Andy_y_li/stcHeader.git
git@gitee.com:Andy_y_li/stcHeader.git
Andy_y_li
stcHeader
stcHeader
master

搜索帮助