Skip to content

Instantly share code, notes, and snippets.

@Hamayama
Last active May 1, 2020 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hamayama/15e4222f6a775fec5c0829361af74e56 to your computer and use it in GitHub Desktop.
Save Hamayama/15e4222f6a775fec5c0829361af74e56 to your computer and use it in GitHub Desktop.
Gauche でテキストファイルを加工するサンプル 2 個です。
;; -*- coding: utf-8 -*-
;;
;; indent_conv.scm
;; 2020-5-2 v1.11
;;
;; <内容>
;; Gauche を使用して、テキストファイルの行頭のインデントを変換します。
;;
;; <使い方>
;; gosh indent_conv.scm infile outfile
;; infile : 入力ファイル名
;; outfile : 出力ファイル名
;;
;; <注意点>
;; ・インデント変換のルールは、タブ1個 → 半角スペース2個 に固定です。
;; (オプション等はありません)
;;
;; ・対応するファイルの文字コードは UTF-8(BOMなし) です。
;; 出力ファイルの改行コードは LF になります。
;;
;; ・インデントのみの行は、改行のみの行に変換します。
;;
;; ・標準入出力のリダイレクトは使用できません。
;;
;; ・入力ファイル内に複数行にまたがる文字列リテラルがあって、
;; その行頭がインデントと同じようになっていた場合には、
;; インデントと判定して変換してしまいます。
;;
(use util.match)
;; タブ1個の半角スペース数
(define *space-size* 2)
;; ファイルの文字コード
(define *file-encoding* "utf-8")
;; 改行コードを CRLF にするとき用
;(define (newline :optional (port (current-output-port))) (display "\r\n" port))
;(define (print . args) (for-each display args) (newline))
(define (convert-lines)
(define space-str (make-string *space-size* #\space))
(for-each
(lambda (line)
(rxmatch-if (#/^[ \t]*$/ line) (#f)
(print "")
(print (regexp-replace-all #/^( *)\t/ line #"\\1~space-str"))))
(generator->lseq read-line)))
(define (convert-file infile outfile)
(with-input-from-file infile
(lambda ()
(with-output-to-file outfile
convert-lines
:encoding *file-encoding*))
:encoding *file-encoding*))
(define (usage out code)
(display "Usage: gosh indent_conv.scm infile outfile\n" out)
(exit code))
(define (main args)
(match args
((_ infile outfile) (convert-file infile outfile))
(_ (usage (current-error-port) 1)))
0)
;; -*- coding: utf-8 -*-
;;
;; conv_to_csv.scm
;; 2018-7-30 v1.00
;;
;; <内容>
;; Gauche を使用して、改造版のベンチマークの結果からCSVを生成します。
;;
;; <使い方>
;; gosh conv_to_csv.scm infile outfile
;; infile : 入力ファイル名 (results.Gauche 等)
;; outfile : 出力ファイル名 (all2.csv 等)
;;
(use util.match)
(use file.filter)
;; 改行コードを CRLF にするとき用
;(define (newline :optional (port (current-output-port))) (display "\r\n" port))
(define (convert-file infile outfile)
(define first-line #t)
;; 各行をチェックしてカンマ区切りの文字列を出力
(file-filter-for-each
(lambda (line out)
(rxmatch-case line
(#/^\+!CSVLINE!\+(.*)$/ (#f data1)
(if first-line
(set! first-line #f)
(newline out))
(format out "~a" data1))
(#/^GC total resized=(.*)$/ (#f data1)
(format out ",~a" data1))
(#/^GC total collected=(.*)$/ (#f data1)
(format out ",~a" data1))
(#/^GC final heap_size=(.*)$/ (#f data1)
(format out ",~a" data1))
))
:reader read-line :input infile :output outfile)
;; 最後の改行を出力
(unless first-line
(with-output-to-file outfile
(lambda () (newline))
:if-exists :append)))
(define (usage out code)
(display "Usage: gosh conv_to_csv.scm infile outfile\n" out)
(exit code))
(define (main args)
(match args
((_ infile outfile) (convert-file infile outfile))
(_ (usage (current-error-port) 1)))
0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment