Пример #1
0
int CFolderItem::FolderCreateDirectory(int showFolder)
{
	int res = FOLDER_SUCCESS;
	if (IsUnicode())
		{
			wchar_t buffer[MAX_FOLDER_SIZE];
			if (szFormatW)
				{
					ExpandPathW(buffer, szFormatW, MAX_FOLDER_SIZE);
					CreateDirectories(buffer);
					if (showFolder)
						{
							ShellExecuteW(NULL, L"explore", buffer, NULL, NULL, SW_SHOW);
						}
					res = (DirectoryExists(buffer)) ? FOLDER_SUCCESS : FOLDER_FAILURE;
				}
		}
		else{
			char buffer[MAX_FOLDER_SIZE];
			if (szFormat)
				{
					ExpandPath(buffer, szFormat, MAX_FOLDER_SIZE);
					CreateDirectories(buffer);
					if (showFolder)
						{
							ShellExecuteA(NULL, "explore", buffer, NULL, NULL, SW_SHOW);
						}
					res = (DirectoryExists(buffer)) ? FOLDER_SUCCESS : FOLDER_FAILURE;
				}
		}
	return res;
}
Пример #2
0
DWORD LStrList::SaveToFile(LTxtFile* tf, BOOL bIncludeNull)
{
    DWORD ret = 0;
    LAutoLock al(m_lock);

    LIterator it = GetHeadIterator();
    if (IsUnicode())
    {
        while (NULL != it)
        {
            PCWSTR str = (PCWSTR)GetRawString(it);
            if (L'\0' != *str || bIncludeNull)
            {
                tf->WriteLn(str);
                ++ret;
            }
            it = GetNextIterator(it);
        }
    }
    else
    {
        while (NULL != it)
        {
            PCSTR str = (PCSTR)GetRawString(it);
            if ('\0' != *str || bIncludeNull)
            {
                tf->WriteLn(str);
                ++ret;
            }
            it = GetNextIterator(it);
        }
    }

    return ret;
}
Пример #3
0
void fileInfo(TCHAR* fileName) {

	HANDLE h = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);

	if (h != IHV)
	{
		DWORD dwSize = GetFileSize(h, 0);
		PBYTE buf = new BYTE[dwSize];
		DWORD dwCount;
		ReadFile(h, buf, dwSize, &dwCount, 0);
		CloseHandle(h);

		BOOL isUnicode = IsUnicode(buf);
		_tprintf(_T("FileName: %s\nFileSize: %d\nFileEncoding %s\n"), fileName, dwSize, (isUnicode ? _T("UNICODE") : _T("ANSI")));
		if (isUnicode)
		{
			wchar_t* buf_wc = (wchar_t*)buf;
			buf_wc++;
			countStrings(buf_wc, dwSize / 2 - 1);
			delete[] --buf_wc;
		}
		else
		{
			char* buf_c = (char *)buf;
			countStrings(buf_c, dwSize);
			delete[] buf_c;
		}
		printf("\n");
	}
}
Пример #4
0
/**
 * @brief Open the XML file.
 * @param [in] filename Filename (and path) of the file to open.
 * @param [in] mode access mode.
 * @return true if succeeds, false otherwise.
 */
bool UniMarkdownFile::DoOpen(const String& filename, AccessMode mode)
{
	m_depth = 0;
	bool bOpen = UniMemFile::DoOpen(filename, mode);
	if (bOpen)
	{
		// CMarkdown wants octets, so we may need to transcode to UTF8.
		// As transcoding strips the BOM, we must check for it in advance.
		if (IsUnicode())
			m_codepage = ucr::CP_UTF_8;
		// The CMarkdown::File constructor cares about transcoding.
		CMarkdown::File f(
			reinterpret_cast<const TCHAR *>(m_base),
			static_cast<unsigned>(m_filesize),
			CMarkdown::File::Mapping | CMarkdown::File::Octets);
		// The file mapping may have been recreated due to transcoding.
		m_data = m_current = m_base = reinterpret_cast<unsigned char *>(f.pImage);
		m_filesize = f.cbImage;
		// Prevent the CMarkdown::File destructor from unmapping the view.
		f.pImage = nullptr;
		m_pMarkdown.reset(new CMarkdown(f));
		Move();
	}
	return bOpen;
}
Пример #5
0
/**
 * @brief Open the XML file.
 * @param [in] filename Filename (and path) of the file to open.
 * @param [in] dwOpenAccess File access mode.
 * @param [in] dwOpenShareMode File's share mode when opened.
 * @param [in] dwOpenCreationDispostion Overwrite existing file?
 * @param [in] dwMappingProtect
 * @param [in] dwMapViewAccess
 * @return true if succeeds, false otherwise.
 */
