/***************************************************************************************
 *
 *  WRITEPAD(r): Handwriting Recognition Engine (HWRE) and components.
 *  Copyright (c) 2001-2016 PhatWare (r) Corp. All rights reserved.
 *
 *  Licensing and other inquires: <developer@phatware.com>
 *  Developer: Stan Miasnikov, et al. (c) PhatWare Corp. <http://www.phatware.com>
 *
 *  WRITEPAD HWRE is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
 *  AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
 *  INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
 *  FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL PHATWARE CORP.
 *  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT, SPECIAL, INCIDENTAL,
 *  INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER,
 *  INCLUDING WITHOUT LIMITATION, LOSS OF PROFIT, LOSS OF USE, SAVINGS
 *  OR REVENUE, OR THE CLAIMS OF THIRD PARTIES, WHETHER OR NOT PHATWARE CORP.
 *  HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
 *  ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
 *  POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with WritePad.  If not, see <http://www.gnu.org/licenses/>.
 *
 **************************************************************************************/

#ifndef FILE_DEFINED        /*  See #endif in the end of file.  */
#define FILE_DEFINED

#include "bastypes.h"
#include "hwr_sys.h"

#ifndef  HFILE_TO_VALUE
typedef  p_VOID  _HFILE;
#define  VALUE_TO_HFILE VALUE_TO_PTR
#define  HFILE_TO_VALUE PTR_TO_VALUE
#endif
typedef  _HFILE _PTR           p_HFILE;

#ifndef  HTEXT_TO_VALUE
typedef  p_VOID  _HTEXT;
#define  VALUE_TO_HTEXT VALUE_TO_PTR
#define  HTEXT_TO_VALUE PTR_TO_VALUE
#endif
typedef  _HTEXT _PTR           p_HTEXT;

#ifndef  HFIND_TO_VALUE
typedef  _HMEM _HFIND;
#define  VALUE_TO_HFIND VALUE_TO_HMEM
#define  HFIND_TO_VALUE HMEM_TO_VALUE
#endif
typedef  _HFIND _PTR           p_HFIND;

#ifndef  HSEEK_TO_VALUE
typedef  _LONG  _HSEEK;
#define  VALUE_TO_HSEEK VALUE_TO_LONG
#define  HSEEK_TO_VALUE LONG_TO_VALUE
#endif
typedef  _HSEEK _PTR           p_HSEEK;

/* Values available for Open (parameter wRdWrAccess HWRFileOpend and
   HWRTextOpen) */
#define HWR_FILE_NORMAL 0
#define HWR_FILE_RDWR   0
#define HWR_FILE_RDONLY 1
#define HWR_FILE_WRONLY 2

#define HWR_TEXT_NORMAL 0
#define HWR_TEXT_RDWR   0
#define HWR_TEXT_RDONLY 1
#define HWR_TEXT_WRONLY 2

/* Origin constants for seek (parameter wOrigin in function HWRFileSeek) */
#define HWR_FILE_SEEK_SET 0
#define HWR_FILE_SEEK_CUR 1
#define HWR_FILE_SEEK_END 2

/* Constants for seek (parameter hSeek in function HWRTextSeek) */
#define HWR_TEXT_SEEK_BEGIN   ((_LONG)(-2L))
#define HWR_TEXT_SEEK_END     ((_LONG)(-3L))

/* EOF constants */
#define HWR_FILE_EOF   ((_WORD)(_INT)-1)
#define HWR_TEXT_EOF   ((_WORD)(_INT)-1)

/* Open mode for file functions (parameter wOpenMode in function HWRFileOpen) */
#define HWR_FILE_EXIST_OPEN      0x01
#define HWR_FILE_EXIST_CREATE    0x02
#define HWR_FILE_EXIST_ERROR     0x03
#define HWR_FILE_EXIST_UNIQUE    0x04
#define HWR_FILE_EXIST_APPEND    0x05
#define HWR_FILE_NOTEXIST_CREATE 0x10
#define HWR_FILE_NOTEXIST_ERROR  0x20
#define HWR_FILE_EXIST_MASK      0x0F
#define HWR_FILE_NOTEXIST_MASK   0xF0

