はじめに
ソースコードは一定の書式(コーディングルール)に従い書くのが一般的です。
ここでは、個人的なプラグラムコードのテンプレートを記します。
C言語
ヘッダファイルのテンプレート
/*******************************************************************************
* Name :
* System :
* type :
* Function :
* References :
* Remarks :
* Version :
* History :
* Ver.-----Date--------Name------------Comment---------------------------------
* 1.00 2007/xx/xx xxxxxx 新規作成
******************************************************************************/
#ifndef __H_
#define __H_
#if defined(__cplusplus)
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif
ヘッダファイルの例
/*******************************************************************************
* Name : test.h
* System : テストシステム
* type : モジュール
* Function : テストを行います。
* References :
* Remarks :
* Version : 1.00
* History :
* Ver.-----Date--------Name------------Comment---------------------------------
* 1.00 2007/xx/xx xxxxxx 新規作成
******************************************************************************/
#ifndef _TEST_H_
#define _TEST_H_
#if defined(__cplusplus)
extern "C" {
#endif
//******************************************************************************
// 外部関数宣言
//******************************************************************************
/* 初期化 */
void TSTInit(void);
/* テスト実行 */
void TSTRun(void);
/* テスト結果取得 */
int TSTGetResult(void);
/* 終了処理 */
void TSTExit(void);
#ifdef __cplusplus
}
#endif
#endif
ソースファイル
ファイルのヘッダに記述
/*******************************************************************************
* Name :
* System :
* type :
* Function :
* References :
* Remarks :
* Version :
* History :
* Ver.-----Date--------Name------------Comment---------------------------------
* 1.00 2007/xx/xx xxxxxx 新規作成
******************************************************************************/
定数定義、関数定義等定義の区切り部分に記載
/*******************************************************************************
*
******************************************************************************/
関数の説明に記載
/*******************************************************************************
*
*
*
******************************************************************************/
ソースファイルの例
/*******************************************************************************
* Name : test.c
* System : テストシステム
* type : モジュール
* Function : テストを行います。
* References :
* Remarks :
* Version : 1.00
* History :
* Ver.-----Date--------Name------------Comment---------------------------------
* 1.00 2007/xx/xx xxxxxx 新規作成
******************************************************************************/
#include "test.h"
/*******************************************************************************
* define
******************************************************************************/
#define TEST_1
#define TEST_2
/*******************************************************************************
* 内部変数定義
******************************************************************************/
static int g_test_1;
static int g_test_2;
/*******************************************************************************
* 内部関数宣言
******************************************************************************/
/* テスト1を実行 */
static int _TSTRun1();
/* テスト2を実行 */
static int _TSTRun2();
/*******************************************************************************
* 内部関数定義
******************************************************************************/
/*******************************************************************************
*
* テスト1を実行
*
******************************************************************************/
static int _TSTRun1()
{
return 1;
}
/*******************************************************************************
*
* テスト2を実行
*
******************************************************************************/
static int _TSTRun2()
{
return 0;
}
/*******************************************************************************
* 外部関数定義
******************************************************************************/
/*******************************************************************************
*
* 初期化する
*
******************************************************************************/
void TSTInit(void)
{
return 0;
}
/*******************************************************************************
*
* テスト実行
* この関数を呼ぶ前に必ずTSTInit()を呼ぶこと
* テスト仕様書をP12を参照
*
******************************************************************************/
void TSTRun(void)
{
return 0;
}
C++言語
ヘッダファイルのテンプレート
//******************************************************************************
// Name :
// System :
// type :
// Function :
// References :
// Remarks :
// Version : 1.00
// History :
// Ver.-----Date--------Name------------Comment---------------------------------
// 1.00 2007/xx/xx xxxxxx 新規作成
//******************************************************************************
#ifndef __H_
#define __H_
#endif
ヘッダファイルの例
//******************************************************************************
// Name : test.h
// System : テストシステム
// type : モジュール
// Function : テストを行います。
// References :
// Remarks :
// Version : 1.00
// History :
// Ver.-----Date--------Name------------Comment---------------------------------
// 1.00 2007/xx/xx xxxxxx 新規作成
//*****************************************************************************/
#ifndef _TEST_H_
#define _TEST_H_
//******************************************************************************
// クラス定義
//******************************************************************************
//******************************************************************************
//
// テストクラス
//
//******************************************************************************
class CTest
{
private:
int tst1;
int tst2;
public:
// コンストラクタ
CTest();
// デストラクタ
~CTest();
// 実行
void Run();
};
#endif
ソースファイル
ファイルのヘッダに記述
//******************************************************************************
// Name :
// System :
// type :
// Function :
// References :
// Remarks :
// Version :
// History :
// Ver.-----Date--------Name------------Comment---------------------------------
// 1.00 2007/xx/xx xxxxxx 新規作成
//******************************************************************************
定数定義、関数定義等定義の区切り部分に記載
//******************************************************************************
//
//******************************************************************************
関数の説明に記載
//******************************************************************************
//
//
//
//******************************************************************************
ソースファイルの例
//******************************************************************************
// Name : test.cpp
// System : テストシステム
// type : モジュール
// Function : テストを行います。
// References :
// Remarks :
// Version : 1.00
// History :
// Ver.-----Date--------Name------------Comment---------------------------------
// 1.00 2007/xx/xx xxxxxx 新規作成
//*****************************************************************************/
#include "test.h"
//******************************************************************************
// define
//*****************************************************************************/
// テスト1を実行する場合に定義
#define TEST1 1
//******************************************************************************
// クラス定義
//*****************************************************************************/
//******************************************************************************
//
// コンストラクタ
//
//******************************************************************************
CTest::CTest()
{
}
//******************************************************************************
//
// デストラクタ
//
//******************************************************************************
CTest::~CTest()
{
}
//******************************************************************************
//
// 実行
//
//******************************************************************************
void CTest::Run()
{
return 0;
}
Python
ファイルのテンプレート
# coding: utf-8
"""
system :
type : module/packeage
function :
auther : x.xxxxxx
history : rev---+date----------+name--------+comment--------------------------
1.00 2007/xx/xx x.xxxxxx 初版作成
"""
#------------------------------------------------------------------------------
# module
#------------------------------------------------------------------------------
__version__ = "$Revision: 1.00 $".split()[-2:][0]
__all__ = {}
#------------------------------------------------------------------------------
# import
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# クラス定義
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# 内部関数定義
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# 外部関数定義
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# テスト関数定義
#------------------------------------------------------------------------------
def __test():
pass
#------------------------------------------------------------------------------
# main
#------------------------------------------------------------------------------
if __name__ == "__main__":
__test()
ファイル例
# coding: utf-8
"""
system : none
type : module
function : パーサテスト
auther : x.xxxxxx
history : rev---+date----------+name--------+comment--------------------------
1.00 2006/11/xx x.xxxxxx 初版作成
"""
#------------------------------------------------------------------------------
# module
#------------------------------------------------------------------------------
__version__ = "$Revision: 1.00 $".split()[-2:][0]
__all__ = {}
#------------------------------------------------------------------------------
# import
#------------------------------------------------------------------------------
import webtool
from HTMLParser import HTMLParser
from sgmllib import SGMLParser
#------------------------------------------------------------------------------
# テスト用クラス定義
#------------------------------------------------------------------------------
class TestHtmlParser(HTMLParser):
"""
HTMLParserテスト用
"""
def handle_starttag(self, tag, attrs):
print "[starttag]", tag, attrs
def handle_startendtag(self, tag, attrs) :
print "[startendtag]", tag, attrs
def handle_endtag(self, tag):
print "[endtag]", tag
def handle_data(self, data):
print "[data]", data
def handle_charref(self, ref):
print "[charref]", data
def handle_entityref(self, name):
print "[entityref]", name
def handle_comment(self, data):
print "[comment]", data
def handle_decl(self, decl):
print "[decl]", decl
def handle_pi(self, data):
print "[pi]",data
#------------------------------------------------------------------------------
class TestSgmlParser(SGMLParser):
"""
SGMLParserテスト用
"""
def __init__(self):
SGMLParser.__init__(self)
self.table_count = 0
self.data_count = 0
def handle_starttag(self, tag, method, attrs):
print "[starttag]", tag, method, attrs
def handle_endtag(self, tag, method):
print "[endtag]", tag, method
def handle_data(self, data):
print "[data] ", data, "count:", self.data_count
self.data_count += 1
def handle_comment(self, data):
print "[comment]", data
def handle_decl(self, decl):
print "[decl]", decl
def handle_pi(self, data):
print "[pi]", data
def unknown_starttag(self, tag, attrs):
print "[unknown_starttag]", tag, attrs, "count:", self.table_count
if tag == "table":
self.table_count += 1
self.data_count = 0
def unknown_endtag(self, tag):
print "[unknown_endtag]", tag
def unknown_charref(self, ref):
print "[unknown_charref]", ref
def unknown_entityref(self, ref):
print "[unknown_entityref]", ref
#------------------------------------------------------------------------------
# 外部関数定義
#------------------------------------------------------------------------------
def check(url):
"""
検査関数
"""
parser = TestSgmlParser()
try:
# URLを開く
f = webtool.openYahoo(str(url))
except IOError, (errno, strerror):
# エラー出力
print "[error]url error"
debugtool.printFrame()
conv_str = (lambda data: unicode(data, 'euc-jp', 'ignore')
.encode('shift_jis', 'ignore'))
text = conv_str(f.read())
parser.feed(text)
#------------------------------------------------------------------------------
# テスト関数定義
#------------------------------------------------------------------------------
def __test():
"""
テスト関数
"""
url = "urlを記入"
check(url)
#------------------------------------------------------------------------------
# main
#------------------------------------------------------------------------------
if __name__ == "__main__":
__test()
スクリプトファイルテンプレート
#!/usr/bin/env python
# coding: utf-8
"""TODO helloworld
概要
TODO: helloworld [-?] [-v]
詳細
TODO: This describes how to use this script. This docstring
will be printed by the script if there is a problem or
if the user requests help (-?, --?, --help).
オプション
-? : 使用方法出力 (--? と --helpは同じ)
-t : 時間計測
使用例
TODO: Show some examples of how to use this script.
終了コード
TODO: List exit codes
"""
#------------------------------------------------------------------------------
# module
#------------------------------------------------------------------------------
__version__ = "$Revision: 1.00 $".split()[-2:][0]
__all__ = {}
#------------------------------------------------------------------------------
# import
#------------------------------------------------------------------------------
import sys, os, getopt, traceback
import time
import re
#------------------------------------------------------------------------------
# 定数定義
#------------------------------------------------------------------------------
MESURE_TIME = False
#------------------------------------------------------------------------------
# 関数定義
#------------------------------------------------------------------------------
def exit_with_usage ():
"""
使用方法を出力して、処理を終了させる
"""
print unicode(globals()['__doc__'], "utf-8", "ignore")
os._exit(1)
#------------------------------------------------------------------------------
def parse_args (options='', long_options=[]):
"""
引数をパースする
"""
try:
optlist, args = getopt.getopt(sys.argv[1:], options+'?', long_options+['help','?'])
except Exception, e:
print str(e)
exit_with_usage()
options = dict(optlist)
# -? -helpであれば、使用方法を出力して、処理を終了
if [elem for elem in options if elem in ['-?','--?','--help']]:
exit_with_usage()
return (options, args)
#------------------------------------------------------------------------------
# メイン処理定義
#------------------------------------------------------------------------------
def main ():
"""
メイン処理を記述
"""
print 'Hello world!'
#------------------------------------------------------------------------------
# main
#------------------------------------------------------------------------------
if __name__ == '__main__':
# 引数をパースする
(options, args) = parse_args('t')
# 時間計測フラグがあるかどうかを調べる
if '-t' in options:
MESURE_TIME = True
else:
MESURE_TIME = False
# 引数が必ずいるスクリプトの場合は、ここで終了する
# if len(args)<=0:
# exit_with_usage()
try:
start_time = time.time()
if MESURE_TIME: print "*" * 80
if MESURE_TIME: print "start: ", time.asctime()
if MESURE_TIME: print "*" * 80
main()
if MESURE_TIME: print "*" * 80
if MESURE_TIME: print "end: ", time.asctime()
if MESURE_TIME: print '処理時間: ',
if MESURE_TIME: print (time.time() - start_time) / 60.0
if MESURE_TIME: print "*" * 80
sys.exit(0)
except SystemExit, e:
raise e
except Exception, e:
print 'ERROR, UNEXPECTED EXCEPTION'
print str(e)
traceback.print_exc()
os._exit(1)
Java
クラスのテンプレート
/**
* クラスの説明
* @author 編集者
* @since 2007/11/01
* @version 1.0
*/
class Test
{
/** 変数の説明 */
private int n = 0;
/**
* 関数の説明
* @param m パラメータの説明
* @return 返り値の説明
*/
public int test(int m)
{
return (n*m);
}
}