barus's diary

とても真面目なblogですにゃ

【VC++2019】リストファイル開く→さらに開いてヘッダコメント抜出処理。

よく使うのでメモっとく。

 

openlist.txtの中身が以下で、それぞれのファイルが存在するものとする。それぞれのファイルヘッダを抜き出す。

ーーーー

G:\plog\vs2019\fopens\fileopen\ssl\d1_lib.c
G:\plog\vs2019\fopens\fileopen\ssl\d1_msg.c
G:\plog\vs2019\fopens\fileopen\ssl\d1_srtp.
G:\plog\vs2019\fopens\fileopen\ssl\methods.c
G:\plog\vs2019\fopens\fileopen\ssl\packet.c
G:\plog\vs2019\fopens\fileopen\ssl\packet_locl.h
G:\plog\vs2019\fopens\fileopen\ssl\pqueue.c
G:\plog\vs2019\fopens\fileopen\ssl\s3_cbc.c
G:\plog\vs2019\fopens\fileopen\ssl\s3_enc.c
G:\plog\vs2019\fopens\fileopen\ssl\s3_lib.c
G:\plog\vs2019\fopens\fileopen\ssl\s3_msg.c
G:\plog\vs2019\fopens\fileopen\ssl\ssl_asn1.c
G:\plog\vs2019\fopens\fileopen\ssl\ssl_cert.c

ーーーー

f:id:hatakeka:20190420113529p:plain



 

fileopen.cpp  ・・・メイン

Myfile.cpp  ・・・定番処理

MyStr.cpp  ・・・定番処理

WHead.cpp  ・・・Workクラス、状況によって変わる。

 

  • fileopen.cpp
// fileopen.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。// fileopen.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。//
#include <iostream>#include <Windows.h>#include <vector>#include <string>#include <stdexcept>#include "ftree.h"#include "MyFile.h"#include "WHead.h"
using namespace std;
int main(int argc, char** argv){    std::cout << "Hello World!\n"; 
MyFile my; WHead whead; vector<string> l, m;
//whead.read(argv[1], &l); //return 0;
//読み込み my.read(argv[1], &l);
//test view my.show(&l);
//個別処理 cout << "-----------------" << endl;
list<LIST_WHEAD> wlist; whead.listbranch(&l,&wlist);
whead.show(&wlist);
return 0;}

 

  • Myfile.h

#pragma once#pragma once#include <windows.h>#include <stdio.h>#include <tchar.h>#include <locale.h>
#include <vector>#include <string>#include <iostream>#include <fstream>
using namespace std;
class MyFile{public: MyFile(); ~MyFile();
void read(char* filename, std::vector<string> *l);//ファイルをリストにいったん格納 void show(std::vector<string>* l);//リストの中身表示 void putfile(string buff, string path);//ファイルに書き出す int readall(string filename, string* result);};

 

  • Myfile.cpp

#include "MyFile.h"#include "MyFile.h"
MyFile::MyFile(){}

MyFile::~MyFile(){}
void MyFile::read(char* filename, std::vector<string>* l) {

std::ifstream fin(filename); std::string str; if (fin.fail()) { std::cerr << "失敗" << std::endl; return; } while (getline(fin, str)) { // std::cout << "[" << str << "]" << std::endl; l->push_back(str); } fin.close();
}
void MyFile::show(std::vector<string>* list) {
vector<string>::iterator itr = list->begin();
while (itr != list->end()) { //cout << itr->c_str(); cout << itr->c_str() << endl; itr++; }
}
void MyFile::putfile(string buff, string path) { int i = 0; ofstream ofs; ofs.open(path.c_str()); ofs << buff; ofs.close();}

int MyFile::readall(string filename, string* result){ std::ifstream ifs("test.txt"); if (ifs.fail()) { std::cerr << "失敗" << std::endl; return -1; } std::string str*1, std::istreambuf_iterator<char>()); // std::cout << "[" << str << "]" << std::endl; *result = str;
return 0;}

 

  • MyStr.h

#pragma once#pragma once
#include <string>#include <iostream>#include <fstream>#include <vector>using namespace std;


class MyStr{public: MyStr(); ~MyStr();
// ser_mae,ser_ato で囲まれた部分を取得する。 vector<string> split(const std::string& str, const char* ser_mae, const char* ser_ato);
//指定文字列1、文字列2の間の文字を抜き出す string mid(const string linebuff, string key1, string key2);
};

 

  • MyStr.cpp

#include "MyStr.h"#include "MyStr.h"
MyStr::MyStr(){}

MyStr::~MyStr(){}
//-------------------// htmlタグ// <  > で囲まれた部分を取得する。//-------------------vector<string> MyStr::split(const std::string& str, const char* ser_mae, const char* ser_ato){ vector<string> rlt;// 分割結果を格納するベクター
string search1 = ser_mae; string search2 = ser_ato;
int p0 = 0, p1 = 0, p2 = 0, len = 0; int i = 0; // p1 = str.find(search1, 0); while *2 != std::string::npos) { string dmy;
//p1 = str.find(search1, p1 + search1.length()); p2 = str.find(search2, p1 + search1.length() + 1);
if (p2 == std::string::npos || p1 == std::string::npos) {
break; } else { len = p2 - p1 - search1.length(); dmy = str.substr(p1 + search1.length(), len);
rlt.push_back(dmy); // cout << "split i=" << i << ",p1+1=" << p1 + 1 << ",len=" << len << endl; // cout << "split i=" << i << "," << dmy << endl;
} p1 = p2; i++;
}

return rlt;}