#define HWR_FILE_EXCL      (HWR_FILE_EXIST_OPEN|HWR_FILE_NOTEXIST_ERROR)
#define HWR_FILE_OPEN      (HWR_FILE_EXIST_OPEN|HWR_FILE_NOTEXIST_CREATE)
#define HWR_FILE_CREAT     HWR_FILE_OPEN
#define HWR_FILE_TRUNC     (HWR_FILE_EXIST_CREATE|HWR_FILE_NOTEXIST_CREATE)
#define HWR_FILE_APPEND    (HWR_FILE_EXIST_APPEND|HWR_FILE_NOTEXIST_CREATE)

/* Open mode for text functions (parameter wOpenMode in function HWRTextOpen) */
#define HWR_TEXT_EXIST_OPEN      0x01
#define HWR_TEXT_EXIST_CREATE    0x02
#define HWR_TEXT_EXIST_ERROR     0x03
#define HWR_TEXT_EXIST_UNIQUE    0x04
#define HWR_TEXT_EXIST_APPEND    0x05
#define HWR_TEXT_NOTEXIST_CREATE 0x10
#define HWR_TEXT_NOTEXIST_ERROR  0x20
#define HWR_TEXT_EXIST_MASK      0x0F
#define HWR_TEXT_NOTEXIST_MASK   0xF0

#define HWR_TEXT_EXCL      (HWR_TEXT_EXIST_OPEN|HWR_TEXT_NOTEXIST_ERROR)
#define HWR_TEXT_OPEN      (HWR_TEXT_EXIST_OPEN|HWR_TEXT_NOTEXIST_CREATE)
#define HWR_TEXT_CREAT     HWR_TEXT_OPEN
#define HWR_TEXT_TRUNC     (HWR_TEXT_EXIST_CREATE|HWR_TEXT_NOTEXIST_CREATE)
#define HWR_TEXT_APPEND    (HWR_TEXT_EXIST_APPEND|HWR_TEXT_NOTEXIST_CREATE)

/* Binary file functions */

_HFILE  HWRFileOpen(_STR zPathName, _WORD wRdWrAccess, _WORD wOpenMode);
/* a, Ǽ 2. wRdWrAccess - ǧǒ ǧ¹ȇ -  E,
    2 ǒǗ2E. wOpenMode -  a° 2. 4§U
   2Ǘ° 4aǗ a¶:
   HWR_FILE_EXCL - a 2 E 2, 2  ,  .
   HWR_FILE_OPEN - 2 2 ,   Ǽ§U. 2 ° - a§U.
   HWR_FILE_CREAT -  ,   HWR_FILE_OPEN.
   HWR_FILE_TRUNC - 2 Ǽ§U Ǘ 4  ǜ °  2 .
   HWR_FILE_APPEND - »   HWR_FILE_OPEN,  ǧ2 aU 2
   U/    2.
	Ǽa - Handle 2 _NULL a 
	*/

_WORD  HWRFileRead(_HFILE hFile, p_VOID pReadBuffer,
                   _WORD wNumberOfBytes);
/*   2 . Ǽa 2 o  (0 2  
   2ǧ a  2  2.
   */

_WORD HWRFileWrite(_HFILE hFile, p_VOID pWriteBuffer,
                   _WORD wNumberOfBytes);
/*   2 . Ǽa 2 o  (0 2  
   2ǧ a  2  4  ǧ2.
   */

/* _WORD HWRFileXRead (_HFILE hFile, p_VOID pReadBuffer,
							  _WORD wNumberOfBytes); */

/* _WORD HWRFileXWrite (_HFILE hFile, p_VOID pReadBuffer,
							  _WORD wNumberOfBytes); */

_BOOL HWRFileSeek(_HFILE hFile, _LONG lOffset, _WORD wOrigin);
/* Ǘ 2U U/  2ǻ lOffset  4ǖ wOrigin.
   Ǽa _TRUE 2  a2 a42  _FALSE 2 .
   */

_LONG HWRFileTell(_HFILE hFile);
/* Ǽa ¹E ǼE 2 2 -1L 2  2 ǼU 
   a2.
   */

_BOOL  HWRFileEOF(_HFILE hFile);
/* Ǽa  _NULL 2 2 U/   2  _NULL
    a4 2.
   */

_BOOL HWRFileClose(_HFILE hFile);
/* a 2. Ǽa _NULL a , _TRUE a Ȅo.
*/

/* Text file functions */

