HcFileAdapter.c 9.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
#if defined(__MMI_DSM_NEW__) && defined( __HC_CALL_NEW__)
/*-------------------------------------------------------------------------------*
杭州斯凯版权所有
File: HcCallIn_UI.c
Create: 2009.09.23
--------------------------------------------------------------------------------*/
#include "HcFileAdapter.h"
#include "fat_fs.h"
#include "GlobalMenuItems.h"
#include "FrameworkStruct.h"
#include "GlobalDefs.h"
#include "FileManagerGProt.h"
//#include "mmidsmmain.h"
#if defined(WIN32)
#define MMI_SYSTEM_DRV SRV_FMGR_SYSTEM_DRV
#define MMI_PUBLIC_DRV SRV_FMGR_PUBLIC_DRV
#define MMI_CARD_DRV SRV_FMGR_CARD_DRV
#else
#include "mrp_features.h"
#endif

static uint8 *g_bytestream_buf_ptr = 0;
static uint32 g_bytestream_size = 0;
static int32 g_bytestream_offset = 0;

int8 g_filename[HCPIM_MAX_FILE_LEN*2];


int HC_get_driver_char(int type)
{
    switch(type)
    {
        case HC_DRIVER_SYSTEM:
            return MMI_SYSTEM_DRV; //FS_GetDrive(FS_DRIVE_I_SYSTEM, 1, FS_NO_ALT_DRIVE);
            break;
        case HC_DRIVER_PUBLIC:
            return MMI_PUBLIC_DRV; //FS_GetDrive(FS_DRIVE_V_NORMAL, 1, FS_NO_ALT_DRIVE);
            break;
        case HC_DRIVER_CARD:
            return MMI_CARD_DRV; //FS_GetDrive(FS_DRIVE_V_REMOVABLE, 1, FS_NO_ALT_DRIVE);
            break;
    }

	return MMI_SYSTEM_DRV;
}


/********************************************************************
Function:		HcFile_CreateHccallDir
Description: 	create the hccall's root dir
Input:		void
Output:		void
Return:		void
Notice:		this function is called when the phone is powered on
*********************************************************************/
int32 HcFile_CreateHccallDir(void)
{
	int ret;

	memset(g_filename, 0, sizeof(g_filename));
	kal_wsprintf((uint16 *)g_filename, "%c:\\%w", HC_get_driver_char(HC_DRIVER_CARD), L"newldzs");

	ret = FS_GetAttributes((uint16*)g_filename);
	if((ret < 0) ||(!(ret&FS_ATTR_DIR)))
	{
		FS_CreateDir((uint16*)g_filename);
	}
	
	return TRUE;
}


/********************************************************************
Function:		HcFile_Makeup_FileName
Description: 	makeup the hccall's file name, add the default 
Input:		void
Output:		void
Return:		void
Notice:		this function is called when the phone is powered on
*********************************************************************/
int32 HcFile_Makeup_FileName(int8 *destPathname, int8* srcFilename)
{
	int8 fileName[50] = {0};

	if(strcmp((const char*)HCUSER_CONFIG_F, (const char*)srcFilename)!=0)
	{
		kal_wsprintf((uint16 *)destPathname, "%c:\\%w", HC_get_driver_char(HC_DRIVER_CARD), L"newldzs\\");
	}
	else
	{
		char buf[100] = {0};
		sprintf(buf, "%c:\\", HC_get_driver_char(HC_DRIVER_PUBLIC));
		mmi_asc_to_ucs2((S8*)destPathname, (S8*)buf);
	}

	mmi_asc_to_ucs2((S8*)fileName, (S8*)srcFilename);
	mmi_ucs2cat((S8*)destPathname, (const S8*)fileName);
	
	return TRUE;
}