/*----------------------------------------------------------指定文字列1、文字列2の間の文字を抜き出す------------------------------------------------------------*/string MyStr::mid(const string linebuff, string key1, string key2) {
string str1, str2;
str1 = linebuff; int f1 = 0, f2 = 0; int f1_siz = 0, f2_siz = 0, len = 0;
f1 = str1.find(key1); //".dat\">"); f2 = str1.find(key2, f1); //".dat</a>", f1); f1_siz = key1.size(); f2_siz = key2.size(); len = f2 - f1 - f1_siz;
if (f1 == -1 || f2 == -1 || len < f2_siz) { } else { str2 = str1.substr(f1 + f1_siz, len); }
return str2;
}

///* strtok01.c */////#include <stdio.h>//#include <string.h>////int main()//{// char str1 = "This is a pen. That is a pen. Here is a book.", seps = " .";// char* token, * nexttoken;//// token = strtok_s(str1, seps, &nexttoken);//// while (token != NULL) {// printf("%s\n", token);// token = strtok_s(NULL, seps, &nexttoken);// }//// return 0;//}//結果表示//This//is//a//pen

 

  • WHead.h

#pragma once#pragma once

#include <string>#include <iostream>#include <fstream>#include <vector>#include <list>using namespace std;
struct LIST_WHEAD { list<string> lm; string all; string filename;};
class WHead{public:
public: WHead(); ~WHead(); void read(char* filename, std::list<LIST_WHEAD>* l); void listbranch(vector<string>* l, list<LIST_WHEAD>* wlist); void show(std::list<LIST_WHEAD>* l); bool search(string buf, string sep);
};

 

  • WHead.cpp

#include "WHead.h"#include "WHead.h"
WHead::WHead(){}

WHead::~WHead(){}
//ファイルリストからファイルを開いて処理void WHead::listbranch(vector<string>* l, list<LIST_WHEAD> *wlist) { vector<string>::iterator ite = l->begin(); while (ite != l->end()) { read*3 { std::cerr << "open error  " << filename << std::endl; return; } LIST_WHEAD strlist;
strlist.filename = filename;
int cnt = 0; string allstr; while (getline(fin, str)) { if (search(str, "/*") || search(str, "*/")) { cnt++; } if (cnt > 0) { //std::cout << cnt << " :" << str << endl; strlist.all += str; strlist.lm.push_back(str); } if (cnt > 1)break; } l->push_back(strlist);
fin.close();
}
//文字を探すbool WHead::search(string buf, string sep) { ///* 文字集合の検索 */ //s.find_first_of("=-"); // 1 ('=', '-'のいずれかが最初に出現した位置) //s.find_last_of("=-");  // 3 ('=', '-'のいずれかが最後に出現した位置) if (buf.find(sep) != std::string::npos) { //cout << "search find:" << buf << endl; return true; }
return false;//find
}
//テストviewvoid WHead::show(std::list<LIST_WHEAD>* l) {
list<LIST_WHEAD>::iterator itr = l->begin();
while (itr != l->end()) { cout << itr->filename << endl; cout << itr->all << endl; cout << "---" << endl; //list<string>::iterator itr2 = itr->lm.begin(); //while (itr2 != itr->lm.end()) { // cout << itr2->c_str() << endl; //} itr++; }}

 

 

 

 

*1:std::istreambuf_iterator<char>(ifs

*2:p1 = str.find(search1, p1

*3:char*)ite->c_str(), wlist); ite++; }
}


//個別処理void WHead::read(char* filename, std::list<LIST_WHEAD>* l) { std::ifstream fin(filename); std::string str; char strline[1000]; //cout << filename << endl; if (fin.fail(