/** * @file XTPSyntaxEditLexCfgFileReader.h * * @copyright * (c) 1998-2025 Codejock Software, All Rights Reserved. * * This source file is the property of Codejock Software and must not be * redistributed by any means without the explicit written permission of * Codejock Software. * * The use of this source code is governed by the terms and conditions specified * in the Toolkit Pro license agreement. Codejock Software grants you, as a * single software developer, the limited right to use this software on one * computer only. * * Contact Information: * support@codejock.com * http://www.codejock.com * */ /** @cond */ #if !defined(__XTPSYNTAXEDITLEXCFGFILEREADER_H__) # define __XTPSYNTAXEDITLEXCFGFILEREADER_H__ /** @endcond */ # if _MSC_VER > 1000 # pragma once # endif // _MSC_VER > 1000 # include "Common/Base/Diagnostic/XTPDisableNoisyWarnings.h" class CXTPSyntaxEditLexCfgFileReader; /** @cond */ namespace XTPSyntaxEditLexAnalyser /** @endcond */ { /** @cond */ /** * @brief * XTPSyntaxEditTokenType type defines type of tokens that read by * CXTPSyntaxEditLexCfgFileReader class from configuration files. * @see * CXTPSyntaxEditLexCfgFileReader */ enum XTPSyntaxEditTokenType { xtpEditTokType_Unknown, /**< Specifies unknown token. */ xtpEditTokType_Delim, /**< Specifies the Delimiter token. */ xtpEditTokType_Name, /**< Specifies the Name token. */ xtpEditTokType_Value, /**< Specifies the Value token. */ xtpEditTokType_Quoted, /**< Specifies the Quoted token. */ xtpEditTokType_Comment, /**< Specifies the Comment token. */ xtpEditTokType_EOL, /**< Specifies the EndOfLine token. */ xtpEditTokType_Control /**< Specifies the Control token. */ }; /** * @brief * XTP_EDIT_LEXPROPINFO helper structure is used to group information * which describes one Lex Class property pair. The property pair is * property name and property value. */ struct _XTP_EXT_CLASS XTP_EDIT_LEXPROPINFO { XTP_EDIT_LEXPROPINFO(); XTP_EDIT_LEXPROPINFO(const XTP_EDIT_LEXPROPINFO& rSrc); const XTP_EDIT_LEXPROPINFO& operator=(const XTP_EDIT_LEXPROPINFO& rSrc); CStringArray arPropName; /**< stores property name chain */ CStringArray arPropValue; /**< stores value for property names with corresponding index */ int nLine; int nOffset; int nPropertyLen; }; /** * @brief * CXTPSyntaxEditLexPropInfoArray type defines custom array type based on * CArray to store XTP_EDIT_LEXPROPINFO structures. * @see * XTP_EDIT_LEXPROPINFO */ class _XTP_EXT_CLASS CXTPSyntaxEditLexPropInfoArray : public CArray { public: CXTPSyntaxEditLexPropInfoArray(); CXTPSyntaxEditLexPropInfoArray(const CXTPSyntaxEditLexPropInfoArray& rSrc); const CXTPSyntaxEditLexPropInfoArray& operator=(const CXTPSyntaxEditLexPropInfoArray& rSrc); }; /** * @brief * XTP_EDIT_LEXCLASSINFO structure is used to group information which describes * a set of properties for one Lex Class. * @see * XTP_EDIT_LEXPROPINFO */ struct _XTP_EXT_CLASS XTP_EDIT_LEXCLASSINFO { XTP_EDIT_LEXCLASSINFO(); XTP_EDIT_LEXCLASSINFO(const XTP_EDIT_LEXCLASSINFO& rSrc); const XTP_EDIT_LEXCLASSINFO& operator=(const XTP_EDIT_LEXCLASSINFO& rSrc); CString csClassName; /**< name to identify lex class */ CXTPSyntaxEditLexPropInfoArray arPropertyDesc; /**< array to store a set of lex class properties */ int nStartLine; int nEndLine; }; /** * @brief * Determines custom array type to store XTP_EDIT_LEXCLASSINFO structures. * @see * XTP_EDIT_LEXCLASSINFO. */ class _XTP_EXT_CLASS CXTPSyntaxEditLexClassInfoArray : public CArray { public: CXTPSyntaxEditLexClassInfoArray(); CXTPSyntaxEditLexClassInfoArray(const CXTPSyntaxEditLexClassInfoArray& rSrc); const CXTPSyntaxEditLexClassInfoArray& operator=(const CXTPSyntaxEditLexClassInfoArray& rSrc); BOOL m_bModified; }; /** @endcond */ } // namespace XTPSyntaxEditLexAnalyser /** * @brief * This class provides functionality to read configuration information * from configuration files with a predefined structure. Its information * describes lex class instances that represent various lexical items * for given language. * You construct an instance of CXTPSyntaxEditLexCfgFileReader by calling * its constructor. Then you should call the ReadSource() member function * to read data from persistent storage (file for this implementation), * and process raw data into XTP_EDIT_LEXCLASSINFO structures. Call the * GetLexClassInfoArray() member function to return a pointer to an array * of XTP_EDIT_LEXCLASSINFO data structures. */ class _XTP_EXT_CLASS CXTPSyntaxEditLexCfgFileReader { friend class CXTPSingleton; public: /** * @brief * Destroys a CXTPSyntaxEditLexCfgFileReader object, handles cleanup * and de-allocation. */ virtual ~CXTPSyntaxEditLexCfgFileReader(); /** * @brief * Reads data from the configuration file. * @param csFileName [in] Reference to a string containing the full path * to the configuration file. * @param bSaveInfo [in] TRUE to save read data in the LexClassInfoMap, * FALSE otherwise. * @details * Use this member function to read configuration data from configuration * file with predefined structure. Data loaded into internal data buffer. */ void ReadSource(const CString& csFileName, BOOL bSaveInfo); /** * @brief * Reads data from resources. * @param nResourceID [in] Resource ID of the HTML configuration data. * @details * Use this member function to read configuration data from application * resources with predefined structure. Data loaded into internal data buffer. */ void ReadSource(UINT nResourceID); /** * @brief * Reads data from resources. * @param bSaveInfo TRUE to save info, FALSE otherwise. * @param sPassedString String passed. * @param pSyntaxScheme Pointer to the string scheme. * @details * Used for non-file based settings. * Formal file name for map is "InternalSyntaxSet" */ void ReadStringSource(BOOL bSaveInfo, CString sPassedString, CString* pSyntaxScheme); /** * @brief * Gets the internal array. * @return * A reference to the internal array. */ XTPSyntaxEditLexAnalyser::CXTPSyntaxEditLexClassInfoArray& GetLexClassInfoArray(); /** * @brief * Gets the internal map. * @return * A reference to the internal map. */ CMapStringToPtr& GetLexClassInfoMap() { return m_mapLexClassInfo; } /** * @brief * Clears a specified map and deletes its data. * @param mapInfo Reference to the map to clear. * @see * CopyInfoMap */ void CleanInfoMap(CMapStringToPtr& mapInfo); /** * @brief * Copies the internal map and its data to another map. * @param mapInfo [out] Reference to the map to copy to. * @details * Use CleanInfoMap to free (delete) map data. * @see * CleanInfoMap */ void CopyInfoMap(CMapStringToPtr& mapInfo); /** * @brief * Writes LexClasses to given schema configuration file * @param csFileName Configuration file name. * @param arLexClassDesc Array with lex class data (description) to save. * @return * TRUE if successful, otherwise FALSE. */ BOOL WriteCfgFile(const CString& csFileName, XTPSyntaxEditLexAnalyser::CXTPSyntaxEditLexClassInfoArray& arLexClassDesc); /** * @brief * Writes LexClasses to given schema configuration file * @param arLexClassDesc Array with lex class data (description) to save. * @return * TRUE if successful, otherwise FALSE. */ BOOL WriteCfg2String(XTPSyntaxEditLexAnalyser::CXTPSyntaxEditLexClassInfoArray& arLexClassDesc); /** * @brief * Converts escape char sequences to corresponding char. * @param strSrc [in] Text to be converted. * @param bQuoted [in] TRUE if the text should be interpreted as being * enclosed by single quotes (' '), FALSE otherwise. * @return * The converted text. */ CString StrToES(CString strSrc, BOOL bQuoted); /** * @brief * Converts char to corresponding escape char sequences. * @param strSrc [in] Text to be converted. * @param bQuoted [in] TRUE if the text should be interpreted as being * enclosed by single quotes (' '), FALSE otherwise. * @return * The converted text. */ CString ESToStr(CString strSrc, BOOL bQuoted); private: /** * A map describes whether property description has a flag or not. */ typedef CMap CMapPtrToBool; # ifdef _DEBUG /** * Process error messages of file processing. Writes messages into debug window. */ void ProcessFileException(CFileException* pExc); # endif /** * Process next token from file. */ int GetToken(); /** * Process next lex class token from file. */ int GetLexToken(); /** * Process corresponding token */ int ProcessUnknowToken(TCHAR tchCurrSymbol); int ProcessDelimToken(TCHAR tchCurrSymbol); int ProcessNameToken(TCHAR tchCurrSymbol); int ProcessValueToken(TCHAR tchCurrSymbol); int ProcessQuotedToken(TCHAR tchCurrSymbol); int ProcessCommentToken(TCHAR tchCurrSymbol); int ProcessEOLToken(TCHAR tchCurrSymbol); int ProcessControlToken(TCHAR tchCurrSymbol); /** * Parses single lex class. */ void ParseLexClass(XTPSyntaxEditLexAnalyser::XTP_EDIT_LEXCLASSINFO& infoClass); /** * Searches for lex class. */ XTPSyntaxEditLexAnalyser::XTP_EDIT_LEXCLASSINFO* FindClassDesc(XTPSyntaxEditLexAnalyser::CXTPSyntaxEditLexClassInfoArray& arInfoClass, const CString& csClassName); /** * Searches for property of lex class. */ XTPSyntaxEditLexAnalyser::XTP_EDIT_LEXPROPINFO* FindPropDesc( XTPSyntaxEditLexAnalyser::XTP_EDIT_LEXCLASSINFO* pInfoClass, XTPSyntaxEditLexAnalyser::XTP_EDIT_LEXPROPINFO* pInfoProp, CMapPtrToBool& mapUsed); /** * Read source string by string into string array. */ void ReadSource2(const CString& csFileName, CStringArray& arBuffer); /** * Writes single string into file. */ void WriteString(CFile& file, LPCTSTR pcszString); /** * Writes a number of string into file. */ void WriteStrings(CFile& file, CStringArray& arBuffer, int nFrom, int nTo); /** * Writes a whole property description into file. */ void WriteProp(CFile& file, CString& csOffset, const XTPSyntaxEditLexAnalyser::XTP_EDIT_LEXPROPINFO& newInfoProp); void WriteProp(CFile& file, CString& csOffset, const XTPSyntaxEditLexAnalyser::XTP_EDIT_LEXPROPINFO& oldInfoProp, const XTPSyntaxEditLexAnalyser::XTP_EDIT_LEXPROPINFO& newInfoProp, const CStringArray& arBuffer); /** * Process configuration data. */ void Parse(XTPSyntaxEditLexAnalyser::CXTPSyntaxEditLexClassInfoArray& arLexClassDesc); CString* p_sSyntaxScheme; CString* p_sColorScheme; void SetSyntaxAndColorScheme(CString* pSyntaxScheme, CString* pColorScheme) { p_sSyntaxScheme = pSyntaxScheme; p_sColorScheme = pColorScheme; m_csDataBuffer = *p_sSyntaxScheme; } private: int m_nCurrLine; /**< Current processing line identifier. */ int m_nCurrLine_pos; /**< Position in current line. */ int m_nCurrPos; /**< Current position in data buffer. */ int m_nEOFPos; /**< EOF position. */ int m_nPrevPos; /**< Previous position in data buffer. */ int m_nTokenType; /**< Type of processed token. */ BOOL m_bReadNames; /**< Flag to read names of properties. */ BOOL m_bES; CString m_csDataBuffer; /**< Internal buffer to read file to. */ CString m_strToken; /**< Text of processed token. */ CMapStringToPtr m_mapLexClassInfo; XTPSyntaxEditLexAnalyser::CXTPSyntaxEditLexClassInfoArray m_arLexClassInfo; /**< Lex classes array. */ /** * Singleton instantiation. */ CXTPSyntaxEditLexCfgFileReader(); friend CXTPSyntaxEditLexCfgFileReader* AFX_CDECL XTPSyntaxEditLexConfig(); }; /** * @brief * Gets the CXTPSyntaxEditLexCfgFileReader global object. * @return * A pointer to a CXTPSyntaxEditLexCfgFileReader object. */ AFX_INLINE CXTPSyntaxEditLexCfgFileReader* AFX_CDECL XTPSyntaxEditLexConfig() { return &CXTPSingleton::Instance(); } /** * @brief * This class provides the ability to monitor changes in configuration files * and read new data from monitored files in time. */ class CXTPSyntaxEditConfigurationManager; /** * this class monitors files changes */ class _XTP_EXT_CLASS CXTPSyntaxEditFileChangesMonitor { public: /** * @brief * Default object constructor. */ CXTPSyntaxEditFileChangesMonitor(); /** * @brief * Destroys a CXTPSyntaxEditFileChangesMonitor() object, handles cleanup * and de-allocation. Stops monitoring if it has not stopped already. */ virtual ~CXTPSyntaxEditFileChangesMonitor(); /** * @brief * Sets the default monitoring folder. * @param strPath [in] Reference to the path string. */ void SetDefaultFolder(const CString& strPath); /** * @brief * Sets the associated configuration manager. * @param pConfigMgr [in] Pointer to the configuration manager to be set. * @see * CXTPSyntaxEditConfigurationManager */ void SetConfigurationManager(CXTPSyntaxEditConfigurationManager* pConfigMgr); /** * @brief * Adds a file to be monitored. * @param strFilename [in] Reference to the file name string. * @param dwOwnerFlags [in] Additional flags (from enum XTPSyntaxEditCfgObjects). * @return * TRUE if the operation was successful, otherwise FALSE. * @details * Adds a file to the bunch of files to monitor changes at. First, tries to * check the file by the provided file name string, then tries the file in * the default folder. Bad file names are ignored. * @see * CXTPSyntaxEditConfigurationManager */ BOOL AddMonitorFile(CString& strFilename, DWORD dwOwnerFlags); /** * @brief * Starts the thread which will be monitoring folder changes. */ void StartMonitoring(); /** * @brief * Stops the thread which is monitoring folder changes. */ void StopMonitoring(); /** * @brief * Refreshes the contents of the folder, update file's status and * re-reads changed configuration files. */ void RefreshFiles(); private: /** * @brief * Removes all files which are monitored at the moment. */ void RemoveAll(); private: CString m_strPath; CWinThread* m_pThread; HANDLE m_evExitThread; static UINT AFX_CDECL ThreadMonitorProc(LPVOID); BOOL m_bES; CXTPSyntaxEditConfigurationManager* m_pConfigMgr; /**< Pointer to the associated configuration manager. */ /** * this class contain information about monitoring file */ class _XTP_EXT_CLASS CFMFileInfo { public: /** * @brief * Default object constructor. */ CFMFileInfo(); CString m_strFileName; /**< Monitoring file name. */ BY_HANDLE_FILE_INFORMATION m_sysFileInfo; /**< File information. */ BOOL m_bExists; /**< TRUE if the file exists, otherwise FALSE. */ DWORD m_dwOwnerFlags; /**< Additional flags. */ /** * @brief * Copy operator. */ const CFMFileInfo& operator=(const CFMFileInfo& rSrc); }; /** * @brief * Retrieves the file information of a specified file. * @param pcszFilePath [in] Name of the file to retrieve the file information of. * @param pInfo [out] Pointer to a BY_HANDLE_FILE_INFORMATION structure * to receive the file information. * @return * TRUE if successful, otherwise FALSE. */ BOOL GetFileInfo(LPCTSTR pcszFilePath, BY_HANDLE_FILE_INFORMATION* pInfo); CArray m_arFiles; /**< Bunch of filenames to monitor changes at. */ }; AFX_INLINE void CXTPSyntaxEditFileChangesMonitor::SetConfigurationManager( CXTPSyntaxEditConfigurationManager* pConfigMgr) { m_pConfigMgr = pConfigMgr; } # include "Common/Base/Diagnostic/XTPEnableNoisyWarnings.h" /** @cond */ #endif // !defined(__XTPSYNTAXEDITLEXCFGFILEREADER_H__) /** @endcond */