Skip to content

Commit

Permalink
rewrite file open and enum func
Browse files Browse the repository at this point in the history
  • Loading branch information
ponapalt committed Aug 23, 2021
1 parent e2bbd81 commit 7019d2f
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 67 deletions.
10 changes: 5 additions & 5 deletions basis.cpp
Expand Up @@ -752,7 +752,7 @@ void CBasis::SaveVariable(const yaya::char_t* pName)

vm.logger().Message(7);
vm.logger().Filename(filename);
FILE *fp = yaya::w_fopen((wchar_t *)filename.c_str(), L"w");
FILE *fp = yaya::w_fopen(filename.c_str(), L"w");
if (fp == NULL) {
vm.logger().Error(E_E, 57, filename);
return;
Expand Down Expand Up @@ -903,10 +903,10 @@ void CBasis::RestoreVariable(const yaya::char_t* pName)
//暗号化セーブファイル対応
if ( ayc ) {
filename += L".ays";
fp = yaya::w_fopen((wchar_t *)filename.c_str(), L"r");
fp = yaya::w_fopen(filename.c_str(), L"r");
if (!fp) {
filename.erase(filename.size()-4,4);
fp = yaya::w_fopen((wchar_t *)filename.c_str(), L"r");
fp = yaya::w_fopen(filename.c_str(), L"r");
if (!fp) {
vm.logger().Error(E_N, 0);
return;
Expand All @@ -917,10 +917,10 @@ void CBasis::RestoreVariable(const yaya::char_t* pName)
}
}
else {
fp = yaya::w_fopen((wchar_t *)filename.c_str(), L"r");
fp = yaya::w_fopen(filename.c_str(), L"r");
if (!fp) {
filename += L".ays";
fp = yaya::w_fopen((wchar_t *)filename.c_str(), L"r");
fp = yaya::w_fopen(filename.c_str(), L"r");
if (!fp) {
vm.logger().Error(E_N, 0);
return;
Expand Down
4 changes: 2 additions & 2 deletions cell.h
Expand Up @@ -201,13 +201,13 @@ class CCell
{
const CValue &c = ansv_const();
if ( (c.s_value.size() > 10000) || c.array_size() ) {
m_ansv.reset((CValue*)NULL);
m_ansv.reset();
}
}
{
const CValue &c = emb_ansv_const();
if ( (c.s_value.size() > 10000) || c.array_size() ) {
m_emb_ansv.reset((CValue*)NULL);
m_emb_ansv.reset();
}
}
}
Expand Down
92 changes: 67 additions & 25 deletions dir_enum.cpp
Expand Up @@ -46,29 +46,50 @@ CDirEnum::~CDirEnum()

bool CDirEnum::next(CDirEnumEntry &entry)
{
std::string name;
std::string name_a;
yaya::string_t name_w;
bool isdir = false;

bool isUnicode = false;

#if defined(WIN32)
isUnicode = IsUnicodeAware();
#endif

while ( true ) {
if ( ! is_init ) {
#if defined(WIN32)
yaya::string_t tmp_str = enumpath + L"\\*.*";

WIN32_FIND_DATA w32FindData;
if ( isUnicode ) {
WIN32_FIND_DATAW w32FindData;

yaya::string_t tmp_str = enumpath + L"\\*.*";
char *s_filestr = Ccct::Ucs2ToMbcs(tmp_str, CHARSET_DEFAULT);
if ( ! s_filestr ) { return false; }
dh = ::FindFirstFileW(tmp_str.c_str(),&w32FindData);

dh = ::FindFirstFile(s_filestr,&w32FindData);
free(s_filestr);
s_filestr = NULL;
if ( dh == INVALID_HANDLE_VALUE ) { return false; }

if ( dh == INVALID_HANDLE_VALUE ) { return false; }
is_init = true;

is_init = true;
name_w = w32FindData.cFileName;
isdir = (w32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
else {
WIN32_FIND_DATAA w32FindData;

char *s_filestr = Ccct::Ucs2ToMbcs(tmp_str, CHARSET_DEFAULT);
if ( ! s_filestr ) { return false; }

name = w32FindData.cFileName;
isdir = (w32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
dh = ::FindFirstFileA(s_filestr,&w32FindData);
free(s_filestr);
s_filestr = NULL;

if ( dh == INVALID_HANDLE_VALUE ) { return false; }

is_init = true;

name_a = w32FindData.cFileName;
isdir = (w32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
}

#elif defined(POSIX)

Expand All @@ -83,44 +104,65 @@ bool CDirEnum::next(CDirEnumEntry &entry)

is_init = true;

name = ent->d_name;
name_a = ent->d_name;
isdir = ent->d_type == DT_DIR;

#endif
}
else {
#if defined(WIN32)

WIN32_FIND_DATA w32FindData;
if ( ::FindNextFile(dh,&w32FindData) == 0 ) { return false; }
if ( isUnicode ) {
WIN32_FIND_DATAW w32FindData;
if ( ::FindNextFileW(dh,&w32FindData) == 0 ) { return false; }

name_w = w32FindData.cFileName;
isdir = (w32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
else {
WIN32_FIND_DATAA w32FindData;
if ( ::FindNextFileA(dh,&w32FindData) == 0 ) { return false; }

name = w32FindData.cFileName;
isdir = (w32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
name_a = w32FindData.cFileName;
isdir = (w32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
}

#elif defined(POSIX)

struct dirent* ent = readdir(dh);
if ( ! ent ) { return false; }

name = ent->d_name;
name_a = ent->d_name;
isdir = ent->d_type == DT_DIR;

#endif
}

if (name != "." && name != "..") {
break;
if ( isUnicode ) {
if (name_w != L"." && name_w != L"..") {
break;
}
}
else {
if (name_a != "." && name_a != "..") {
break;
}
}
}

#if defined(WIN32)
yaya::char_t *t_wfile = Ccct::MbcsToUcs2(name, CHARSET_DEFAULT);
if (! t_wfile ) { return false; }
if ( isUnicode ) {
entry.name = name_w;
}
else {
yaya::char_t *t_wfile = Ccct::MbcsToUcs2(name_a, CHARSET_DEFAULT);
if (! t_wfile ) { return false; }

entry.name = t_wfile;
free(t_wfile);
entry.name = t_wfile;
free(t_wfile);
}
#elif defined(POSIX)
entry.name = widen(name);
entry.name = widen(name_a);
#endif

entry.isdir = isdir;
Expand Down
8 changes: 1 addition & 7 deletions file1.cpp
Expand Up @@ -42,13 +42,7 @@ int CFile1::Open(void)
if (fp != NULL)
return 1;

char *filepath = Ccct::Ucs2ToMbcs(name, CHARSET_DEFAULT);
if (filepath == NULL)
return 0;

fp = yaya::w_fopen((wchar_t *)name.c_str(), (wchar_t *)mode.c_str());
free(filepath);
filepath = NULL;
fp = yaya::w_fopen(name.c_str(), (wchar_t *)mode.c_str());

if ( ! fp ) {
size = 0;
Expand Down
4 changes: 2 additions & 2 deletions log.cpp
Expand Up @@ -100,7 +100,7 @@ void CLog::Start(const yaya::string_t &p, int cs, HWND hw, char il)
if (fileen) {
char *tmpstr = Ccct::Ucs2ToMbcs(str, charset);
if (tmpstr != NULL) {
FILE *fp = yaya::w_fopen((yaya::char_t *)path.c_str(), L"w");
FILE *fp = yaya::w_fopen(path.c_str(), L"w");
if (fp != NULL) {
/* if (charset == CHARSET_UTF8)
write_utf8bom(fp);*/
Expand Down Expand Up @@ -181,7 +181,7 @@ void CLog::Write(const yaya::char_t *str, int mode)
if (! path.empty()) {
char *tmpstr = Ccct::Ucs2ToMbcs(cstr, charset);
if (tmpstr != NULL) {
FILE *fp = yaya::w_fopen((yaya::char_t *)path.c_str(), L"a");
FILE *fp = yaya::w_fopen(path.c_str(), L"a");
if (fp != NULL) {
fprintf(fp, "%s", tmpstr);
fclose(fp);
Expand Down
30 changes: 24 additions & 6 deletions misc.cpp
Expand Up @@ -823,17 +823,35 @@ char IsLegalPlainStrLiteral(const yaya::string_t &str)
}

/* -----------------------------------------------------------------------
* 関数名 : IsNt
* 機能概要: 0/1=9x系/NT系 を返します
* 関数名 : IsUnicodeAware
* 機能概要: Unicode系APIが使えるかどうかを返します
* POSIXは常にtrue / Win9x/Meのみfalse
* -----------------------------------------------------------------------
*/
#if defined(WIN32) || defined(_WIN32_WCE)
char IsNt(void)
class IsUnicodeAwareHelper
{
OSVERSIONINFO osi = { sizeof(OSVERSIONINFO) };
public:
bool isnt;

GetVersionEx(&osi);
return (osi.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 1 : 0;
IsUnicodeAwareHelper() {
OSVERSIONINFO osVer;
osVer.dwOSVersionInfoSize = sizeof(osVer);

::GetVersionEx(&osVer);
isnt = (osVer.dwPlatformId == VER_PLATFORM_WIN32_NT);
}
};

bool IsUnicodeAware(void)
{
static IsUnicodeAwareHelper h;
return h.isnt;
}
#else
bool IsUnicodeAware(void)
{
return true;
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion misc.h
Expand Up @@ -58,7 +58,7 @@ char IsLegalVariableName(const yaya::string_t &str);
char IsLegalStrLiteral(const yaya::string_t &str);
char IsLegalPlainStrLiteral(const yaya::string_t &str);

char IsNt(void);
bool IsUnicodeAware(void);

void EscapeString(yaya::string_t &wstr);
void UnescapeString(yaya::string_t &wstr);
Expand Down
2 changes: 1 addition & 1 deletion parser0.cpp
Expand Up @@ -254,7 +254,7 @@ char CParser0::LoadDictionary1(const yaya::string_t& filename, std::vector<CDefi
fix_filepath(file);
#endif
// ファイルを開く
FILE *fp = yaya::w_fopen((wchar_t *)file.c_str(), L"r");
FILE *fp = yaya::w_fopen(file.c_str(), L"r");
if (fp == NULL) {
vm.logger().Error(E_E, 5, file);
return 2;
Expand Down
4 changes: 2 additions & 2 deletions value.h
Expand Up @@ -167,7 +167,7 @@ class CValue
m_array = rhs.m_array;
}
else {
m_array.reset((CValueArray*)NULL);
m_array.reset();
if ( type == F_TAG_STRING ) {
s_value = rhs.s_value;
}
Expand Down Expand Up @@ -333,7 +333,7 @@ class CValue
return *m_array;
}
inline void array_clear(void) {
m_array.reset((CValueArray*)NULL);
m_array.reset();
}
};

Expand Down
36 changes: 21 additions & 15 deletions wsex.cpp
Expand Up @@ -166,29 +166,35 @@ void yaya::ws_replace(yaya::string_t &str, const wchar_t *before, const wchar_t
* -----------------------------------------------------------------------
*/
#if defined(WIN32) || defined(_WIN32_WCE)
FILE *yaya::w_fopen(const wchar_t *fname, const wchar_t *mode)
FILE *yaya::w_fopen(const yaya::char_t *fname, const yaya::char_t *mode)
{
// ファイル名とオープンモードををMBCSへ変換
char *mfname = Ccct::Ucs2ToMbcs(fname, CHARSET_DEFAULT);
if (mfname == NULL)
return NULL;
char *mmode = Ccct::Ucs2ToMbcs(mode, CHARSET_DEFAULT);
if (mmode == NULL) {
FILE *fp;
if ( IsUnicodeAware() ) {
fp = _wfopen(fname,mode);
}
else {
// ファイル名とオープンモードををMBCSへ変換
char *mfname = Ccct::Ucs2ToMbcs(fname, CHARSET_DEFAULT);
if (mfname == NULL)
return NULL;
char *mmode = Ccct::Ucs2ToMbcs(mode, CHARSET_DEFAULT);
if (mmode == NULL) {
free(mfname);
mfname = NULL;
return NULL;
}
// オープン
fp = fopen(mfname, mmode);
free(mfname);
mfname = NULL;
return NULL;
free(mmode);
mmode = NULL;
}
// オープン
FILE *fp = fopen(mfname, mmode);
free(mfname);
mfname = NULL;
free(mmode);
mmode = NULL;

return fp;
}
#else
FILE* yaya::w_fopen(const wchar_t* fname, const wchar_t* mode) {
FILE* yaya::w_fopen(const yaya::char_t* fname, const yaya::char_t* mode) {
std::string s_fname = narrow(yaya::string_t(fname));
std::string s_mode = narrow(yaya::string_t(mode));

Expand Down
2 changes: 1 addition & 1 deletion wsex.h
Expand Up @@ -30,7 +30,7 @@ namespace yaya {
void ws_eraseend(yaya::string_t &str, wchar_t c);
void ws_replace(yaya::string_t &str, const wchar_t *before, const wchar_t *after, int count = 0);

FILE *w_fopen(const wchar_t *fname, const wchar_t *mode);
FILE *w_fopen(const yaya::char_t *fname, const yaya::char_t *mode);
//void write_utf8bom(FILE *fp);

int ws_fgets(yaya::string_t &str, FILE *stream, int charset, int ayc, int lc, int cutspace = true);
Expand Down

0 comments on commit 7019d2f

Please sign in to comment.