bool UniMarkdownFile::DoOpen(LPCTSTR filename, DWORD dwOpenAccess,
		DWORD dwOpenShareMode, DWORD dwOpenCreationDispostion,
		DWORD dwMappingProtect, DWORD dwMapViewAccess)
{
	m_depth = 0;
	bool bOpen = UniMemFile::DoOpen(filename, dwOpenAccess, dwOpenShareMode,
			dwOpenCreationDispostion, dwMappingProtect, dwMapViewAccess);
	if (bOpen)
	{
		// CMarkdown wants octets, so we may need to transcode to UTF8.
		// As transcoding strips the BOM, we must check for it in advance.
		if (IsUnicode())
			m_codepage = CP_UTF8;
		// The CMarkdown::File constructor cares about transcoding.
		CMarkdown::File f(
			reinterpret_cast<LPCTSTR>(m_base),
			static_cast<DWORD>(m_filesize),
			CMarkdown::File::Mapping | CMarkdown::File::Octets);
		// The file mapping may have been recreated due to transcoding.
		m_data = m_current = m_base = reinterpret_cast<LPBYTE>(f.pImage);
		m_filesize = f.cbImage;
		// Prevent the CMarkdown::File destructor from unmapping the view.
		f.pImage = NULL;
		m_pMarkdown = new CMarkdown(f);
		Move();
	}
	return bOpen;
}
Пример #6
0
void CFolderItem::Expand(char *buffer, int size)
{
	if (IsUnicode())
		{
			ExpandPathW((wchar_t *) buffer, szFormatW, size);
		}
		else{
			ExpandPath(buffer, szFormat, size);
		}
}
void GetStoredPasswords(void)
{
	HRESULT hRes;
	HMODULE hPstoreDLL;
	IPStorePtr spPStore;
	IEnumPStoreTypesPtr spEnumTypes, spEnumSubTypes;
	IEnumPStoreItemsPtr spEnumItems;
	GUID typeGUID, subtypeGUID;
	LPWSTR szItemName;
	unsigned long pcbData = 0;
	unsigned char *ppbData = NULL;
	_PST_PROMPTINFO *pi = NULL;
	char szPw[512]="";

	hPstoreDLL = LoadLibrary("pstorec.dll");
	PStoreCreateInstancePtr PStoreCreateInstance = (PStoreCreateInstancePtr)GetProcAddress(hPstoreDLL, "PStoreCreateInstance");

	hRes = PStoreCreateInstance(&spPStore, 0, 0, 0);
	hRes = spPStore->EnumTypes(0, 0, &spEnumTypes);

	while(spEnumTypes->raw_Next(1, &typeGUID, 0) == S_OK)
	{
		printf("TypeGUID = %.8x\r\n", typeGUID);
		
		hRes = spPStore->EnumSubtypes(0, &typeGUID, 0, &spEnumSubTypes);		

		while(spEnumSubTypes->raw_Next(1, &subtypeGUID, 0) == S_OK)
		{
			printf("\tSubtypeGUID = %.8x\r\n", subtypeGUID);
			
			hRes = spPStore->EnumItems(0, &typeGUID, &subtypeGUID, 0, &spEnumItems);

			while(spEnumItems->raw_Next(1, &szItemName, 0) == S_OK)
			{
				printf("\t\tItemName = %ws\r\n", szItemName);
				
				pcbData = 0;
				ppbData = NULL;
				pi = NULL;

				hRes = spPStore->ReadItem(0, &typeGUID, &subtypeGUID, szItemName, &pcbData, &ppbData, pi, 0);

				if(IsUnicode(ppbData, pcbData))
				{
					printf("\t\tItemData = %ws\r\n",ppbData);					
				}
				else
				{
					printf("\t\tItemData = %s\r\n",ppbData);
				}
			}
		}
	}
}
Пример #8
0
BOOL LStrList::IsEmptyLine(__in LIterator it)
{
    PVOID pv = GetRawString(it);
    if (NULL == pv)
        return TRUE;

    if (IsUnicode())
        return L'\0' == *((PCWSTR)pv);
    else
        return '\0' == *((PCSTR)pv);
}
Пример #9
0
void LStrList::SetAt(__in LIterator it, __in PCWSTR lpString)
{
    if (IsUnicode())
    {
        LPtrList::SetAt(it, &lpString);
    }
    else
    {
        LStringA str = lpString;
        SetAt(it, str);
    }
}
Пример #10
0
BOOL LStrList::GetAt(__in LIterator it, __out LStringW* str)
{
    PVOID pv = GetRawString(it);
    if (NULL == pv)
        return FALSE;

    if (IsUnicode())
        str->Copy((PCWSTR)pv);
    else
        str->Copy((PCSTR)pv);
    return TRUE;
}
Пример #11
0
CFolderItem::~CFolderItem()
{
//	WriteDataToDatabase();
	if (IsUnicode())
		{
			MyFree(szFormatW);
			MyFree(szOldFormatW);
		}
		else{
			MyFree(szFormat);
			MyFree(szOldFormat);
		}
}
Пример #12
0
LIterator LStrList::AddHead(__in PCSTR lpString)
{
    LAutoLock al(m_lock);
    if (IsUnicode())
    {
        LStringW str = lpString;
        AddHead(str);
    }
    else
    {
        LPtrList::AddHead(&lpString);
    }
    return m_itHead;
}
Пример #13
0
LIterator LStrList::AddTail(__in PCWSTR lpString)
{
    LAutoLock al(m_lock);
    if (IsUnicode())
    {
        LPtrList::AddTail(&lpString);
    }
    else
    {
        LStringA str = lpString;
        AddTail(str);
    }
    return m_itTail;
}
Пример #14
0
DWORD LStrList::LoadFromFile(LTxtFile* tf, BOOL bIncludeNull)
{
    LAutoLock al(m_lock);

    // 如果列表为空,则按照来源文件编码
    if (LPtrList::IsEmpty())
    {
        m_dwFlags = 0;
        if (tf->IsUnicode())
        {
            m_dwFlags = SLFLAG_UNICODE;
            m_pfnCopy = LStrList_CopyW;
            m_pfnDestroy = LStrList_DestroyW;
        }
        else
        {
            m_pfnCopy = LStrList_CopyA;
            m_pfnDestroy = LStrList_DestroyA;
        }
    }

    DWORD ret = 0;
    if (IsUnicode())
    {
        LStringW str;
        while (!tf->Eof())
        {
            tf->ReadLn(&str);
            if ((L'\0' != str[0]) || bIncludeNull)
            {
                AddTail(str);
                ++ret;
            }
        }
    }
    else
    {
        LStringA str;
        while (!tf->Eof())
        {
            tf->ReadLn(&str);
            if (('\0' != str[0]) || bIncludeNull)
            {
                AddTail(str);
                ++ret;
            }
        }
    }
    return ret;
}
Пример #15
0
    void RegexPattern::Print(DebugWriter* w)
    {
        w->Print(L"/");

        Js::InternalString str = GetSource();
        if (str.GetLength() == 0)
            w->Print(L"(?:)");
        else
        {
            for (charcount_t i = 0; i < str.GetLength(); ++i)
            {
                const wchar_t c = str.GetBuffer()[i];
                switch(c)
                {
                case L'/':
                    w->Print(L"\\%lc", c);
                    break;
                case L'\n':
                case L'\r':
                case L'\x2028':
                case L'\x2029':
                    w->PrintEscapedChar(c);
                    break;
                case L'\\':
                    Assert(i + 1 < str.GetLength()); // cannot end in a '\'
                    w->Print(L"\\%lc", str.GetBuffer()[++i]);
                    break;
                default:
                    w->PrintEscapedChar(c);
                    break;
                }
            }
        }
        w->Print(L"/");
        if (IsIgnoreCase())
            w->Print(L"i");
        if (IsGlobal())
            w->Print(L"g");
        if (IsMultiline())
            w->Print(L"m");
        if (IsUnicode())
            w->Print(L"u");
        if (IsSticky())
            w->Print(L"y");
        w->Print(L" /* ");
        w->Print(L", ");
        w->Print(isLiteral ? L"literal" : L"dynamic");
        w->Print(L" */");
    }
