⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readfile.c

📁 ARM入门的好帮手.包含了从简单到相对较复杂的程序.
💻 C
字号:
//*-----------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*-----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*-----------------------------------------------------------------------------
//* File Name               : ReadFile.c
//* Object                  : Read Binary file
//*
//* 1.0 02/05/99 JPP        : Creation
//*-----------------------------------------------------------------------------

/*----- Files to be included Definition -----*/
#include <windows.h>
#include <stdio.h>
#include "ReadFile.h"
#include "flash_49BV.h"

/*----- Types and Constants Definition -----*/

extern void displayError(void);
extern FileName workFile;              // file description
extern FlashDef FlashTable;            // Flash descriton

//* --------------------------------------------------------------------------
//* Function Name        : closeBinFile
//* Object               : Free the handle
//* Input Parameters     : none
//* Output Parameters    : none
//* Functions called     : Windows     : CloseHandle, GlobalUnlock
//* --------------------------------------------------------------------------
void closeBinFile(void)
{
    CloseHandle(workFile.hf);
    GlobalUnlock(workFile.hBin);
}
//* --------------------------------------------------------------------------
//* Function Name        : openBinFile
//* Object               : open binary file
//* Input Parameters     : none
//* Output Parameters    : FALSE if INVALID_HANDLE_VALUE or TRUE
//* Functions called     : Windows     : CreateFile
//*                        Application : displayError
//* --------------------------------------------------------------------------
BOOL openBinFile(void)
{
    //* first value
    closeBinFile();
    // Open file (return if error)
    workFile.hf=CreateFile(workFile.ofn.lpstrFile,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    // Check Handle
    if (workFile.hf == INVALID_HANDLE_VALUE)
    {
        displayError();
        return FALSE;
    }
    return TRUE;
}
//* --------------------------------------------------------------------------
//* Function Name        : readDataFile
//* Object               : Read binary file
//* Input Parameters     : data buffer and read size
//* Output Parameters    : nb read byte
//* Functions called     : Windows     : ReadFile
//*                        Application : displayError, closeBinFile
//* --------------------------------------------------------------------------
long readDataFile(char * buffer,long size)
{
    long cbRead;
    if (!ReadFile(workFile.hf,buffer,size,&cbRead,NULL) )
    {
        displayError();
        closeBinFile();
    }
    return cbRead;
}

//* --------------------------------------------------------------------------
//* Function Name        : readBinFile
//* Object               : Read binary file and copy this file into Handle to
//*                        memory buffer
//* Input Parameters     : ofn = OPENFILENAME structure
//* Output Parameters    : Buffer handler if success, NULL else
//* Functions called     : Windows     : GetFileSize, GlobalAlloc,GlobalLock
//*                        Application : displayError,openBinFile,closeBinFile
//*                                      readDataFile
//* --------------------------------------------------------------------------
HLOCAL readBinFile(OPENFILENAME ofn)
{
    BYTE *bin;

    // Open file (return if error)
    if (!openBinFile())
    {

        return NULL;
    }

    // Get file size (return if error)
    workFile.size=GetFileSize(workFile.hf,NULL);
    if (workFile.size==0xFFFFFFFF)
    {
        displayError();
        closeBinFile();
        return NULL;
    }

    // Allocate a buffer (return if error)
    workFile.hBin=GlobalAlloc(GMEM_MOVEABLE,workFile.size);
    if (workFile.hBin==NULL)
    {
        displayError();
        closeBinFile();
        return NULL;
    }

    //* Fill the buffer (return NULL if error)
    bin=GlobalLock(workFile.hBin);

    if (readDataFile(bin,workFile.size)!=workFile.size)
    {
         return NULL;
    }
    //* Read the first bytes
    openBinFile();
    readDataFile(workFile.data,sizeof(workFile.data));
    // close file handle, unlock buffer and return Buffer handle
    closeBinFile();
    return workFile.hBin;
}

//* --------------------------------------------------------------------------
//* Function Name        : openBinaryFile
//* Object               : get the file
//* Input Parameters     : none
//* Output Parameters    : none
//* Functions called     : Windows     : dialog box , GetOpenFileName,memset
//*                        Application : readBinFile
//* --------------------------------------------------------------------------
void openBinaryFile(HWND hwnd)
{
    int len;
    //* Erase buffer
    memset(&workFile, 0, sizeof(workFile));
    // set the filter
    len=sprintf(&workFile.filter[0],"Bin Files (*.bin)");
    sprintf(&workFile.filter[len+1],"*.bin");

    // Set dialogue box
    workFile.ofn.lStructSize = sizeof(OPENFILENAME);
    workFile.ofn.hwndOwner = hwnd;
    workFile.ofn.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY;
    workFile.ofn.lpstrFile= &workFile.File[0];
    workFile.ofn.nMaxFile=sizeof(workFile.File);
    workFile.ofn.lpstrFilter = &workFile.filter [0];
    workFile.ofn.lpstrFileTitle=&workFile.FileTitle[0];
    workFile.ofn.nMaxFileTitle=sizeof(workFile.FileTitle);
    workFile.ofn.lpstrInitialDir=&workFile.InitialDir[0];
    workFile.ofn.lpstrTitle=&workFile.Title[0];
    workFile.ofn.lpstrDefExt=&workFile.DefExt[0];

    if (GetOpenFileName(&workFile.ofn)!=0)
    {
        readBinFile(workFile.ofn);
    }
}
HLOCAL writeBinaryFile(unsigned char padding,long file_size,BOOL type)
{
    BYTE *bin;
    int  *pt;
    long cbwrite ;
    // Open file (return if error)
        closeBinFile();
    // Open file (return if error)
    workFile.hf=CreateFile(workFile.ofn.lpstrFile,GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    // Check Handle
    if (workFile.hf == INVALID_HANDLE_VALUE)
    {
        displayError();
        return FALSE;
    }
    // Allocate a buffer (return if error)
    workFile.hBin=GlobalAlloc(GMEM_MOVEABLE,file_size);
    if (workFile.hBin==NULL)
    {
        displayError();
        closeBinFile();
        return NULL;
    }

    //* Fill the buffer (return NULL if error)
    bin=GlobalLock(workFile.hBin);
    if (type ==TRUE)
    {
        pt =(int*)bin;
        for (cbwrite=0;cbwrite < file_size /4  ; cbwrite ++)
        {
             pt[cbwrite]= (int)cbwrite;
        }
    }
    else
    memset(bin,padding,file_size);
    if (!WriteFile(workFile.hf,bin,file_size,&cbwrite,NULL) )
    {
        displayError();
        closeBinFile();
        return NULL;
    }

    closeBinFile();
    return workFile.hBin;
}

//*----------------------------------------------------------------------------
//* Function Name       : printFileInfo
//* Object              : Format print info
//* Input Parameters    : char * addresse of message, Len
//* Output Parameters   :
//* Functions called    : sprintf
//*----------------------------------------------------------------------------
void printFileInfo ( char *message,int * len)
//* Begin
{
//    int cmpt;
    int nb_sector;
    long end_add;
    //* if File Exist
    if (workFile.hf != INVALID_HANDLE_VALUE)
    {
        //* Print file carac
       *len += sprintf(message+*len,"- File: %s\n",
            workFile.File);
       *len += sprintf(message+*len,"Size: %ld Bytes / (%ld Kbytes and %ld Bytes)\n",
            workFile.size,workFile.size/1024,workFile.size-(1024*(workFile.size/1024)));
       end_add =FlashTable.add+workFile.size-1;
       
	   nb_sector = at91_get_flash_sector ( &FlashTable, end_add, FlashTable.add);
       if (workFile.size ==0)
       {
          *len += sprintf(message+*len,"Warning file empty");
       }
       else
       {
           if ( nb_sector==FLASH_SECTOR_ERROR)
           {
               *len += sprintf(message+*len,"Warning file to big");
           }
           else
           {
                *len += sprintf(message+*len,"Use %d of %d Flash sector",
                nb_sector+1,FlashTable.sector_nb);
            }
        }
    }
    else //* Fin don't exist
    {
        *len += sprintf(message+*len,"- No file \n");
    }
}
//* End of file

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -