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

📄 ifile.c

📁 事例介绍brew中文件接口的操作,包括读文件、写文件、以及对文件的操作。适合初学者。
💻 C
📖 第 1 页 / 共 2 页
字号:
/*===========================================================================

FILE: menu.c
===========================================================================*/


/*===============================================================================
INCLUDES AND VARIABLE DEFINITIONS
=============================================================================== */
#include "AEEModGen.h"          // Module interface definitions
#include "AEEAppGen.h"          // Applet interface definitions
#include "AEEShell.h"           // Shell interface definitions
#include "AEEStdlib.h"
#include "string.h"
#include "AEEFile.h"
#include "aeemenu.h"
#include "ifile.bid"
#include "ifile_res.h"
/*#define*/
#define STATE_MAINMENU 1
#define STATE_KEYUP 2
#define STATE_KEYCREATE 3
#define kColorClearGray 0xC0C0C000
#define kColorClearLightYellow 0xC0FFFF00
#define kColorClearBlack 0X000000000
#define kColorClearDialogBox kColorClearBlack
#define kColorClearDialogBackGround kColorClearLightYellow
#define DIALOG_BORDER_WIDTH 1//对话框边的宽度
/*typedef definition*/
typedef struct _cMEnu
{
	AEEApplet a;
	IMenuCtl *pMenuCtl;     //菜单接口
	IFileMgr *pIFileMgr;    //IFileMgr指针  
	int m_nScrWidth;
	int m_nScrHeight;
	int m_nLineHeight;
    int m_nLargeLineHeight;
	AEERect m_nClntAreaRect;
	uint8 m_nState;   	
}CMenu;
typedef struct _UserInfo
{
	char userName[2];
	double  score;
}UserInfo;

/*other type definition*/
static boolean menu_HandleEvent(CMenu*,AEEEvent,uint16,uint32);
static boolean menu_InitAppData(CMenu *);
static void menu_FreeAppData(CMenu *);
static boolean menu_Start(CMenu * Papp);
static boolean File_Create(CMenu *pApp);
static boolean File_Sort(CMenu *pApp);
static boolean File_Show(CMenu *pApp);
static void DevDrawDialog(CMenu *pApp,const AECHAR *szText);
/*-------------------------------------------------------------------
Function Prototypes
-------------------------------------------------------------------*/
//static boolean menu_HandleEvent(IApplet * pi, AEEEvent eCode, 
     //                               uint16 wParam, uint32 dwParam);

/*===============================================================================
FUNCTION DEFINITIONS
=============================================================================== */

/*===========================================================================

FUNCTION: AEEClsCreateInstance

DESCRIPTION
	This function is invoked while the app is being loaded. All Modules must provide this 
	function. Ensure to retain the same name and parameters for this function.
	In here, the module must verify the ClassID and then invoke the AEEApplet_New() function
	that has been provided in AEEAppGen.c. 

   After invoking AEEApplet_New(), this function can do app specific initialization. In this
   example, a generic structure is provided so that app developers need not change app specific
   initialization section every time except for a call to IDisplay_InitAppData(). 
   This is done as follows: InitAppData() is called to initialize AppletData 
   instance. It is app developers responsibility to fill-in app data initialization 
   code of InitAppData(). App developer is also responsible to release memory 
   allocated for data contained in AppletData -- this can be done in 
   IDisplay_FreeAppData().

PROTOTYPE:
   int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj)

PARAMETERS:
	clsID: [in]: Specifies the ClassID of the applet which is being loaded

	pIShell: [in]: Contains pointer to the IShell object. 

	pIModule: pin]: Contains pointer to the IModule object to the current module to which
	this app belongs

	ppObj: [out]: On return, *ppObj must point to a valid IApplet structure. Allocation
	of memory for this structure and initializing the base data members is done by AEEApplet_New().

DEPENDENCIES
  none

RETURN VALUE
  AEE_SUCCESS: If the app needs to be loaded and if AEEApplet_New() invocation was
     successful
  EFAILED: If the app does not need to be loaded or if errors occurred in 
     AEEApplet_New(). If this function returns FALSE, the app will not be loaded.

SIDE EFFECTS
  none
===========================================================================*/
int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj)
{
	CMenu *pApp;
   *ppObj = NULL;
		
   if(ClsId == AEECLSID_IFILE){
      if(AEEApplet_New(sizeof(CMenu),
		  ClsId, 
		  pIShell,
		  po,
		  (IApplet**)ppObj,
         (AEEHANDLER)menu_HandleEvent,         //指向应用程序事件处理函数 ,不能为空
		 (PFNFREEAPPDATA)menu_FreeAppData)     //指向应用程序退出时的资源释放函数,可以为空
		 
         == TRUE)
      {
		 pApp=(CMenu*)*ppObj;
		 if(!menu_InitAppData(pApp))           //初始化应用程序数据
		 {
			 IAPPLET_Release((IApplet*)pApp);  //初始化应用程序数据出错,释放申请的内存资源
			 *ppObj=NULL;
			 return EFAILED;                   //初始化失败时返回值
		 }

		  return (AEE_SUCCESS);
      }
   }
	return (EFAILED);                          //classID检查失败时返回
}

