rabbitfoot530's diary

読んだ本と、プログラムに関することのメモです。好きな言語は、C++, Python, Golang, TypeScript。数学・物理・学習理論も好きです。

2012-01-01から1年間の記事一覧

emacs24らしくパッケージ(el)をインストール

emacs24でelscreenを今まで通り使ってると「Symbol's value as variable is void: dir」っていうエラーがでて、ファイルを指定してemacsを起動できない。 ということで、emacs24らしくelscreenをインストールして使う。とりあえず、.emacsに下記を追記。デフ…

Ubuntuで多機能(ボタン)マウスの設定

Ubuntuで多機能(ボタン)マウス(M570)を使ってると、デフォルトだと左、右クリック以外の2つのボタンには、「進む」と「戻る」が設定されています。 けど、これに「コピー」「ペースト」とかを割り当てたい。 そんなときには、easystrokeを使って設定します…

pexpectでinteractは使わない

pexpectでinteract()を使用する記事を結構みるけど、そのまま使ったらクラッシュしてしまったので、interactは使えない?みたい。interactの代わりにexpect(pexpect.EOF)を使うとうまくいく。 import pexpect SERVER = "local" USER = "root" PASSWORD = "Ra…

htmlをロードする際キャッシュさせない

.load関数でhtmlをロードしようとすると、ブラウザにキャッシュが残ってて、mataにno-cacheを設定してても、何やらhtmlの更新が反映されない。。。というときは、、、 $.ajaxSetup({ cache : false }); これで更新がすぐ反映される。

Google Chromeのオプション起動

mac

/ApplicationsにAppleScript Editorで下記のコマンドを作成。 do shell script "/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --allow-file-access-from-files --user-data-dir=/Users/nao/Library/Application\\ Support/Google/DevG…

Ubuntu12.04メニューが表示されない

Ubuntu12.04のメニューバーが表示される部分のウィンドウが表示されなくて、ウィンドウが移動できないような状態の場合、unityをリセットするとなおります。 unity --reset

12.04でログインできなくったら

ubuntu 12.04でログインしようと、パスワードまでは入力するが、すぐまたパスワード入力画面に戻ってしまう場合、sshなどでログインしてから、下記のファイルを消す rm ~/.Xauthority

warning: address of local variable

c++

warning: address of local variable XXXという警告が出たら、XXXの部分の変数にstaticをつけて静的にする。 char data[100]; ...... return data; こんなコードのときにでる。なので、data変数をstaticにしてreturnしてあげる。そのままは、つけれないから…

ハードディスクの読み込み速度を計測

sudo hdparm -Tt /dev/sda1

sleep_for

c++

thread中にsleepさせるコード。 #include <iostream> #include <thread> #include <chrono> int main() { int count = 0; while (1) { std::cout << "Wait..." << count++ << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(2000)); std::cout << "waited 2000 ms" </chrono></thread></iostream>…

upgrade Ubuntu 10.04 LTS to 12.04 LTS

コマンドによるアップグレード sudo apt-get install update-manager-core do-release-upgrade -d upgradeする前に、/etc/update-manager/release-upgradesにprompt=ltsが書かれているか確認したほうがいいかも。 追記 失敗した。。。しらべたところLTS to L…

std::thread

c++

boost::threadじゃなくて、std::threadでのスレッドプログラム。 #include <thread> std::mutex m; void th_do(int i) { std::lock_guard<std::mutex> lk(m); std::cout << "do! : " << i << std::endl; } int main() { std::vector<std::thread> threads; for (int i = 0; i < 5; ++i) { thre</std::thread></std::mutex></thread>…

operatorオーバーロードでパス結合

c++

boost::filesystem::pathみたいな感じでパスの結合をやりたかったので書いてみた。 #include <iostream> #include <cstring> class Path { private: std::string _path; public: std::string str() { return _path; } const char* c_str() { return _path.c_str(); } Path& oper</cstring></iostream>…

inodeから、ファイル名を取得する

c++

inodeからファイル名を取得する関数ってないのかな? 作ってみました。 #include <iostream> #include <dirent.h> #include <cstring> const std::string getFileNameByFd(unsigned int ino, const char* path) { DIR* dp = opendir(path); struct dirent* dir; std::string d_name; while</cstring></dirent.h></iostream>…

ファイル/ディレクトリの存在確認

c++ c

ファイル/ディレクトリが存在してるかどうかの確認。 #include <sys/stat.h> int main() { struct stat st; const char* file = "exist_test.cpp"; int ret = stat(file, &st); if (0 == ret) { std::cout << "Exist!" << std::endl; } else { std::cout << "Not Exist!" </sys/stat.h>…

スペースでsplit

c++

