Hatena::ブログ(Diary)

inomas’ diary... このページをアンテナに追加 RSSフィード

December - 03 , 2011

文字コード一括変換

複数のファイルを一括して、処理したいことがある。

そのときは、find + xargsを使うのが常套手段だが、

こういうときはどうするんだろうと考えてみた。

「あるフォルダにある*.c, *.h *.cppファイルのみ、Shift-JISからUTF-8に変換したい。」

これがなかなか難しい。

find . -iregex ".+\.\(c\|cpp\|h\)" | xargs -I{} perl -e "system(\"iconv -f SHIFT_JIS -t UTF-8 {} > temp; mv temp {} \");"

うーん、perlのsystem()を使うなんて、いまいちだと思う。

もっと美しいコードはないかなぁ。

iconvの代わりにPerlのEncodeを使うと、

sjis2utf8.pl

#!/usr/bin/perl
#
# Convert Shift-JIS(CR/LF) to UTF-8(LF)
#
#
use Encode;
use File::Temp qw/tempfile/;
use File::Copy qw/copy/;

my $in  = shift or die "Any files are not given.\n";
my $out = shift or die "Output file is not given.\n";

if($in eq $out){
	my $fh = File::Temp->new(TEMPLATE=>"tempfileXXXX", SUFFIX=>".dat");
	$out = $fh->filename;
	convert($in,$out);
	copy $out, $in;
	close($fh);
}
else{
	convert($in,$out);
}

sub convert{
	open(IN,"<:encoding(shiftjis)",$_[0]);
	open(OUT,">",$_[1]);
	while(my $line=<IN>){
		$line =~ s/\r//;
		print OUT encode('utf8',$line);
	}
	close(IN);
	close(OUT);
}

と書いて、

find . -iregex ".+\.\(c\|cpp\|h\)" | xargs -I{} ./sjis2utf8.pl {} {}

とした。

こちらの方が実用的。

プログラマのための文字コード技術入門 (WEB+DB PRESS plus) (WEB+DB PRESS plusシリーズ)

プログラマのための文字コード技術入門 (WEB+DB PRESS plus) (WEB+DB PRESS plusシリーズ)

December - 09 , 2010

Redmine on CentOS 5.5

CentOS 5.5上でRedmineインストールに少々手こずったのでメモ。

  1. 1 MySQLインストール

yum install mysql-server.x86_64

yum install mysql-devel.x86_64

chkconfig mysqld on

  1. 2 MySQLRedmine用のデータベース作成

mysql

grant all on redmine.* to username@localhost identified by "password"

create database redmine

/etc/my.cnfの設定

[mysql]

default_character_set=utf8

[mysqld]

default_character_set=utf8

character_set_server=utf8

skip_character_set_client_handshake

  1. 3 OpenSSLのインストール

yum install openssl-devel.x86_64

  1. 4 Rubyのインストール

yum install gcc.x86_64

tar xzf ruby-1.8.7-p302.tar.gz

cd ruby-1.8.7-p302

./configure

make

make install

mv /usr/bin/ruby /usr/bin/ruby-1.8.5

ln -s /usr/local/bin/ruby /usr/bin/ruby

  1. 5 Redmineを動かす上で必要なライブラリの構築

cd ruby-1.8.7-p302

cd ext/zlib

ruby extconf.rb

make

make install

cd ext/openssl

ruby extconf.rb

make

make install

  1. 6 RubyGemsインストール

tar xzf rubygems-1.3.7.tar.gz

cd rubygems-1.3.7

ruby setup.rb

  1. 7 Rubyその他もろもろインストール

gem install rake

gem install rack --version=1.0.1

gem install mysql

  1. 8 Redmineの構築

redmine-1.0.3/config/database.ymlの編集

rake config/initializer/session_store.rb

rake db:migrate RAILS_ENV=production

rake load_default_data RAILS_ENV=production

  1. 9 Redmineの起動

cd redmine-1.0.3

