/* file1.cpp    file access example */

#include <windows.h>
#include <stdio.h>
#include "file1.h"

int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine,
    int nCmdShow)
{
    HWND        hWnd ;      /* the window's "handle" */
    MSG         msg ;       /* a message structure */
    WNDCLASS    wndclass ;  /* window class structure */
    char        cBuf [128] ;/* character buffer */

        wndclass.style          = CS_HREDRAW | CS_VREDRAW ;
        wndclass.lpfnWndProc    = WndProc ;
        wndclass.cbClsExtra     = 0 ;
        wndclass.cbWndExtra     = 0 ;
        wndclass.hInstance      = hInstance ;
        wndclass.hIcon          = NULL ;
        wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW) ;
        wndclass.hbrBackground  = GetStockObject (WHITE_BRUSH) ;
        wndclass.lpszMenuName   = "MYMENU" ;
        wndclass.lpszClassName  = "MyClass" ;
                        /* register the window class */
        if (!RegisterClass (&wndclass))
            return 0 ;
 
    LoadString (hInstance, S_PROGRAMCAPTION, cBuf, sizeof (cBuf)) ;

    hWnd = CreateWindow ("MyClass", cBuf, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, NULL, hInstance, NULL) ;
    ShowWindow (hWnd, nCmdShow) ;   /* display the window */
    UpdateWindow (hWnd) ;

    while (GetMessage (&msg, NULL, 0, 0))   /* message loop */
    {
        TranslateMessage (&msg) ;   /* translate keyboard messages */
        DispatchMessage (&msg) ;    /* send message to WndProc() */
    }
    return (msg.wParam) ;
}

LRESULT CALLBACK WndProc (HWND hWnd, UINT wMessage, 
                                        WPARAM wParam, LPARAM lParam)
{
    int             i ;
    FILE            *fp;
    int             nFileLong ;
    PSTR            pStr, pStr2 ;
    HDC             hDC ;

    switch (wMessage)       /* process windows messages */
    {
        case WM_COMMAND:
            switch (LOWORD(wParam))             /* menu items */
            {
                case IDM_WRITE:                 /* write to the file */
                    fp = fopen ("testfile.txt", "w") ;
                    if (fp == NULL)
                        StringTableMessageBox (hWnd, S_NOCREATE,
                            S_FILEERROR, MB_OK | MB_ICONHAND) ;
                    else
                    {                          /* allocate memory block */
                        pStr=(PSTR)malloc(27);
                        pStr2 = pStr;
                        for (i = 0 ; i < 26 ; i++)   /* letters to block */
                            *pStr2++ = (char)('A' + i) ;
                        *pStr2 = 0 ;           /* null terminate string */
                                               /* write data to file */
                        fwrite (pStr, 1, lstrlen(pStr), fp) ;
                        fclose (fp) ;          /* close file */
                        free (pStr) ;          /* free memory block */
                        StringTableMessageBox (hWnd, S_CREATED,
                            S_MESSAGE, MB_OK | MB_ICONINFORMATION) ;
                    }
                    break ;
                case IDM_READ:
                    fp = fopen ("testfile.txt", "r") ;
                    if (fp == NULL)
                          StringTableMessageBox (hWnd, S_NOREAD,
                            S_FILEERROR, MB_OK | MB_ICONHAND) ;
                    else
                    {                           
                                                /* find out file length */
                         fseek (fp, 0, SEEK_END) ;
                         nFileLong = ftell (fp) ;
                                                /* return to front of file */
                         fseek (fp, 0, SEEK_SET) ; 
                                                /* allocate memory block */
                         pStr = (PSTR)malloc(nFileLong  +  1 ) ;
                                                /* copy file data to block */
                         fread (pStr, 1, nFileLong, fp) ;
                         pStr[nFileLong] = 0 ;  /* null terminate the string */
                         fclose (fp) ;
                         hDC = GetDC (hWnd) ;   /* display the file content */
                         TextOut (hDC, 0, 0, pStr, lstrlen (pStr)) ;
                         ReleaseDC (hWnd, hDC) ;
                         free (pStr) ;          /* free memory block */
                    }
                    break ;

                case IDM_ADD:               /* append data to file end */
                                            /* allocate a memory block */
                    pStr = (PSTR) malloc(11);
                    pStr2 = pStr;
                    for (i = 0 ; i < 10 ; i++) /* copy digits to block */
                        *pStr2++ = (char)('0' + i) ;
                    *pStr2 = 0 ;            /* null terminate the string */
                                            /* open file */
                    fp=fopen("testfile.txt", "a");
                    if (fp == NULL)
                          StringTableMessageBox (hWnd, S_NOOPEN,
                                      S_FILEERROR, MB_OK | MB_ICONHAND) ;
                    else
                    {                             /* move to end of file ... */
                        fseek (fp, 0, SEEK_END) ; /* not necessary since "a" */
                                            /* write data at file end */
                        fwrite (pStr, 1, lstrlen(pStr), fp) ;
                        fclose (fp) ;       /* close file */
                    }
                    free (pStr) ;           /* free memory block */
                    StringTableMessageBox (hWnd, S_NOWTRY,
                        S_MESSAGE, MB_OK | MB_ICONINFORMATION) ;
                    break ;
                case IDM_QUIT:              /* "Quit" menu item */
                    DestroyWindow (hWnd) ;  /* destroy window, */
                    break ;                 /* terminating application */
            }
            break ;

        case WM_DESTROY:                    /* stop application */
            PostQuitMessage (0) ;
            break ;

        default:        /* default windows message processing */
            return (DefWindowProc (hWnd, wMessage, wParam, lParam)) ;
    }
    return (0) ;
}

/* display a message box based on two string table entries */

int StringTableMessageBox (HWND hWnd, int nString, 
                                        int nCaption, WORD wFlags)
{
    char    cBuf1 [128], cBuf2 [128] ;
    HANDLE  hInstance ;

    hInstance = (HINSTANCE)GetWindowLong (hWnd, GWL_HINSTANCE) ;
    LoadString (hInstance, nString, cBuf1, sizeof (cBuf1)) ;
    LoadString (hInstance, nCaption, cBuf2, sizeof (cBuf2)) ;
    return (MessageBox (hWnd, cBuf1, cBuf2, wFlags)) ;
}