[C++] 130 文字列分割ライブラリの機能追加 区切り文字の変更

[M1 Mac, Big Sur 11.6.8, FLTK 1.3.8, NO IDE]

前回記事で文字列のスペースを削除して新たなファイル名を作成しましたが、より読みやすくするため区切り文字をアンダースコアなどに変更できるようライブラリにsplitJoin2関数を追加しました。

#pragma once
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <vector>
#include <cstdio>
#include <sstream>
#include <iostream>

using std::string; using std::vector;
using std::cout; using std::endl;

class Split{
    
public:
vector<string> splits(string str, const char* del);
string splitJoin(string str, const char* del, int start, int end);
string splitJoin2(string str, const char* del1, const char* del2, int start, int end);
int chCWD(string str, const char* del, int start, int end);
};
#include "Split.h"

using std::string; using std::vector;
using std::cout; using std::endl;

// 文字列を文字列delで分割しリスト化する関数
vector<string> Split::splits(string str, const char* del) {
    int first = 0;
    int last = str.find_first_of(del);
 
    vector<string> result;
 
    while (first < str.size()) {
        string subStr(str, first, last - first);
 
        result.push_back(subStr);
 
        first = last + 1;
        last = str.find_first_of(del, first);
 
        if (last == string::npos) {
            last = str.size();
        }
    }
    return result;
}

// 分割リストのstart番目からend番目までの要素を同じ区切り文字で再結合する関数(startの最小値は0)
string Split::splitJoin(string str, const char* del, int start, int end){
    string result2;

    if (str.find(del) == std::string::npos){
        cout << "区切り文字が含まれていません" << endl;
        return str;
    }

    vector<string> list = Split::splits(str, del);

    vector<string> list2;
    for (int i = 0; i < list.size(); i++){
        if (end < 0){
            end += list.size();
        }
        if (i >= start && i <= end) {
            list2.push_back(list[i]);
        }
    }

    for (int i = 0; i< list2.size(); i++){
        result2.append(list[i] + del);
    }
    return result2;
}

// 分割リストの全要素を異なる区切り文字で再結合する関数
string Split::splitJoin2(string str, const char* del1, const char* del2){
    string result3;

    if (str.find(del1) == std::string::npos){
        cout << "区切り文字が含まれていません" << endl;
        return str;
    }

    vector<string> list = Split::splits(str, del1);

    // リストの要素を区切り文字del2で結合
    const char* delim = del2;
    std::ostringstream os;
    std::copy(list.begin(), list.end(), std::ostream_iterator<string>(os, delim));
    result3 = os.str();

    return result3;
}

// 作業ディレクトリを変更(階層を上げる)
int Split::chCWD(string str, const char* del, int start, int end){
    string new_str = Split::splitJoin(str, del, start, end);

    int rtn = chdir(new_str.c_str());
    return rtn;
}

[C++] 129 FLTK : 動画変換アプリの製作 FFmpeg

[M1 Mac, Big Sur 11.6.8, FLTK 1.3.8, NO IDE]

movファイルをmp4に変換するアプリを即席で作成しました。HomebrewからFFmpegをインストールして使用しています。

GUIは以前作成した画像変換アプリのものを流用しました。とりあえずmov→mp4変換だけの単機能です。

画面から動画をキャプチャした時のファイル名に半角スペースが含まれているためにFFmpegが動かず、前処理として半角スペースを削除したファイル名にリネームしてから変換させました。

FFmpegはコンソールツールなのでappファイルではなく実行ファイルを使うことになります。

たかだかスペースのために難易度が結構高くなりました。作っておいた文字列分割ライブラリSplitが役に立っています。

#include <cstdio> 
#include "Split.h"

Split spt;