空白区切りで文字列をスプリットする。 std::string line = "1 2 3 4"; std::stringstream ss(line); std::string str; for (int i = 0; !ss.eof(); ++i) { ss >> str; if (i & 1) { std::cout << "odd : " << str << std::endl; else { std::cout << "even …

boost::unordered_mapからc++0xのunordered_mapへの書き換え

boost::unordered_mapで書いてる部分をc++0xのunordered_mapを使って書くためには、、、 #include <unordered_map> // for C++0x #include <boost/unordered_map.hpp> // for boost boost::unordered_map<int, int> boost; std::unordered_map<int, int> cpp0x;</int,></int,></boost/unordered_map.hpp></unordered_map>

opensslをビルドする設定

MacのXodeでopensslを使ったコードをビルドするには、下記の設定が必要。 Other Linker Flagsに-lcryptoを追加 CMakeでビルドするには、下記の設定が必要 SET(LIBS "-lcrypto") target_link_libraries(target ${LIBS})

boost::mutex::scoped_lockのunlock

scoped_lockを任意の場所でunlockしたい場合は、unlockを呼ぶ。 boost::mutex::scoped_lock lock(m_mutex); lock.unlock();

unityをemacsきーばいんどにする

12.04LTSにupgradeしたら、unityに強制的になってしまったので、しぶしぶ使ってます。けど、そんなunityでも1つだけ譲れないところが、emacsキーバインド。前までのgconf-editorは使えないので、下記のコマンドにて1発設定。 gsettings get org.gnome.deskto…

strcmpと'', ""の関係

c c++

strcmpで文字列を比較するときに、文字列側には、""を使わないといけない。''だとエラーになる。 std::string str = "a"; strcmp(str, 'a'); // エラー std::string str = "a"; strcmp(str, "a"); // OK

ユークリッドの互除法

ユークリッドの互除法が読んでる本で出てきたので、軽く実装。 def euclid(a, b): numerator = max(a, b) denominator = min(a, b) if numerator == 0 or denominator == 0: return None times = numerator / denominator remainder = numerator - denominat…

interfaceのアドレスを取得

c++ c

ifconfigで表示されるIPアドレスの情報をc/c++から取得するコード #include <ifaddrs.h> struct ifaddrs* ifaddr; if (0 != getifaddrs(&ifaddr)) { return 1; } char addrstr[INET_ADDRSTRLEN]; while (ifaddr != NULL) { if (ifaddr->ifa_addr->sa_family == AF_INET)</ifaddrs.h>…

debパッケージの作り方(zabbix2.0編)

ソースしかないzabbix2.0.0.rc2からdebianパッケージを作る手順 まずは、ソースのダウンロード http://prdownloads.sourceforge.net/zabbix/zabbix-2.0.0rc2.tar.gz?download 必要なパッケージのインストール sudo aptitude install dh-make devscripts debh…

C++でJSON

C++でJSONを扱いたい便利なライブラリ。picojson.hを使用させてもらいます。 #include "picojson.h" picojson::object o; o.insert(std::make_pair("key", picojson::value(std::string("VALUE")))); std::cout << o["key"].get<std::string>() << std::endl; picojson::v</std::string>…

format指定の文字列

snprintfとかつかってたのを、お手軽にやる! #include <boost/format.hpp> boost::format fmt("%s,%s"); fmt % "Hello!" % "World!"; std::cout << fmt.str() << std::endl;</boost/format.hpp>

boost::threadから返り値を取得

boost::threadにfuncを指定して、funcからの戻り値を取得。funcには、引数を渡すために、bindする。 int return_func(std::string a, std::string b) { retrun 0; } boost::packaged_task<int> pt(boost::bind(return_func, "a", "b")); boost::unique_future<int> uf </int></int>…

key, valueのペアの格納と取り出し

key, valueをpairで格納しておいて、それを取り出すときは、tieでひとまとめにしてとりだす。 #include <boost/container/vector.hpp> #include <boost/tuple/tuple.hpp> boost::container::vector<std::string, std::string> v; std::string key, value; BOOST_FOREACH(boost::tie(key, value), v) { std::cout << key << value << std::en</std::string,></boost/tuple/tuple.hpp></boost/container/vector.hpp>…

空白削除

空白をstd::stringから削除。 std::string str = " ab "; boost::trim(str); 文字指定する場合は std::string str = " ab "; boost::trim_if(str, boost::is_any_of(" "));

sleep

wait処理をしたいとき、sleepでやってもいいけど、boostでやるなら、下記のよう。 #include <boost/thread.hpp> #include <boost/date_time/posix_time/posix_time.hpp> while (true) { boost::this_thread::sleep(boost::posix_time::milliseconds(1000); std::cout << "hi" << std::endl; }</boost/date_time/posix_time/posix_time.hpp></boost/thread.hpp>