_HTEXT HWRTextOpen (_STR zPathName, _WORD wRdWrAccess, _WORD wOpenMode);
/* a, Ǽ 2. wRdWrAccess - ǧǒ ǧ¹ȇ -  E,
    2 ǒǗ2E. wOpenMode -  a° 2. 4§U
   2Ǘ° 4aǗ a¶:
   HWR_TEXT_EXCL - a 2 E 2, 2  ,  .
   HWR_TEXT_OPEN - 2 2 ,   Ǽ§U. 2 ° - a§U.
   HWR_TEXT_CREAT -  ,   HWR_TEXT_OPEN.
   HWR_TEXT_TRUNC - 2 Ǽ§U Ǘ 4  ǜ °  2 .
   HWR_TEXT_APPEND - »   HWR_TEXT_OPEN,  ǧ2 aU 2
   U/    2.
	Ǽa - Handle 2 _NULL a 
	*/

#if HWR_SYSTEM == HWR_MACINTOSH
_WORD  _FVPREFIX HWRTextPrintf(_HTEXT hText, _STR pFormatString, ...);
/*  printf. Ǽa 2 o 42Ǘ,  a  -
   HWR_TEXT_EOF.
   */

_WORD _FVPREFIX HWRTextSPrintf(p_CHAR pcBuffer, _STR pFormatString, ...);
/*  sprintf. Ǽa 2 o 42Ǘ,  a  -
   HWR_TEXT_EOF.
   */
#else
#define HWRTextSPrintf sprintf
#define HWRTextPrintf  fprintf
#endif /* HWR_SYSTEM */
/* _WORD    _FVPREFIX HWRTextScanf (_HTEXT hText, _STR pFormatString, ... ); */
/*   a.
*/

/* _WORD HWRTextGetLine (_HTEXT hText, p_VOID pBuffer); */
/* µU  ǒ - 2 HWRTextGetS.
*/

_WORD HWRTextGetC(_HTEXT hText);
/* ǖ 42. aǖ§U a2UU \r\n. Ǽa  42,
    a  HWR_TEXT_EOF.
   */

_WORD HWRTextPutC(_WORD wChar, _HTEXT hText);
/* ǖ 42. aǖ§U a2UU \r\n. Ǽa  42,
    a  HWR_TEXT_EOF.
   */

_STR HWRTextGetS(_STR zStr, _WORD wMaxSize, _HTEXT hText);
/* ǖ a. aǖ§U a2UU \r\n. wMaxSize - a4a a 2U
   a4 a (   \0, a  ǒ2U§U   -
   2U  ǻ  4). Ǽa§U _NULL a   zStr a
   .  4!
   */

_BOOL HWRTextPutS(_STR zStr, _HTEXT hText);
/* ǖ a. aǖ§U a2UU \r\n. Ǽa§U _NULL a
     _TRUE a Ȅo.
   */

_BOOL HWRTextEOF(_HTEXT hText);
/* Ǽa  _NULL 2 2 U/   2  _NULL
    a4 2.
   */

_HSEEK HWRTextTell(_HTEXT hText);
/* a ¹E ǼE 2 2U ǜ, ǒ  4 ȇ°
   ¹  (2 ¹ !). Ǽa _NULL a    handle
   a Ȅo.  handle 4ǻ 2Ǘ° 2  µ HWRTextSeek.
   */

_BOOL HWRTextSeek(_HTEXT hText, _HSEEK hSeek);
/* 2 2  ǼE a2U4E hSeek. hSeek - 2 a2
   2U HWRTextTell, 2  ǖ   HWR_TEXT_SEEK_BEGIN
   2 HWR_TEXT_SEEK_END. a 4,   
   HWR_FILE_SEEK_END  HWR_TEXT_SEEK_END oǻ 4,  Ǘaۄ
   a 42 , §, aǄ a   .
   */

_BOOL HWRTextClose(_HTEXT hText);
/* a 2. Ǽa _NULL a , _TRUE a Ȅo.
*/


typedef struct
{
	_STR    zFileName;
	_LONG   lFileSize;
} _FileFind, _PTR p_FileFind;

_HFIND HWRFileFindOpen(_STR zPathName, _WORD wAttr);
p_FileFind  HWRFileFind(_HFIND hFind);
_BOOL  HWRFileFindClose(_HFIND hFind);

#endif  /*  FILE_DEFINED  */


