NI-Lab. - blotted-000011 ~ 書き散らかしたメモ ~
/*****************************************************************************/
VC++でのクラス名と関数名(メソッド名)の命名規則どうしよう……
ということで、考えてみた一例。
ライブラリ
汎用クラス
FooClass.barFunction();
WindowsAPIラッパークラス
FooClass.BarFunction();
MFC拡張クラス
FooClass.BarFunction();
/*****************************************************************************/
COleDateTime
DATE
VT_DATE
/*****************************************************************************/
[MFC]CStdioFileの使用サンプル
CString strFileName("C:\\hoge.txt");
UINT nOpenFlags = CFile::modeRead;
TRY
{
CStdioFile file;
file.Open(strFileName, nOpenFlags);
CString strLine;
while(file.ReadString(strLine)){
MessageBox(strLine);
}
file.Close();
}
CATCH( CFileException, e )
{
;
}
END_CATCH
/*****************************************************************************/
ASCIIコード表を表示する
$ man ascii
参考:
http://www02.so-net.ne.jp/~hat/imail/sec02.html
[2] 様々な文字コード - インターネットメールの注意点
/*****************************************************************************/
[Windows]標準メッセージボックスの内容をクリップボードにコピーする
[Ctrl]+[C] でコピー可能になったのは、Windows2000から?
/*****************************************************************************/
[JavaScript]オブジェクトの型を取得する
typeof(objectName) でオブジェクトのプロトタイプを取得できる。
document.formName.elementName.type で フォームのエレメントの文字列を取得できる。
<!-- 使用例 -->
<script language="JavaScript">
<!--
function check(target) {
alert(target.type); // フォームのエレメントの文字列
alert(typeof(target)); // オブジェクトのプロトタイプ
var val;
if(target.type == "select-one"){
// 指定されたオブジェクトが select
// select の値を取得する
val = target.options[ target.selectedIndex ].value;
}else if(target.type == "hidden"){
// 指定されたオブジェクトが hidden
// hidden の値を取得する
val = target.value;
}
alert(val); // 値を表示
}
-->
</script>
/*****************************************************************************/
[Java]ファイル名から拡張子を抜き出す
// test
public static void main(String[] args) {
System.out.println(getExtensionWithDot("file.jpg"));
System.out.println(getExtensionWithDot("file.png"));
System.out.println(getExtensionWithDot("file."));
System.out.println(getExtensionWithDot("file"));
System.out.println(getExtensionWithDot(".jpg"));
}
/**
* ファイル名から拡張子を取得します。
* @param file ファイル名
* @return ドット(.)を含む拡張子文字列
* ファイル名には、ファイルの名前区切り文字 File.separatorChar を含まないものを指定する。
*/
private static String getExtensionWithDot(String file){
int i = file.lastIndexOf('.');
if(i == -1){
return ""; // 拡張子なし
}else{
return file.substring(i);
}
}
/*****************************************************************************/
[StyleSheet]ブラウザには表示するが印刷されなくする方法
<style type="text/css" media="print">
<!--
.notprint {visibility: hidden;}
-->
</style>
<p class="notprint">
印刷時には見えない部分。
</p>
/*****************************************************************************/
[Java]バイト数0を指定してStringオブジェクトを生成する
public class NewStringTest {
public static void main(String[] args) throws Exception{
byte[] b = new byte[] { 0, 0, 0, 0, 0, }; // どんな数値でも良い
String s = new String(b, 0, 0, "MS932"); // バイト数0を指定する
System.out.println("The created string is '" + s + "'");
}
}
実行結果:
The created string is ''
/*****************************************************************************/
[Java]intからbyteへキャストした変換の逆数値変換
public class I2B {
public static void main(String[] args) {
System.out.println("int" + "\t" + "byte");
for (int i = 0; i <= 255; i++) {
byte b = (byte) i; // int から byte へキャスト
System.out.println(i + "\t" + b);
}
System.out.println("byte" + "\t" + "int");
for (int b = -128; b <= 127; b++) {
int i = toInt((byte)b); // int から byte へのキャストの逆変換
System.out.println(b + "\t" + i);
}
}
private static int toInt(byte b) {
// int : 0 ~ 255 (0x00-0x0ff)
// byte : -128 ~ 127
return (int) (0xff & b);
}
}
実行結果:
int byte
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 16
17 17
18 18
19 19
20 20
21 21
22 22
23 23
24 24
25 25
26 26
27 27
28 28
29 29
30 30
31 31
32 32
33 33
34 34
35 35
36 36
37 37
38 38
39 39
40 40
41 41
42 42
43 43
44 44
45 45
46 46
47 47
48 48
49 49
50 50
51 51
52 52
53 53
54 54
55 55
56 56
57 57
58 58
59 59
60 60
61 61
62 62
63 63
64 64
65 65
66 66
67 67
68 68
69 69
70 70
71 71
72 72
73 73
74 74
75 75
76 76
77 77
78 78
79 79
80 80
81 81
82 82
83 83
84 84
85 85
86 86
87 87
88 88
89 89
90 90
91 91
92 92
93 93
94 94
95 95
96 96
97 97
98 98
99 99
100 100
101 101
102 102
103 103
104 104
105 105
106 106
107 107
108 108
109 109
110 110
111 111
112 112
113 113
114 114
115 115
116 116
117 117
118 118
119 119
120 120
121 121
122 122
123 123
124 124
125 125
126 126
127 127
128 -128
129 -127
130 -126
131 -125
132 -124
133 -123
134 -122
135 -121
136 -120
137 -119
138 -118
139 -117
140 -116
141 -115
142 -114
143 -113
144 -112
145 -111
146 -110
147 -109
148 -108
149 -107
150 -106
151 -105
152 -104
153 -103
154 -102
155 -101
156 -100
157 -99
158 -98
159 -97
160 -96
161 -95
162 -94
163 -93
164 -92
165 -91
166 -90
167 -89
168 -88
169 -87
170 -86
171 -85
172 -84
173 -83
174 -82
175 -81
176 -80
177 -79
178 -78
179 -77
180 -76
181 -75
182 -74
183 -73
184 -72
185 -71
186 -70
187 -69
188 -68
189 -67
190 -66
191 -65
192 -64
193 -63
194 -62
195 -61
196 -60
197 -59
198 -58
199 -57
200 -56
201 -55
202 -54
203 -53
204 -52
205 -51
206 -50
207 -49
208 -48
209 -47
210 -46
211 -45
212 -44
213 -43
214 -42
215 -41
216 -40
217 -39
218 -38
219 -37
220 -36
221 -35
222 -34
223 -33
224 -32
225 -31
226 -30
227 -29
228 -28
229 -27
230 -26
231 -25
232 -24
233 -23
234 -22
235 -21
236 -20
237 -19
238 -18
239 -17
240 -16
241 -15
242 -14
243 -13
244 -12
245 -11
246 -10
247 -9
248 -8
249 -7
250 -6
251 -5
252 -4
253 -3
254 -2
255 -1
byte int
-128 128
-127 129
-126 130
-125 131
-124 132
-123 133
-122 134
-121 135
-120 136
-119 137
-118 138
-117 139
-116 140
-115 141
-114 142
-113 143
-112 144
-111 145
-110 146
-109 147
-108 148
-107 149
-106 150
-105 151
-104 152
-103 153
-102 154
-101 155
-100 156
-99 157
-98 158
-97 159
-96 160
-95 161
-94 162
-93 163
-92 164
-91 165
-90 166
-89 167
-88 168
-87 169
-86 170
-85 171
-84 172
-83 173
-82 174
-81 175
-80 176
-79 177
-78 178
-77 179
-76 180
-75 181
-74 182
-73 183
-72 184
-71 185
-70 186
-69 187
-68 188
-67 189
-66 190
-65 191
-64 192
-63 193
-62 194
-61 195
-60 196
-59 197
-58 198
-57 199
-56 200
-55 201
-54 202
-53 203
-52 204
-51 205
-50 206
-49 207
-48 208
-47 209
-46 210
-45 211
-44 212
-43 213
-42 214
-41 215
-40 216
-39 217
-38 218
-37 219
-36 220
-35 221
-34 222
-33 223
-32 224
-31 225
-30 226
-29 227
-28 228
-27 229
-26 230
-25 231
-24 232
-23 233
-22 234
-21 235
-20 236
-19 237
-18 238
-17 239
-16 240
-15 241
-14 242
-13 243
-12 244
-11 245
-10 246
-9 247
-8 248
-7 249
-6 250
-5 251
-4 252
-3 253
-2 254
-1 255
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 16
17 17
18 18
19 19
20 20
21 21
22 22
23 23
24 24
25 25
26 26
27 27
28 28
29 29
30 30
31 31
32 32
33 33
34 34
35 35
36 36
37 37
38 38
39 39
40 40
41 41
42 42
43 43
44 44
45 45
46 46
47 47
48 48
49 49
50 50
51 51
52 52
53 53
54 54
55 55
56 56
57 57
58 58
59 59
60 60
61 61
62 62
63 63
64 64
65 65
66 66
67 67
68 68
69 69
70 70
71 71
72 72
73 73
74 74
75 75
76 76
77 77
78 78
79 79
80 80
81 81
82 82
83 83
84 84
85 85
86 86
87 87
88 88
89 89
90 90
91 91
92 92
93 93
94 94
95 95
96 96
97 97
98 98
99 99
100 100
101 101
102 102
103 103
104 104
105 105
106 106
107 107
108 108
109 109
110 110
111 111
112 112
113 113
114 114
115 115
116 116
117 117
118 118
119 119
120 120
121 121
122 122
123 123
124 124
125 125
126 126
127 127
/*****************************************************************************/
[StyleSheet]画像と文字を重ねて表示する
HTMLのみでは無理。スタイルシート必須。
http://allabout.co.jp/computer/hpcreate/closeup/CU20040510A/
画像上の自由な位置に字を重ねる
> <div style="position: relative;">
> <img src="dog.jpg" width="320" height="140" alt="犬看板"><br>
> <div style="position: absolute; top: 15px; left: 150px; width: 150px;">
> 看板の内側に表示する文章です。
> </div>
> </div>
/*****************************************************************************/
[Windows]標準エラー出力のリダイレクト
http://www.monyo.com/technical/windows/04.html
標準エラー出力をハンドルする - NTコマンドプロンプトの機能紹介
> c:\> route 2> route.txt
> c:\> (complex.exe 2> stderr.txt) > stdout.txt
> c:\> route 2>&1 | more
/*****************************************************************************/
[Ant]ant -projecthelp
<target name="compile" description="Compile SourceCode">
description に説明文を書いておくと、
ant -projecthelp で、ヘルプ表示のように説明文が表示される。
> C:\>ant -projecthelp
> Buildfile: build.xml
>
> Main targets:
>
> all All exec target
> compile Compile SourceCode
> Default target: compile
/*****************************************************************************/
[Java]JComboBoxの編集中の値を取得する
JComboBox#getSelectedItem では、編集中の値を取得できないので、
JComboBox#getEditor で取得した ComboBoxEditor の
getItem メソッドにて値を取得する。
JComboBox#getSelectedItem の問題点。
たとえば、ユーザが JComboBox の値を編集しつつ、
編集している値が確定しないままで、
メニュー等からイベントを起動させたとき、
イベント内で JComboBox#getSelectedItem を使って取得した値は、
編集前の値になっている。
class Test implements ActionListener {
JComboBox cb;
String current;
Test() {
cb = new JComboBox();
cb.setEditable(true); // 編集可能にする
}
String getCurrent() {
return current;
}
public void actionPerformed(ActionEvent e) {
// イベント等からは、こんな感じで取得。
// cb.getSelectedItem() は使わない。
current = (String)cb.getEditor().getItem();
}
}
/*****************************************************************************/
[Java]javax.imageio.ImageIOでサポートされているはずのJPEG画像が出力できない
// 擬似サンプルコード
// ImageIO.getWriterFormatNames() では、JPEGが返ってくるのに、
// なぜか ImageIO.write が false を返す。
BufferedImage image;
OutputStream os;
ImageIO.write(image, "jpeg", os);
たぶん、原因は↓コレ↓
http://developer.java.sun.com/developer/bugParade/bugs/4622201.html
Bug ID: 4622201 javax.imageio cannot compress JPEG images in JDK 1.4
どうやら、Windows2000 での JDK1.4 でのバグらしい。
1.4.1 で修正されている。
/*****************************************************************************/
[Java]匿名クラスを使用していないのに ClassName$1.class が生成されてしまう
// 単純化した例
public class Outside{
private class Inside{
// 内部クラスにて、
// パッケージプライベートな引数なしのコンストラクタを
// 明示的に宣言すると、ClassName$1.class が生成されない。
// ただし、このような単純な例で調査したところ、
// パッケージプライベートな引数なしのコンストラクタを宣言しなくても、
// ClassName$1.class が生成されない?
// 要するに、よくわからないということ。
Inside(){
; //
}
}
}
$1.class
参考:
http://www.lake.its.hiroshima-cu.ac.jp/~mondo/Java/TnE/014.html
Trial and Error in Java 014 : anonymous class, Part 1
/*****************************************************************************/
[Tomcat]Windowsサービスに登録する
Apache Tomcat 5 の場合。
// tomcat service usage
Usage: service.bat install/remove [service_name]
// ウィンドウズのサービスに登録
${CATALINA_HOME}/bin/service install
// ウィンドウズのサービスから削除
${CATALINA_HOME}/bin/service remove
/*****************************************************************************/
[Apache]外部PCにて共有されているファイルを利用する方法
Tomcatでも、ほぼ同様だと思う。
Tomcat5w.exe で表示されるサービス設定画面だとうまく動作しなかったが、
Windows2000付属の[管理ツール] → [サービス] で、
[Apache Tomcat] の [ログオン] タブにて、
アカウントを設定したら動作した。
サーバ起動アカウントがファイルを見る権限があるかどうかが問題ということで。
http://mm.apache.or.jp/pipermail/newbie/2002-October.txt
[Newbie 3130] Re: 他のPC に対しAliasを設定することは可能?
> Alias /vpath/ "//Hostname4\work/"
>UNCパスの先頭を\\ではなく//で記述する必要があるようです。
[Newbie 3132] Re: 他のPC に対しAliasを設定することは可能?
>Apacheをサービスで起動している場合は、サービスのプロパティを開いて
>「ログオン」のタブを確認してください。
>デフォルトだと、「ローカルシステムアカウント」が選択されてると
>思うんで、「アカウント」の方を選んで、共有フォルダにアクセス
>できるユーザアカウントとパスワードを設定してください。
>
>※「ローカルシステムアカウント」は共有フォルダに対する
> アクセス権をもっていないはずですので・・・。
/*****************************************************************************/
[Java]double値の比較
public class DoubleTest {
public static void main(String[] args){
System.out.println(Double.NaN);
double d1 = 0.0;
double d2 = -0.0;
System.out.println(d1 + " == " + d2 + " -> " + (d1 == d2));
double d3 = 1.000;
double d4 = 1.000000;
System.out.println(d3 + " == " + d4 + " -> " + (d3 == d4));
double d5 = 1.001;
double d6 = 1.001000;
System.out.println(d5 + " == " + d6 + " -> " + (d5 == d6));
}
}
// 実行結果
C:\>java DoubleTest
NaN
0.0 == -0.0 -> true
1.0 == 1.0 -> true
1.001 == 1.001 -> true
/*****************************************************************************/
[Java]Tomcat4にて、サーブレットをロードしているクラスローダ。
WebappClassLoader
available:
delegate: false
repositories:
/WEB-INF/classes/
required:
----------> Parent Classloader:
StandardClassLoader
available:
delegate: true
repositories:
file:C:\java\tomcat\shared\classes\
required:
----------> Parent Classloader:
StandardClassLoader
available:
Extension[org.apache.tools.ant, implementationVendor=Apache Software Foundat
ion, implementationVersion=1.6.0, specificationVendor=Apache Software Foundation
, specificationVersion=1.6.0]
Extension[org.apache.commons.collections, implementationVendor=Apache Softwa
re Foundation, implementationVersion=2.1, specificationVendor=Apache Software Fo
undation, specificationVersion=2.1]
Extension[commons-dbcp, implementationVendor=Apache Software Foundation, imp
lementationVendorId=, implementationVersion=1.1, specificationVendor=Apache Soft
ware Foundation, specificationVersion=]
Extension[org.apache.commons.logging, implementationVendor=Apache Software F
oundation, implementationVersion=1.0.3, specificationVendor=Apache Software Foun
dation, specificationVersion=1.0]
Extension[commons-pool, implementationVendor=Apache Software Foundation, imp
lementationVendorId=, implementationVersion=1.1, specificationVendor=Apache Soft
ware Foundation, specificationVersion=]
Extension[javax.mail, implementationVendor=Sun Microsystems, Inc., implement
ationVendorId=com.sun, implementationVersion=1.2, specificationVendor=Sun Micros
ystems, Inc., specificationVersion=1.2]
delegate: true
repositories:
file:C:\java\tomcat\common\classes\
file:C:\java\tomcat\common\endorsed\xercesImpl.jar
file:C:\java\tomcat\common\endorsed\xmlParserAPIs.jar
file:C:\java\tomcat\common\lib\activation.jar
file:C:\java\tomcat\common\lib\ant.jar
file:C:\java\tomcat\common\lib\commons-collections.jar
file:C:\java\tomcat\common\lib\commons-dbcp-1.1.jar
file:C:\java\tomcat\common\lib\commons-logging-api.jar
file:C:\java\tomcat\common\lib\commons-pool-1.1.jar
file:C:\java\tomcat\common\lib\jasper-compiler.jar
file:C:\java\tomcat\common\lib\jasper-runtime.jar
file:C:\java\tomcat\common\lib\jdbc2_0-stdext.jar
file:C:\java\tomcat\common\lib\jndi.jar
file:C:\java\tomcat\common\lib\jta.jar
file:C:\java\tomcat\common\lib\mail.jar
file:C:\java\tomcat\common\lib\naming-common.jar
file:C:\java\tomcat\common\lib\naming-factory.jar
file:C:\java\tomcat\common\lib\naming-resources.jar
file:C:\java\tomcat\common\lib\servlet.jar
required:
----------> Parent Classloader:
sun.misc.Launcher$AppClassLoader@18e3e60
/*****************************************************************************/
[Java]JVMでサポートされている文字セット名リストを表示する
public class AvailableCharsetNames {
public static void main(String[] args) {
java.util.Map names = java.nio.charset.Charset.availableCharsets();
for (java.util.Iterator i = names.keySet().iterator(); i.hasNext();) {
System.out.println(i.next());
}
}
}
/*****************************************************************************/
[Java]${JAVA_HOME}/jre/lib/ext
${JAVA_HOME}/jre/lib/ext 以下にあるファイルは
拡張子が jar でなくても読み込まれてしまうらしい。(zipとか)
jar_ を読みこんだことがあるので。
/*****************************************************************************/
[Tomcat]org.apache.commons.digester.Digester error
http://www.jajakarta.org/kvasir/bbs/technical/68?expand=true
サーブレット WEB.XMLの編集 致命的: Parse Error
> web.xml の要素は出現順序が決まっています。
> サーブレットごとに書いてはいけません。要素の種類ごとに並べてくださ。い
> servlet 要素、 servlet-mapping 要素、それぞれまとめて書けばOKです。
http://java.sun.com/j2ee/dtds/web-app_2_2.dtd
> <!ELEMENT web-app (icon?, display-name?, description?, distributable?,
> context-param*, servlet*, servlet-mapping*, session-config?,
> mime-mapping*, welcome-file-list?, error-page*, taglib*,
> resource-ref*, security-constraint*, login-config?, security-role*,
> env-entry*, ejb-ref*)>
/*****************************************************************************/
[Java]拡張子から入出力可能な画像フォーマットか判定
boolean isReadableImageFormatName(String s) {
String[] names = ImageIO.getReaderFormatNames();
for (int i = 0; i < names.length; i++) {
if (names[i].equalsIgnoreCase(s)) {
return true;
}
}
return null;
}
boolean isWritableImageFormatName(String s) {
String[] names = ImageIO.getWriterFormatNames();
for (int i = 0; i < names.length; i++) {
if (names[i].equalsIgnoreCase(s)) {
return names[i];
}
}
return null;
}
/*****************************************************************************/
// 出力する画像フォーマットが透明色を持つことができないときに、画像の透明色を消去する方法
BufferedImage eraseTransparentColor(BufferedImage in) {
BufferedImage out =
new BufferedImage(
in.getWidth(null),
in.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics g = out.createGraphics();
g.drawImage(in, 0, 0, null);
return out;
}
/*****************************************************************************/
ImageIO.getReaderFormatNames() と ImageIO.getWriterFormatNames() の
Windows2000, Java 1.4.1_02 での取得例
ReaderFormatNames: jpeg, gif, png, JPG, jpg, JPEG
WriterFormatNames: jpeg, png, JPG, PNG, jpg, JPEG
/*****************************************************************************/
// 単純に画像フォーマット変換をする
void convert(File in, File out, String formatname) throws IOException {
BufferedImage bi = ImageIO.read(in);
ImageIO.write(bi, formatname, out);
}
/*****************************************************************************/