2008年8月21日木曜日

文字列を改行での分割モジュールver02

分割は一行か七行と決めうちして、モジュールを作成したのですが
十行の分割がやりたくなったので
何行の分割もできるようにモジュールを改造しました

FILE名:FILE_div_str.hsp
#module div_str
/* グローバル変数
変数
max_ar ar_locとar_sの配列の最大
index 分割した数
string 分割する文字列
len Stringの長さ

配列
ar_loc \nの場所
ar_s 分割した文字列を格納
**/

// グローバル変数の初期化
#deffunc init_at_div_str int max
max_ar=max
dim ar_loc,max
sdim ar_s,max,256
sdim string,1024
index=0
return

// setter
#deffunc set_str_at_div_str str p
string=p
len=strlen(string)
return

// getter
#defcfunc get_str int p
if p>index:return ""
return ar_s.p

// 文字列分割
#deffunc div_str_at_div_str int div_num
num=0
if div_num>=max_ar{count=max_ar}
else{count=div_num}
count--

// \nの位置を調べて配列にar_loc\nの位置を格納
num++
if instr(string,ar_loc.0,"\n")==-1{ar_loc.1=-1}
else{ ar_loc.1=instr(string,ar_loc.0,"\n")+2}
repeat count,1
tmp=cnt+1
num++
if instr(string,ar_loc.cnt,"\n")==-1{ar_loc.tmp=-1:break}
else{ ar_loc.tmp=instr(string,ar_loc.cnt,"\n")+ar_loc.cnt+2 }
loop
index=num

// 一つ目の\nの位置までの文字列を配列ar_sに格納
if num>=1{
hoge=ar_loc.1-2
ar_s.1=strmid(string,0,hoge)
}

// \nの位置から次の\nの位置までの文字列を配列ar_sに格納
repeat count,1
n=cnt+1
if (num>=n)&(ar_loc.cnt!=-1){
hoge=ar_loc.n-ar_loc.cnt-2
ar_s.n=strmid(string,ar_loc.cnt,hoge)
}
loop

// 文字列に\nが含まれてないので、文字列を配列ar_sに格納
if ar_loc.1==-1{
ar_s.1=string
}

// 最後の\nの位置から、文字列の最後までを配列ar_sに格納
repeat count,1
nn=cnt+1
if ar_loc.nn==-1{
hoge=len-ar_loc.cnt
ar_s.nn=strmid(string,-1,hoge)
break
;mes nn ;コメントアウト
}
loop
return

// 複数行の文字列から、最初の一行
// 単一行の文字列であれば文字列全部を返す
#defcfunc div_one_at_div_str var pre
loc=instr(pre,0,"\n")
if loc==-1:return pre
return strmid(pre,0,loc)

#global

0 件のコメント: