Skip to content

Commit 6c24731

Browse files
committed
Update hspcmp
コンパイル時に複数のファイルがある場合は文字コードを統一するように修正
1 parent f56fc6e commit 6c24731

12 files changed

Lines changed: 167 additions & 30 deletions

File tree

src/hspcmp/codegen.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,12 +2668,15 @@ int CToken::PutDSStr(char *str, bool converts_to_utf8)
26682668
// Register strings to data segment (caching)
26692669

26702670
char *p;
2671+
p = str;
26712672

26722673
// output as UTF8 format
26732674
if ( converts_to_utf8 ) {
2674-
p = ExecSCNV(str, SCNV_OPT_SJISUTF8);
2675+
//ソースコードがUTF-8の場合は変換は必要ない
2676+
if (pp_utf8 == 0) {
2677+
p = ExecSCNV(str, SCNV_OPT_SJISUTF8);
2678+
}
26752679
} else {
2676-
p = str;
26772680
if (pp_utf8) {
26782681
if ((hed_cmpmode & CMPMODE_UTF8OUT)==0) {
26792682
p = ExecSCNV(str, SCNV_OPT_UTF8SJIS);
@@ -3149,7 +3152,6 @@ int CToken::GenerateCode( CMemBuf *srcbuf, char *oname, int mode )
31493152
cg_utf8out = mode & COMP_MODE_UTF8;
31503153
cg_strmap = mode & COMP_MODE_STRMAP;
31513154
cg_skiperror = mode & COMP_MODE_SKIPERROR;
3152-
if ( pp_utf8 ) cg_utf8out = 0; // ソースコードがUTF-8の場合は変換は必要ない
31533155

31543156
if (cg_utf8out) {
31553157
Mes("#use UTF-8 strings.");
@@ -3171,7 +3173,7 @@ int CToken::GenerateCode( CMemBuf *srcbuf, char *oname, int mode )
31713173

31723174
if ( res ) {
31733175
// エラー終了
3174-
char tmp[512];
3176+
char tmp[8192];
31753177
CStrNote note;
31763178
CMemBuf srctmp;
31773179
#ifdef JPNMSG
@@ -3182,6 +3184,14 @@ int CToken::GenerateCode( CMemBuf *srcbuf, char *oname, int mode )
31823184
if ( cg_errline > 0 ) {
31833185
note.Select( bakbuf.GetBuffer() );
31843186
note.GetLine( tmp, cg_errline-1, 510 );
3187+
#ifdef HSPWIN
3188+
if (pp_utf8) {
3189+
// UTF-8 -> Shift-JIS
3190+
char stmp[8192];
3191+
strcpy(stmp,tmp);
3192+
ConvUtf82SJis(stmp, tmp, (int)strlen(stmp));
3193+
}
3194+
#endif
31853195
Mesf( "--> %s",tmp );
31863196
}
31873197
} else {

src/hspcmp/hsc3.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,12 @@ void CHsc3::PreProcessEnd( void )
343343
}
344344

345345

346+
int CHsc3::GetHeaderOption(void)
347+
{
348+
return hed_option;
349+
}
350+
351+
346352
int CHsc3::Compile( char *fname, char *outname, int mode )
347353
{
348354
// Compile
@@ -354,7 +360,9 @@ int CHsc3::Compile( char *fname, char *outname, int mode )
354360
CToken tk;
355361

356362
genmode = mode;
357-
if ( cmpopt & CMPMODE_UTF8OUT ) genmode |= HSC3_MODE_UTF8;
363+
if (cmpopt & CMPMODE_UTF8OUT) {
364+
genmode |= HSC3_MODE_UTF8;
365+
}
358366

359367
if ( lb_info != NULL ) tk.SetLabelInfo( lb_info ); // プリプロセッサのラベル情報
360368

src/hspcmp/hsc3.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class CHsc3 {
6565
int GetRuntimeFromHeader( char *fname, char *res );
6666
int SaveOutbuf( char *fname );
6767
int SaveAHTOutbuf( char *fname );
68+
int GetHeaderOption(void);
6869

6970
// Analyse
7071
void InitAnalysisInfo(int mode, char* match, int line=0 );

src/hspcmp/main.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static char *p[] = {
4444
" -r execute runtime with result",
4545
" -s output string map",
4646
" -m compile for Emscripten",
47+
" -x use 32bit runtime",
4748
" ---------------------------------",
4849
" -h??? print command help",
4950
" -lk??? print HSP3 keyword list",
@@ -67,7 +68,7 @@ int main( int argc, char *argv[] )
6768
{
6869
char a1,a2,a3;
6970
int b,st;
70-
int cmpopt,ppopt,utfopt,pponly,execobj,strmap,hsphelp;
71+
int cmpopt,ppopt,utfopt,pponly,execobj,strmap,hsphelp,hsp64;
7172
char *opt_lk = NULL;
7273
char *opt_ls = NULL;
7374
int opt_lsref, opt_lsmode;
@@ -83,7 +84,7 @@ int main( int argc, char *argv[] )
8384

8485
if (argc<2) { usage1();return -1; }
8586

86-
st = 0; ppopt = 0; cmpopt = 0; utfopt = 0; pponly = 0; strmap = 0; hsphelp = 0; opt_lsref = 0; opt_lsmode = 0;
87+
st = 0; ppopt = 0; cmpopt = 0; utfopt = 0; pponly = 0; strmap = 0; hsphelp = 0; opt_lsref = 0; opt_lsmode = 0; hsp64 = 1;
8788
execobj = 0;
8889
fname[0]=0;
8990
fname2[0]=0;
@@ -173,6 +174,8 @@ int main( int argc, char *argv[] )
173174
}
174175
st = 1;
175176
break;
177+
case 'x':
178+
hsp64 = 0; break;
176179
default:
177180
st = 1;break;
178181
}
@@ -220,6 +223,11 @@ int main( int argc, char *argv[] )
220223
strcpy( fname2, fname ); cutext( fname2 ); addext( fname2,"i" );
221224
addext( fname,"hsp" ); // 拡張子がなければ追加する
222225

226+
// HSP64 check
227+
if (hsp64) {
228+
ppopt |= HSC3_OPT_RUNTIME64 | HSC3_OPT_UTF8OUT;
229+
}
230+
223231
// label pick
224232
if (opt_ls) {
225233
if (*opt_ls == 0) opt_ls = NULL;
@@ -228,6 +236,11 @@ int main( int argc, char *argv[] )
228236
hsc3->InitAnalysisInfo(opt_lsmode | opt_lsref, opt_ls);
229237
st = hsc3->PreProcess(fname, fname2, ppopt, fname);
230238
if ((pponly == 0) && (st == 0)) {
239+
if (hsp64) {
240+
if (hsc3->GetHeaderOption() & HEDINFO_HSP64) {
241+
cmpopt |= HSC3_MODE_RUNTIME64 | HSC3_MODE_UTF8;
242+
}
243+
}
231244
st = hsc3->CompileLabelOut(fname2, cmpopt);
232245
}
233246
if (st >= 0) {
@@ -294,6 +307,11 @@ int main( int argc, char *argv[] )
294307
// 通常のコンパイル
295308
st = hsc3->PreProcess( fname, fname2, ppopt, fname );
296309
if (( pponly == 0 )&&( st == 0 )) {
310+
if (hsp64) {
311+
if (hsc3->GetHeaderOption() & HEDINFO_HSP64) {
312+
cmpopt |= HSC3_MODE_RUNTIME64 | HSC3_MODE_UTF8;
313+
}
314+
}
297315
st = hsc3->Compile( fname2, oname, cmpopt );
298316
}
299317
puts( hsc3->GetError() );

src/hspcmp/membuf.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <stdarg.h>
1010
#include <assert.h>
1111
#include "membuf.h"
12+
#include "supio.h"
1213

1314
//-------------------------------------------------------------
1415
// Routines
@@ -48,6 +49,42 @@ void CMemBuf::InitIndexBuf( int sz )
4849
}
4950

5051

52+
char *CMemBuf::InitSubBuffer(int sz)
53+
{
54+
sub_buf = (char*)malloc(sz);
55+
return sub_buf;
56+
}
57+
58+
59+
void CMemBuf::ExchangeSubToMainBuffer(int size)
60+
{
61+
if (mem_buf != NULL) {
62+
free(mem_buf);
63+
}
64+
if (idxbuf != NULL) {
65+
free(idxbuf);
66+
}
67+
68+
limit_size = size;
69+
mem_buf = (char*)malloc(limit_size);
70+
mem_buf[0] = 0;
71+
memcpy( mem_buf, sub_buf, limit_size);
72+
73+
if (sub_buf != NULL) {
74+
free(sub_buf);
75+
sub_buf = NULL;
76+
}
77+
78+
cur = size;
79+
80+
// Indexバッファ初期化
81+
idxflag = 0;
82+
idxmax = -1;
83+
curidx = 0;
84+
idxbuf = NULL;
85+
}
86+
87+
5188
char *CMemBuf::PreparePtr( int sz )
5289
{
5390
// バッファ拡張チェック

src/hspcmp/membuf.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class CMemBuf {
1717
public:
1818
CMemBuf();
1919
CMemBuf( int sz );
20+
21+
char *InitSubBuffer( int sz );
22+
void ExchangeSubToMainBuffer(int size);
23+
2024
virtual ~CMemBuf();
2125
void AddIndexBuffer( void );
2226
void AddIndexBuffer( int sz );
@@ -68,6 +72,8 @@ class CMemBuf {
6872
int idxmax; // Index Buffer Max
6973
int curidx; // Current Index
7074

75+
char *sub_buf; // Sub Buffer
76+
7177
char name[256]; // File Name
7278
};
7379

src/hspcmp/token.cpp

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3832,6 +3832,7 @@ int CToken::ExpandLine( CMemBuf *buf, CMemBuf *src, char *refname )
38323832
//
38333833
char *p = src->GetBuffer();
38343834
int pline = 1;
3835+
bool utf8text = false;
38353836
enumgc = 0;
38363837
mulstr = LMODE_ON;
38373838
*errtmp = 0;
@@ -3851,10 +3852,38 @@ int CToken::ExpandLine( CMemBuf *buf, CMemBuf *src, char *refname )
38513852
#endif
38523853
}
38533854
p += 3;
3855+
utf8text = true;
38543856
}
38553857
}
38563858
}
38573859

3860+
#ifdef HSPWIN
3861+
// Windows版のみ、入力のテキスト文字コードを指定に合わせて変換する
3862+
//
3863+
int currentsize = (int)strlen(p);
3864+
if (utf8text==false) utf8text = IsUTF8Text(p);
3865+
if (pp_utf8 == 0){
3866+
if (utf8text) {
3867+
// UTF-8 -> Shift-JIS
3868+
Mesf("#Convert to SJIS [%s].",refname);
3869+
char *p_sjis = src->InitSubBuffer(currentsize);
3870+
int newsize = ConvUtf82SJis(p, p_sjis, currentsize);
3871+
src->ExchangeSubToMainBuffer(newsize);
3872+
p = src->GetBuffer();
3873+
}
3874+
}
3875+
else {
3876+
if (!utf8text) {
3877+
// Shift-JIS -> UTF-8
3878+
Mesf("#Convert to UTF8 [%s].", refname);
3879+
currentsize = currentsize * 4 + 1;
3880+
char* p_utf8 = src->InitSubBuffer(currentsize);
3881+
int newsize = ConvSJis2Utf8(p, p_utf8, currentsize);
3882+
src->ExchangeSubToMainBuffer(newsize);
3883+
p = src->GetBuffer();
3884+
}
3885+
}
3886+
#endif
38583887
while(1) {
38593888
RegistExtMacro( "__line__", pline ); // 行番号マクロを更新
38603889
pp_orgline = pline;
@@ -3871,14 +3900,15 @@ int CToken::ExpandLine( CMemBuf *buf, CMemBuf *src, char *refname )
38713900
}
38723901

38733902
while(1) {
3874-
a1 = *(unsigned char *)p;
38753903
if ( a1 == ' ' || a1 == '\t' ) {
38763904
p++; continue;
38773905
}
38783906
#ifdef HSPWIN
38793907
if ( hed_cmpmode & CMPMODE_SKIPJPSPC ) {
3880-
if ( a1 == 0x81 && p[1] == 0x40 ) { // 全角スペースチェック
3881-
p+=2; continue;
3908+
if (pp_utf8 == 0) {
3909+
if (a1 == 0x81 && p[1] == 0x40) { // 全角スペースチェック
3910+
p += 2; continue;
3911+
}
38823912
}
38833913
}
38843914
#endif
@@ -4502,6 +4532,24 @@ int CToken::ConvUtf82SJis(char* pSource, char* pDist, int buffersize)
45024532
}
45034533

45044534

4535+
int CToken::IsUTF8Text(char* pSource)
4536+
{
4537+
// 文字列がUTF-8であるかを判定する
4538+
//
4539+
while (*pSource) {
4540+
unsigned char c = (unsigned char)*pSource;
4541+
if (c <= 0x7f) {
4542+
pSource++;
4543+
continue;
4544+
}
4545+
int skip = CheckByteUTF8(c);
4546+
if (skip == 0) return 0; // UTF-8の形式に合わないバイトがあった
4547+
pSource += skip + 1;
4548+
}
4549+
return 1;
4550+
}
4551+
4552+
45054553
char* CToken::to_hsp_string_literal(const char* src, bool filename) {
45064554
// 文字列をHSPの文字列リテラル形式に
45074555
// 戻り値のメモリは呼び出し側がfreeする必要がある。

src/hspcmp/token.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class CToken {
253253
void SetHeaderOption(int opt, char* name) { hed_option = opt; strcpy(hed_runtime, name); }
254254
int GetCmpOption( void ) { return hed_cmpmode; }
255255
void SetCmpOption( int cmpmode ) { hed_cmpmode = cmpmode; }
256-
void SetUTF8Input( int utf8mode ) { pp_utf8 = utf8mode; }
256+
void SetUTF8Input(int utf8mode) { pp_utf8 = utf8mode; }
257257

258258
void GenerateLabelList(int mode, char* match);
259259
char* GetLabelListLineModule(void);
@@ -411,6 +411,7 @@ class CToken {
411411
int atoi_allow_overflow(const char* s);
412412
int ConvSJis2Utf8(char* pSource, char* pDist, int buffersize);
413413
int ConvUtf82SJis(char* pSource, char* pDist, int buffersize);
414+
int IsUTF8Text(char* pSource);
414415

415416
// Data
416417
//

src/hspcmp/win32dll/hspcmp3.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,11 @@ p1が128(bit7)の場合はデフォルトで64bitランタイムを選択しま
346346
cmpmode = p1 & HSC3_MODE_DEBUG;
347347
if (p1 & 4) cmpmode |= HSC3_MODE_UTF8;
348348
if (p1 & 8) cmpmode |= HSC3_MODE_STRMAP;
349-
if (p1 & 128) cmpmode |= HSC3_MODE_RUNTIME64 | HSC3_MODE_UTF8;
349+
if (p1 & 128) {
350+
if (hsc3->GetHeaderOption() & HEDINFO_HSP64) {
351+
cmpmode |= HSC3_MODE_RUNTIME64 | HSC3_MODE_UTF8;
352+
}
353+
}
350354

351355
if (p1 & 16) {
352356
st = hsc3->CompileLabelOut(fname2, cmpmode);
@@ -672,7 +676,7 @@ EXPORT BOOL WINAPI hsc3_make ( BMSCR *bm, char *p1, HSPPTRINT p2, HSPPTRINT p3 )
672676
#else
673677
myseed1 = (int)time(0); // Windows以外のランダムシード値
674678
#endif
675-
myseed2 = hsp3_flength(PACKFILE);
679+
myseed2 = (int)hsp3_flength(PACKFILE);
676680

677681
filepack.Reset();
678682
filepack.SetErrorBuffer(hsc3->errbuf);

0 commit comments

Comments
 (0)