/****************************************************************************
函数名:int32 HcFile_Open(int32* FileHandle,int8* name,uint32 flag,uint16 mode)
描  述:打开文件
参  数:FileHandle 文件句柄 name 文件名 flag 打开方式 mode 保留
返  回:成功返回TRUE 否则返回FALSE
****************************************************************************/
int32 HcFile_Open(int32* FileHandle, int8* name, uint32 flag, uint16 mode)
{
	int32 result;

	if (FileHandle==NULL || name==NULL)
	{
		return FALSE;
	}
	
	if (mode == 1)
	{
		*FileHandle = (int32)name;
		g_bytestream_buf_ptr = (uint8*)name;
		g_bytestream_size = (uint32)flag;
		g_bytestream_offset = 0;
		return TRUE;
	}
	
	memset(g_filename, 0, sizeof(g_filename));
	HcFile_Makeup_FileName(g_filename, name);
	
	switch(flag)
	{
		case 0://PO_RDONLY
			result = FS_Open((uint16*)g_filename,FS_READ_ONLY | FS_OPEN_NO_DIR);
			break;
		case 1://PO_WRONLY
			result = FS_Open((uint16*)g_filename,FS_READ_WRITE | FS_CREATE_ALWAYS | FS_OPEN_NO_DIR);
			break;
		case 2://PO_RDWR
			result = FS_Open((uint16*)g_filename,FS_READ_WRITE | FS_CREATE  | FS_OPEN_NO_DIR);
			break;
		default:
			return FALSE;
	}				
	
	if(result >= 0)
	{
		*FileHandle = result;
		  
		return TRUE;
	}
	else
	{        
		return FALSE;
	}
}


/****************************************************************************
函数名:int32 HcFile_Close(int32 FileHandle, uint32 mode)
描  述:关闭文件
参  数:FileHandle 文件句柄 
返  回:成功返回TRUE 否则返回FALSE
****************************************************************************/
int32 HcFile_Close(int32 FileHandle, uint32 mode)
{
	if (FileHandle == 0)
	{
		return FALSE;
	}

	if (mode == 0)
	{
		FS_Close(FileHandle);
	}
	else
	{
		g_bytestream_size = 0;
		g_bytestream_offset = 0;
		g_bytestream_buf_ptr = NULL;
	}

	return TRUE;
}


/****************************************************************************
函数名:int32 HcFile_Read(int32 FileHandle,void* buf,uint32 count,uint32* ReadCount, HC_UINT0)
描  述:读取文件
参  数:FileHandle 文件句柄  buf 存储区 count 存储区大小 readcount 读取计数
返  回:成功返回TRUE 否则返回FALSE
****************************************************************************/
int32 HcFile_Read(int32 FileHandle,void* buf,uint32 count,uint32* ReadCount, uint32 mode)
{
	int rc;
	UINT n;

	if (FileHandle==0 || buf==NULL || count<0)
	{
		return FALSE;
	}

	if (mode == 0)
	{
		rc = FS_Read(FileHandle,buf,count,&n);
		if(rc < 0)
		{
			return FALSE;	
		}

		if (ReadCount)
		{
			*ReadCount = n;
		}
	}
	else
	{
		if (g_bytestream_offset + count > g_bytestream_size)
		{
			return FALSE;
		}
		else
		{
			memcpy(buf, g_bytestream_buf_ptr + g_bytestream_offset, count);
			g_bytestream_offset += count;
			*ReadCount = count;
		}
	}
		
	return TRUE;
}


/****************************************************************************
函数名:int32 HcFile_Write(int32 FileHandle,void* buf,uint32 count,uint32* WriteCount)
描  述:写文件
参  数:FileHandle 文件句柄  buf 存储区 count 存储区大小 readcount 写计数
返  回:成功返回TRUE 否则返回FALSE
****************************************************************************/
int32 HcFile_Write(int32 FileHandle,void* buf,uint32 count,uint32* WriteCount)
{
	int rc;
	UINT  write;

	if (FileHandle==0 || buf==NULL || count<=0)
	{
		return FALSE;
	}
	
	rc = FS_Write(FileHandle,buf,count,&write);
	if(rc < 0)
	{
		return FALSE;
	}
	
	if(WriteCount != NULL)
	{
		*WriteCount = write;
	}
	
	return TRUE;
}