script/server -e production

  1. 10 起動の確認

http://localhost:3000

November - 07 , 2010

三頭山へ登山

西多摩郡檜原村にある、檜原都民の森から三頭山にのぼってきました。

大学時代の友人と一緒に登る予定でしたが、友人の車が途中でアクセルが効かない事態が発生。お互い携帯の連絡がつかないまま、私独りで登ることにしました。

朝8:00から駐車場が開門なのに、6:30には到着していました。

でも、あとからだと駐車場が満杯になっていたので、早めに行って正解でした。

以下、つらつらと写真並べます。

f:id:inomas:20101106064721j:image

f:id:inomas:20101106064824j:image

f:id:inomas:20101106064840j:image

f:id:inomas:20101106065310j:image

f:id:inomas:20101106065435j:image

f:id:inomas:20101106085452j:image

f:id:inomas:20101106065506j:image

f:id:inomas:20101106085223j:image

f:id:inomas:20101106085333j:image

f:id:inomas:20101106104549j:image

f:id:inomas:20101106105635j:image

f:id:inomas:20101106105925j:image

f:id:inomas:20101106110108j:image

f:id:inomas:20101106110137j:image

f:id:inomas:20101106105621j:image

f:id:inomas:20101106124155j:image

f:id:inomas:20101106124442j:image

f:id:inomas:20101106124533j:image

f:id:inomas:20101106130645j:image

f:id:inomas:20101106131248j:image

f:id:inomas:20101106134551j:image

f:id:inomas:20101106134734j:image

f:id:inomas:20101106145145j:image

November - 03 , 2010

RedHat Enterprise Linux 5.5 vsftpdでダウンロード

SELinuxを使っていると、色々と面倒で、

ファイルの受け渡しも難しい。

ただ、SELinuxを理解していないだけだ。

今回は、ただ、Linuxのvsftpdを利用して、他の端末からファイルを

ダウンロードするところまでを紹介。

まず、vsftpdのインストール

yum install vsftpd

vsftpdを自動起動

chkconfig vsftpd on

今からvsftpdを起動

service vsftpd start

で、普通にLinuxのユーザでログインしようとすると、

セキュリティの観点から拒否される。

で、anonymousユーザでログインする。パスワードは要らない。Enterを入力。

localhost:~ unixuser$ ftp 192.168.0.1

Connected to 192.168.0.1.

220 (vsFTPd 2.0.5)

Name (192.168.0.1:unixuser): anonymous

331 Please specify the password.

Password:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> ls

229 Entering Extended Passive Mode (|||10144|)

150 Here comes the directory listing.

drwxr-xr-x 2 0 0 4096 Nov 03 09:01 pub

226 Directory send OK.

デフォルトの公開ディレクトリは、/var/ftpだ。pubの下にダウンロードしたいファイルをコピーしておけばよい。

October - 17 , 2010

2010-10-16 熱海 沈船ファンダイブ

実は、熱海で潜るは初めて。

しかも、沈船を見るのも初めて。

ボートダイブは久しぶり。

天気もよくて、良かった。

ポイントからロープをつたって潜行すると、

1986年1月に沈没して真っ二つに折れてしまった、「あさひ」という沈船の

甲板にたどり着く。

インストラクターが独りタイタニックをしていましたw

f:id:inomas:20101016101335j:image

そして、ソフトコーラルが見事に色とりどりで綺麗だった。

f:id:inomas:20101016101722j:image

サクラダイも撮影。

f:id:inomas:20101016102031j:image

船の甲板には下におりる階段が残っています。

f:id:inomas:20101016102551j:image

2本目は、ソーダイ根というポイント。

f:id:inomas:20101016123227j:image

f:id:inomas:20101016123955j:image

f:id:inomas:20101016130002j:image

f:id:inomas:20101016130205j:image

f:id:inomas:20101016130249j:image

いやー楽しいですね、ダイビングは。

今度は来月、大瀬崎に行く予定です。