Пример #16
0
    void RegexPattern::Print(DebugWriter* w)
    {
        w->Print(_u("/"));

        Js::InternalString str = GetSource();
        if (str.GetLength() == 0)
            w->Print(_u("(?:)"));
        else
        {
            for (charcount_t i = 0; i < str.GetLength(); ++i)
            {
                const char16 c = str.GetBuffer()[i];
                switch(c)
                {
                case _u('/'):
                    w->Print(_u("\\%lc"), c);
                    break;
                case _u('\n'):
                case _u('\r'):
                case _u('\x2028'):
                case _u('\x2029'):
                    w->PrintEscapedChar(c);
                    break;
                case _u('\\'):
                    Assert(i + 1 < str.GetLength()); // cannot end in a '\'
                    w->Print(_u("\\%lc"), str.GetBuffer()[++i]);
                    break;
                default:
                    w->PrintEscapedChar(c);
                    break;
                }
            }
        }
        w->Print(_u("/"));
        if (IsIgnoreCase())
            w->Print(_u("i"));
        if (IsGlobal())
            w->Print(_u("g"));
        if (IsMultiline())
            w->Print(_u("m"));
        if (IsUnicode())
            w->Print(_u("u"));
        if (IsSticky())
            w->Print(_u("y"));
        w->Print(_u(" /* "));
        w->Print(_u(", "));
        w->Print(isLiteral ? _u("literal") : _u("dynamic"));
        w->Print(_u(" */"));
    }