/****************************************************************************
函数名:int32 HcFile_Seek(int32 FileHandle,int32 offset,int16 origin, uint16 mode)
描  述:文件指针转移
参  数:FileHandle 文件句柄  offset 偏移量 origin 原点 SeekLen 长度
返  回:成功返回TRUE 否则返回FALSE
****************************************************************************/
int32 HcFile_Seek(int32 FileHandle, int32 offset,int16 origin, uint16 mode)
{
	int rc;

	if (FileHandle==0)
	{
		return FALSE;
	}

	if (mode == 0)
	{
		rc = FS_Seek(FileHandle, offset, origin);
		if(rc < 0)
		{
			return FALSE;
		}
	}
	else
	{
		uint32 offsetBk = g_bytestream_offset;
		
		switch(origin)
		{
		case PSEEK_SET:
			{
				g_bytestream_offset = offset;
			}
			break;
			
		case PSEEK_CUR:
			{
				g_bytestream_offset += offset;
			}
			break;
			
		case PSEEK_END:
			{
				g_bytestream_offset = g_bytestream_size-offset;
			}
			break;
		}

		if(g_bytestream_offset <0||g_bytestream_offset>= g_bytestream_size)
		{
			g_bytestream_offset= offsetBk;
			return FALSE;
		}
	}

	return TRUE;
}


/****************************************************************************
函数名:int32 HcFile_Delete(int8* name)
描  述:删除文件
参  数:name文件名
返  回:成功返回TRUE 否则返回FALSE
****************************************************************************/
int32 HcFile_Delete(int8* name)
{
	int rc;

	if (name==NULL)
	{
		return FALSE;
	}
	
	memset(g_filename, 0, sizeof(g_filename));
	HcFile_Makeup_FileName(g_filename, name);

	rc = FS_Delete((uint16*)g_filename);

	return TRUE;
}


int32 HcFile_Rename(int8* poldname, int8* pnewname)
{
	int8 *oldfilename = NULL;

	if (poldname==NULL || pnewname==NULL)
	{
		return FALSE;
	}

	oldfilename = (int8*)OslMalloc(HCPIM_MAX_FILE_LEN*2);
	if (!oldfilename)
	{
		return FALSE;
	}
	
	memset(oldfilename, 0, HCPIM_MAX_FILE_LEN*2);
	memset(g_filename, 0, sizeof(g_filename));
	HcFile_Makeup_FileName(oldfilename, poldname);
	HcFile_Makeup_FileName(g_filename, pnewname);
	
	if (FS_Rename((U16*)oldfilename,(U16*)g_filename) == FS_NO_ERROR)
	{
		OslMfree(oldfilename);
		return TRUE;
	}

	OslMfree(oldfilename);
	return FALSE;
}


/****************************************************************************
函数名:int32 HcFile_Delete(int8* name)
描  述:文件是否存在
参  数:name文件名
返  回:是返回TRUE 否则返回FALSE
****************************************************************************/
int32 HcFile_IsExist(int8* name)
{
	int fHandle;

	if (name==NULL)
	{
		return FALSE;
	}
	
	memset(g_filename, 0, sizeof(g_filename));
	HcFile_Makeup_FileName(g_filename, name);
	
	fHandle = FS_Open((uint16*)g_filename,FS_READ_ONLY);
	if(fHandle < 0)
	{
		return FALSE;
	}

	FS_Close(fHandle);
	return TRUE;
}



#ifdef DEBUG_FILE_TRACE
char g_hccall_tracebuffer[1024];

void FilePrintf(const char *pcFormat, ...)
{
	va_list MyList;
	int32 fhandle = 0;
	int32 result = 0;
	uint32 writecount = 0;

	memset(g_hccall_tracebuffer, 0, sizeof(g_hccall_tracebuffer));
	
	va_start(MyList, pcFormat);
	vsprintf((char*)g_hccall_tracebuffer, pcFormat, MyList);
	va_end(MyList);

	result = HcFile_Open(&fhandle, (int8*)HC_FILELOG ,PO_RDWR, 0);
	if (!result)
	{
		return;
	}

	HcFile_Seek(fhandle, 0, PSEEK_END, 0);
	HcFile_Write(fhandle, g_hccall_tracebuffer, strlen(g_hccall_tracebuffer), &writecount);
	HcFile_Write(fhandle, "\r\n", 2, &writecount);
	HcFile_Close(fhandle, 0);
}
#endif

#endif //__HC_CALL_NEW__