/*===========================================================================

FUNCTION menu_HandleEvent

DESCRIPTION
	This is the EventHandler for this app. All events to this app are handled in this
	function. All APPs must supply an Event Handler.

PROTOTYPE:
	boolean menu_HandleEvent(IApplet * pi, AEEEvent eCode, uint16 wParam, uint32 dwParam)

PARAMETERS:
	pi: Pointer to the AEEApplet structure. This structure contains information specific
	to this applet. It was initialized during the AEEClsCreateInstance() function.

	ecode: Specifies the Event sent to this applet

   wParam, dwParam: Event specific data.

DEPENDENCIES
  none

RETURN VALUE
  TRUE: If the app has processed the event
  FALSE: If the app did not process the event

SIDE EFFECTS
  none
===========================================================================*/
//事件处理函数
static boolean menu_HandleEvent(CMenu * pApp, AEEEvent evt, uint16 w, uint32 dw)
{  
   switch (evt) 
	{
      case EVT_APP_START:                        
		    
		  return menu_Start(pApp);       //应用程序启动后,发出事件,跳转到menu_Start()
      case EVT_APP_STOP:

		    // Add your code here .....

         return TRUE;
	  case  EVT_APP_SUSPEND:
      case  EVT_APP_RESUME:
   	  case  EVT_NOTIFY:
      case EVT_KEY:
		  switch(pApp->m_nState)
		  {
		  case STATE_MAINMENU:		 
			  IMENUCTL_HandleEvent(pApp->pMenuCtl,EVT_KEY,w,dw); //菜单上下左右的移动,键控事件
			  return TRUE;
		  case STATE_KEYUP:
			   menu_Start(pApp);
			   IMENUCTL_SetSel(pApp->pMenuCtl,IDS_SORT);
			   return TRUE;
		  case STATE_KEYCREATE:
                menu_Start(pApp);
			   IMENUCTL_SetSel(pApp->pMenuCtl,IDS_CREATE);
			   return TRUE;
         default:
				  return FALSE;
				         
		  }
		  return TRUE;

		case EVT_COMMAND:
			  switch(w)
			  {
			  case IDS_CREATE:
				  
				  return File_Create(pApp);
			  case IDS_SORT:
				  return File_Sort(pApp);
			  case IDS_SHOW: 
				  return File_Show(pApp);				
			  //case IDS_OK:
			//	  return menu_Start(pApp);
			  default:
				  return FALSE;   							 
			  }
			  return TRUE;
			  default:
				  break;
   }

   return FALSE;
}
//////////////////////////////////////////////////////////////////////
//文件创建
static boolean File_Create(CMenu *pApp)
{
	IFile *pIFile;
	UserInfo userInfo[10];
	int i;
	if(IFILEMGR_Test(pApp->pIFileMgr,"test.txt")==SUCCESS)
        pIFile=IFILEMGR_OpenFile(pApp->pIFileMgr,"test.txt",_OFM_READWRITE);//判断文件存在以读写方式打开
	else
		if ((pIFile=IFILEMGR_OpenFile(pApp->pIFileMgr,"test.txt",_OFM_CREATE))==NULL)
	{ 
		IFILEMGR_Release(pApp->pIFileMgr);
		return FALSE;
	}
	for(i=0;i<10;i++)
	{
		userInfo[i].userName[0]='A'+i;
        userInfo[i].userName[1]='\0';
		userInfo[i].score=i+1.0;
		if(IFILE_Write(pIFile,userInfo[i].userName,sizeof(userInfo[i].userName))!=sizeof(userInfo[i].userName))
		{
			IFILE_Release(pIFile);                       //关闭文件
	     	IFILEMGR_Remove(pApp->pIFileMgr,"test.txt"); //删除文件

⌨️ 快捷键说明

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