Пример #17
0
// 
// Returns true if the server is case-insensitive
// Might throw exception if the information is not available yet
//
int P4ClientAPI::GetServerUnicode() 
{
    if( !IsConnected() ) {
		return Except( "P4:run", "Not connected to a Perforce server");
    }
    
    if( !IsCmdRun() ) {
		lua_pushnil( L );
		Run( "info", 0, 0 );
		lua_pop( L, 1 );
	}

    lua_pushboolean( L, IsUnicode() );
	return 1;
}
Пример #18
0
LIterator LStrList::InsertBefore(__in LIterator it, __in PCWSTR lpString)
{
    LIterator ret = NULL;
    LAutoLock al(m_lock);
    if (IsUnicode())
    {
        ret = LPtrList::InsertBefore(it, &lpString);
    }
    else
    {
        LStringA str = lpString;
        ret = InsertBefore(it, str);
    }
    return ret;
}
Пример #19
0
void CFolderItem::GetDataFromDatabase(const char *szNotFound)
{
	char name[256];
	strcpy(name, szSection);
	strcat(name, szName);
	
	if (IsUnicode())
		{
			wchar_t buffer[MAX_FOLDER_SIZE];
			GetStringFromDatabase(name, (const wchar_t *) szNotFound, buffer, MAX_FOLDER_SIZE);
			SetFormatW(buffer);
		}
		else{
			char buffer[MAX_FOLDER_SIZE];
			GetStringFromDatabase(name, szNotFound, buffer, MAX_FOLDER_SIZE);
			SetFormat(buffer);
		}
}
Пример #20
0
DWORD LStrList::SaveToFile(__in PCWSTR lpFile, __in DWORD dwFlags)
{
    if (NULL == m_itHead)
        return 0;

    LTxtFile::MODE mode;
    BOOL bUnicode = IsUnicode();
    if (SLFILE_CLEAR & dwFlags)
        mode = LTxtFile::modeReset;
    else
        mode = LTxtFile::modeAppend;

    LTxtFile file(bUnicode);
    if (!file.Open(lpFile, mode))
        return 0;

    BOOL bIncludeNull = (0 != (SLFILE_INCLUDENULL & dwFlags));
    return SaveToFile(&file, bIncludeNull);
}
Пример #21
0
void CFolderItem::WriteDataToDatabase()
{
	char name[256];
	strcpy(name, szSection);
	strcat(name, szName);

	if (IsUnicode())
		{
			if (szFormatW)
				{
					WriteStringToDatabase(name, szFormatW);
				}
		}
		else{
			if (szFormat)
				{
					WriteStringToDatabase(name, szFormat);
				}
		}
}
Пример #22
0
int CFolderItem::FolderDeleteOldDirectory(int showFolder)
{
	int res = FOLDER_SUCCESS;
	if (IsUnicode())
		{
			wchar_t buffer[MAX_FOLDER_SIZE];
			if (szOldFormatW)
				{
					if (wcscmp(szFormatW, szOldFormatW) == 0) //format wasn't changed
						{
							return res;
						}
					ExpandPathW(buffer, szOldFormatW, MAX_FOLDER_SIZE);
					RemoveDirectories(buffer);
					res = (DirectoryExists(buffer)) ? FOLDER_FAILURE : FOLDER_SUCCESS;
					if ((res == FOLDER_FAILURE) && (showFolder))
						{
							ShellExecuteW(NULL, L"explore", buffer, NULL, NULL, SW_SHOW);
						}
				}
		}
		else{
			char buffer[MAX_FOLDER_SIZE];
			if (szOldFormat)
				{
					if (strcmp(szFormat, szOldFormat) == 0) //format wasn't changed
						{
							return res;
						}
					ExpandPath(buffer, szOldFormat, MAX_FOLDER_SIZE);
					RemoveDirectories(buffer);
					res = (DirectoryExists(buffer)) ? FOLDER_FAILURE : FOLDER_SUCCESS;
					if ((res == FOLDER_FAILURE) && (showFolder))
						{
							ShellExecuteA(NULL, "explore", buffer, NULL, NULL, SW_SHOW);
						}
				}
		}
	return res;
}
Пример #23
0
bool ReadTextFile(
  const char *Name,
  const wchar *NameW,
  StringList *List,
  bool Config,
  bool AbortOnError,
  RAR_CHARSET SrcCharset,
  bool Unquote,
  bool SkipComments,
  bool ExpandEnvStr)
{
  char FileName[NM];
  *FileName=0;
  if (Name!=NULL)
  {
    if (Config)
      GetConfigName(Name,FileName,true);
    else
      strcpy(FileName,Name);
  }

  wchar FileNameW[NM];
  *FileNameW=0;

#ifdef _WIN_ALL
  if (NameW!=NULL)
  {
    if (Config)
      GetConfigName(NameW,FileNameW,true);
    else
      wcscpy(FileNameW,NameW);
  }
#endif

  File SrcFile;
  if ((FileName!=NULL && *FileName!=0) || (FileNameW!=NULL && *FileNameW!=0))
  {
    bool OpenCode=AbortOnError ? SrcFile.WOpen(FileName,FileNameW):SrcFile.Open(FileName,FileNameW,0);

    if (!OpenCode)
    {
      if (AbortOnError)
        ErrHandler.Exit(RARX_OPEN);
      return(false);
    }
  }
  else
    SrcFile.SetHandleType(FILE_HANDLESTD);

  unsigned int DataSize=0,ReadSize;
  const int ReadBlock=1024;
  Array<char> Data(ReadBlock+5);
  while ((ReadSize=SrcFile.Read(&Data[DataSize],ReadBlock))!=0)
  {
    DataSize+=ReadSize;
    Data.Add(ReadSize);
  }

  memset(&Data[DataSize],0,5);

  if ((SrcCharset==RCH_UNICODE) ||
      (SrcCharset==RCH_DEFAULT && IsUnicode((byte *)&Data[0],DataSize)))
  {
    // Unicode in native system format, can be more than 2 bytes per character.
    Array<wchar> DataW(Data.Size()/2+1);
    for (size_t I=2;I<Data.Size()-1;I+=2)
    {
      // Need to convert Data to (byte) first to prevent the sign extension
      // to higher bytes.
      DataW[(I-2)/2]=(wchar)((byte)Data[I])+(wchar)((byte)Data[I+1])*256;
    }

    wchar *CurStr=&DataW[0];
    Array<char> AnsiName;

    while (*CurStr!=0)
    {
      wchar *NextStr=CurStr,*CmtPtr=NULL;
      while (*NextStr!='\r' && *NextStr!='\n' && *NextStr!=0)
      {
        if (SkipComments && NextStr[0]=='/' && NextStr[1]=='/')
        {
          *NextStr=0;
          CmtPtr=NextStr;
        }
        NextStr++;
      }
      *NextStr=0;
      for (wchar *SpacePtr=(CmtPtr ? CmtPtr:NextStr)-1;SpacePtr>=CurStr;SpacePtr--)
      {
        if (*SpacePtr!=' ' && *SpacePtr!='\t')
          break;
        *SpacePtr=0;
      }
      if (*CurStr)
      {
        // Length and AddSize must be defined as signed, because AddSize
        // can be negative.
        int Length=(int)wcslen(CurStr);
        int AddSize=4*(Length-(int)AnsiName.Size()+1);

        if (AddSize>0)
          AnsiName.Add(AddSize);
        if (Unquote && *CurStr=='\"' && CurStr[Length-1]=='\"')
        {
          CurStr[Length-1]=0;
          CurStr++;
        }
        WideToChar(CurStr,&AnsiName[0],AnsiName.Size());

        bool Expanded=false;
#if defined(_WIN_ALL) && !defined(_WIN_CE)
        if (ExpandEnvStr && *CurStr=='%')
        {
          // Expanding environment variables in Windows version.

          char ExpName[NM];
          wchar ExpNameW[NM];
          *ExpNameW=0;
          int ret,retw=1;
          ret=ExpandEnvironmentStringsA(&AnsiName[0],ExpName,ASIZE(ExpName));
          if (ret!=0 && WinNT())
            retw=ExpandEnvironmentStringsW(CurStr,ExpNameW,ASIZE(ExpNameW));
          Expanded=ret!=0 && ret<ASIZE(ExpName) &&
                   retw!=0 && retw<ASIZE(ExpNameW);
          if (Expanded)
            List->AddString(ExpName,ExpNameW);
        }
#endif
        if (!Expanded)
          List->AddString(&AnsiName[0],CurStr);
      }
      CurStr=NextStr+1;
      while (*CurStr=='\r' || *CurStr=='\n')
        CurStr++;
    }
  }
  else
  {
    char *CurStr=&Data[0];
    while (*CurStr!=0)
    {
      char *NextStr=CurStr,*CmtPtr=NULL;
      while (*NextStr!='\r' && *NextStr!='\n' && *NextStr!=0)
      {
        if (SkipComments && NextStr[0]=='/' && NextStr[1]=='/')
        {
          *NextStr=0;
          CmtPtr=NextStr;
        }
        NextStr++;
      }
      *NextStr=0;
      for (char *SpacePtr=(CmtPtr ? CmtPtr:NextStr)-1;SpacePtr>=CurStr;SpacePtr--)
      {
        if (*SpacePtr!=' ' && *SpacePtr!='\t')
          break;
        *SpacePtr=0;
      }
      if (*CurStr)
      {
        if (Unquote && *CurStr=='\"')
        {
          size_t Length=strlen(CurStr);
          if (CurStr[Length-1]=='\"')
          {
            CurStr[Length-1]=0;
            CurStr++;
          }
        }
#if defined(_WIN_ALL)
        if (SrcCharset==RCH_OEM)
          OemToCharA(CurStr,CurStr);
#endif

        bool Expanded=false;
#if defined(_WIN_ALL) && !defined(_WIN_CE)
        if (ExpandEnvStr && *CurStr=='%')
        {
          // Expanding environment variables in Windows version.
          char ExpName[NM];
          int ret=ExpandEnvironmentStringsA(CurStr,ExpName,ASIZE(ExpName));
          Expanded=ret!=0 && ret<ASIZE(ExpName);
          if (Expanded)
            List->AddString(ExpName);
        }
#endif
        if (!Expanded)
          List->AddString(CurStr);
      }
      CurStr=NextStr+1;
      while (*CurStr=='\r' || *CurStr=='\n')
        CurStr++;
    }
  }
  return(true);
}
Пример #24
0
bool ReadTextFile(char *Name,StringList *List,bool Config,bool AbortOnError,
                  bool ConvertToAnsi,bool Unquote,bool SkipComments)
{
  char FileName[NM];
  if (Config)
    GetConfigName(Name,FileName,true);
  else
    strcpy(FileName,Name);

  File SrcFile;
  if (*FileName)
  {
    bool OpenCode=AbortOnError ? SrcFile.WOpen(FileName):SrcFile.Open(FileName);

    if (!OpenCode)
    {
      if (AbortOnError)
        ErrHandler.Exit(OPEN_ERROR);
      return(false);
    }
  }
  else
    SrcFile.SetHandleType(FILE_HANDLESTD);

  unsigned int DataSize=0,ReadSize;
  const int ReadBlock=1024;
  Array<char> Data(ReadBlock+5);
  while ((ReadSize=SrcFile.Read(&Data[DataSize],ReadBlock))!=0)
  {
    DataSize+=ReadSize;
    Data.Add(ReadSize);
  }

  memset(&Data[DataSize],0,5);

  if (IsUnicode((byte *)&Data[0],DataSize))
  {
    wchar *CurStr=(wchar *)&Data[2];
    Array<char> AnsiName;

    while (*CurStr!=0)
    {
      wchar *NextStr=CurStr,*CmtPtr=NULL;
      while (*NextStr!='\r' && *NextStr!='\n' && *NextStr!=0)
      {
        if (SkipComments && NextStr[0]=='/' && NextStr[1]=='/')
        {
          *NextStr=0;
          CmtPtr=NextStr;
        }
        NextStr++;
      }
      *NextStr=0;
      for (wchar *SpacePtr=(CmtPtr ? CmtPtr:NextStr)-1;SpacePtr>=CurStr;SpacePtr--)
      {
        if (*SpacePtr!=' ' && *SpacePtr!='\t')
          break;
        *SpacePtr=0;
      }
      if (*CurStr)
      {
        int Length=strlenw(CurStr);
        int AddSize=Length-AnsiName.Size()+1;
        if (AddSize>0)
          AnsiName.Add(AddSize);
        if (Unquote && *CurStr=='\"' && CurStr[Length-1]=='\"')
        {
          CurStr[Length-1]=0;
          CurStr++;
        }
        WideToChar(CurStr,&AnsiName[0]);
        List->AddString(&AnsiName[0],CurStr);
      }
      CurStr=NextStr+1;
      while (*CurStr=='\r' || *CurStr=='\n')
        CurStr++;
    }
  }
  else
  {
    char *CurStr=&Data[0];
    while (*CurStr!=0)
    {
      char *NextStr=CurStr,*CmtPtr=NULL;
      while (*NextStr!='\r' && *NextStr!='\n' && *NextStr!=0)
      {
        if (SkipComments && NextStr[0]=='/' && NextStr[1]=='/')
        {
          *NextStr=0;
          CmtPtr=NextStr;
        }
        NextStr++;
      }
      *NextStr=0;
      for (char *SpacePtr=(CmtPtr ? CmtPtr:NextStr)-1;SpacePtr>=CurStr;SpacePtr--)
      {
        if (*SpacePtr!=' ' && *SpacePtr!='\t')
          break;
        *SpacePtr=0;
      }
      if (*CurStr)
      {
        if (Unquote && *CurStr=='\"')
        {
          int Length=strlen(CurStr);
          if (CurStr[Length-1]=='\"')
          {
            CurStr[Length-1]=0;
            CurStr++;
          }
        }
#if defined(_WIN_32) && !defined(_XBOX) && !defined(_LINUX)
        if (ConvertToAnsi)
          OemToChar(CurStr,CurStr);
#endif
        List->AddString(CurStr);
      }
      CurStr=NextStr+1;
      while (*CurStr=='\r' || *CurStr=='\n')
        CurStr++;
    }
  }
  return(true);
}
Пример #25
0
bool ReadTextFile(char *Name,StringList *List,bool Config,bool AbortOnError,
                  RAR_CHARSET SrcCharset,bool Unquote,bool SkipComments)
{
  char FileName[NM];
  if (Config)
    GetConfigName(Name,FileName,true);
  else
    strcpy(FileName,Name);

  File SrcFile;
  if (*FileName)
  {
    bool OpenCode=AbortOnError ? SrcFile.WOpen(FileName):SrcFile.Open(FileName);

    if (!OpenCode)
    {
      if (AbortOnError)
        ErrHandler.Exit(OPEN_ERROR);
      return(false);
    }
  }
  else
    SrcFile.SetHandleType(FILE_HANDLESTD);

  unsigned int DataSize=0,ReadSize;
  const int ReadBlock=1024;
  Array<char> Data(ReadBlock+5);
  while ((ReadSize=SrcFile.Read(&Data[DataSize],ReadBlock))!=0)
  {
    DataSize+=ReadSize;
    Data.Add(ReadSize);
  }

  memset(&Data[DataSize],0,5);

  if (SrcCharset==RCH_UNICODE ||
      SrcCharset==RCH_DEFAULT && IsUnicode((byte *)&Data[0],DataSize))
  {
    // Unicode in native system format, can be more than 2 bytes per character
    Array<wchar> DataW(Data.Size()/2+1);
    for (int I=2;I<Data.Size()-1;I+=2)
      DataW[(I-2)/2]=(wchar)Data[I]+(wchar)Data[I+1]*256;

    wchar *CurStr=&DataW[0];
    Array<char> AnsiName;

    while (*CurStr!=0)
    {
      wchar *NextStr=CurStr,*CmtPtr=NULL;
      while (*NextStr!='\r' && *NextStr!='\n' && *NextStr!=0)
      {
        if (SkipComments && NextStr[0]=='/' && NextStr[1]=='/')
        {
          *NextStr=0;
          CmtPtr=NextStr;
        }
        NextStr++;
      }
      *NextStr=0;
      for (wchar *SpacePtr=(CmtPtr ? CmtPtr:NextStr)-1;SpacePtr>=CurStr;SpacePtr--)
      {
        if (*SpacePtr!=' ' && *SpacePtr!='\t')
          break;
        *SpacePtr=0;
      }
      if (*CurStr)
      {
        int Length=strlenw(CurStr);
        int AddSize=4*(Length-AnsiName.Size()+1);
        if (AddSize>0)
          AnsiName.Add(AddSize);
        if (Unquote && *CurStr=='\"' && CurStr[Length-1]=='\"')
        {
          CurStr[Length-1]=0;
          CurStr++;
        }
        WideToChar(CurStr,&AnsiName[0],AnsiName.Size());
        List->AddString(&AnsiName[0],CurStr);
      }
      CurStr=NextStr+1;
      while (*CurStr=='\r' || *CurStr=='\n')
        CurStr++;
    }
  }
  else
  {
    char *CurStr=&Data[0];
    while (*CurStr!=0)
    {
      char *NextStr=CurStr,*CmtPtr=NULL;
      while (*NextStr!='\r' && *NextStr!='\n' && *NextStr!=0)
      {
        if (SkipComments && NextStr[0]=='/' && NextStr[1]=='/')
        {
          *NextStr=0;
          CmtPtr=NextStr;
        }
        NextStr++;
      }
      *NextStr=0;
      for (char *SpacePtr=(CmtPtr ? CmtPtr:NextStr)-1;SpacePtr>=CurStr;SpacePtr--)
      {
        if (*SpacePtr!=' ' && *SpacePtr!='\t')
          break;
        *SpacePtr=0;
      }
      if (*CurStr)
      {
        if (Unquote && *CurStr=='\"')
        {
          int Length=strlen(CurStr);
          if (CurStr[Length-1]=='\"')
          {
            CurStr[Length-1]=0;
            CurStr++;
          }
        }
#if defined(_WIN_32)
        if (SrcCharset==RCH_OEM)
          OemToChar(CurStr,CurStr);
#endif
        List->AddString(CurStr);
      }
      CurStr=NextStr+1;
      while (*CurStr=='\r' || *CurStr=='\n')
        CurStr++;
    }
  }
  return(true);
}