void formatConvert(){
    string path2;

    // フォーマットリストのindex取得
    int formatNum = choice->value();

    // 変換元のファイルパス取得
    const char* path = input_line->value();
    cout << "path "<< path << endl;

    // stringへ変換
    string path_str = string(path);

    // ファイルパスに半角スペースが含まれる場合はこれを削除したファイル名に変更
    if (path_str.find(" ") != std::string::npos)
    {
        // 自製ライブラリSplitにより半角スペースを区切り文字としてリスト化
        vector<string> prefix0_vec = spt.splits(path_str, ' ');

        // リストの要素を結合(スペースなしのpath作成)
        const char* delim = "";
        std::ostringstream os;
        std::copy(prefix0_vec.begin(), prefix0_vec.end(), std::ostream_iterator<string>(os, delim));
        path2 = os.str();
        cout << "path2 "<< path2 << endl;

        // 変換元ファイルをリネーム
        rename(path, path2.c_str());
    } else {
        path2 = path_str;
    }

    // 自製ライブラリSplitによりファイル名から拡張子を削除
    string prefix = spt.splitjoin(path2, '.', 0, -2);
    cout << "prefix "<< prefix << endl;

    // 変換先のファイルパス作成
    string path3 = prefix + fmt[formatNum];
    cout << "path3 "<< path3 << endl;

    // ファイル変換コマンド作成
    string cmd = "ffmpeg -i " + path2 + " -f " + fmt[formatNum] + " " + path3;
    cout << "cmd "<< cmd << endl;

    // コマンド実行
    system(cmd.c_str());

    cout << "フォーマット変換完了!" << endl;
    output_line->insert("フォーマット変換完了!\n");
}

[C++] 128 FLTK : FileChooserの作成 命名規則 キャメルケース

[M1 Mac, Big Sur 11.6.8, FLTK 1.3.8, NO IDE]

Cancelボタンのcallback関数を追加し、命名規則をキャメルケースに統一してリネームしました。クラス名はアッパーキャメルケース、変数・関数はローワーキャメルケースにしました。アンダースコアでつなげるスネークケースよりもこちらが好みです。

まだスネークケースが混ざっているので順次直していきます。

#include <FileChooser.h>
#include <FileChooser2.h>
#include <btnAction.h>

int browserType;
char appDir[FL_PATH_MAX]; // アプリ用ディレクトリ
char selectPath[FL_PATH_MAX]; // 選択ファイルパス
vector<const char*> selectPaths; // 複数選択ファイルパス

void fileListShow(const char* dirname)
{	
	// アプリ用ディレクトリの読込&全ファイル表示
	FileBrowser->load(dirname);

	// アプリ用ディレクトリをコピー
	fl_filename_absolute(appDir, sizeof(appDir), dirname);
	cout << "appDir " << appDir << endl;

	inputFileName->value(dirname);
}

void browserTypeSet(int t) {
	cout << "int t " << t << endl;

	browserType = t;
	if (t == FileChooser::MULTI){
		FileBrowser->type(FL_MULTI_BROWSER);
	}else{
		FileBrowser->type(FL_HOLD_BROWSER);
	}

	if (t == FileChooser::CREATE){
		btnNew->activate();
	}else{
		btnNew->deactivate();
	}

	if (t == FileChooser::DIRECTORY){
		FileBrowser->filetype(Fl_File_Browser::DIRECTORIES);
	}else{
		FileBrowser->filetype(Fl_File_Browser::FILES);
	}
}

void FileBrowserCB()
{
	char *fileName;

	// クリックしたファイルのファイル名を取得
	fileName = (char *)FileBrowser->Fl_Browser::text(FileBrowser->Fl_Browser::value());
	cout << "fileName " << fileName << endl; 
	
	if (!fileName) return;

	selectPath[0] = '\0';

	// ディレクトリ名にファイル名を結合
	strcat(selectPath ,appDir);
	strcat(selectPath ,"/");
	strcat(selectPath ,fileName);

	cout << "selectPath " << selectPath << endl; 

	if (browserType == 1) { // 削除を選択してFL_MULTI_BROWSERになっている場合
		char* name = new char[strlen(selectPath) + 1];
    	strcpy(name, selectPath);
		selectPaths.push_back(name);
	}

	//ファイルパスを表示
	inputFileName->value("");
	inputFileName->value(selectPath);

}

void btnOKCB(Fl_Return_Button*, void*)
{
	// FileChooserを閉じる
	chooser->Fl_Window::hide();

}

void btnCancelCB(Fl_Button*, void*)
{
	if (browserType != 1) { 
		selectPath[0] = '\0';
	} else {
		selectPaths.clear();
	}

	// FileChooserを閉じる
	chooser->Fl_Window::hide();

}