示例#1
0
/*****************************************************************************
* FUNCTION
*	mre_write_line_to_file
* DESCRIPTION
*	Writes line to file 
* PARAMETERS
*	file_name just the name as string in root directory, line as string
* RETURNS
*	MRE_FILE_NOT_WRITTEN if fail, MRE_SCILENT for success
TODO: Remove this. Already defined above.
*****************************************************************************/
VMINT mre_write_line_to_file2 (VMSTR file_name, VMSTR line)
{
	/* local variables */
	VMFILE  file_handle;
	VMCHAR  file_path[MRE_FILE_NAME_SIZE];
	VMWCHAR wfile_name[MRE_FILE_NAME_SIZE];
	VMWCHAR wline[MRE_STR_SIZE_MAX + 1];
    VMUINT written;
	VMUINT  bytes_written;
	VMINT line_bytes;
	
	//const VMINT line_size = sizeof (VMCHAR) * (vm_wstrlen (line)+1); //+2 for new line
	//VMINT line_bytes = sizeof(VMCHAR) * (vm_strlen(line) + 1 )
	//VMSTR line_new[1000];
	//sprintf(line_new,"%s\n", line);

	/* Open file */
	sprintf(file_path,"%c:\\%s",mre_get_drv(),file_name);
	vm_ascii_to_ucs2(wfile_name, MRE_STR_SIZE_MAX, file_path);

	vm_ascii_to_ucs2(wline, MRE_STR_SIZE_MAX, line);
	line_bytes = sizeof(VMWCHAR) *(vm_wstrlen (wline)+1); 

	//g_mre_textbox_text =  (sizeof (VMWCHAR) * (vm_wstrlen (wline)+1));
    //vm_wstrcpy (g_mre_textbox_text, text);

	file_handle = vm_file_open(wfile_name, MODE_APPEND, FALSE);
	if (file_handle < 0){
		file_handle = vm_file_open(wfile_name, MODE_CREATE_ALWAYS_WRITE, FALSE);
	}
	if(file_handle < 0)
	{
		vertical_scrolling_text("Write to file fail....");
	}else{
		/* write to file */
		bytes_written = vm_file_write (file_handle, wline, line_bytes, &written);
		vm_log_debug("%d bytes has been written in file", bytes_written);
		
        if (bytes_written == 0)
        {
            vm_file_close (file_handle);
            return MRE_FILE_NOT_WRITTEN;
        }

	    else 
	    {
		    vm_file_close (file_handle);
			//sprintf (show_text, "Written in file: success");
            //mre_show_text (show_text);
		    return MRE_SCILENT;
	    }
   
	}
	/* Closing file */
	vm_file_close(file_handle);
	return MRE_FILE_NOT_WRITTEN;
}
size_t fwrite_mre(const void *pBuf, int element_size, size_t n, MZ_FILE *pFile){
	/* fwrite in MSC is:
	static size_t mz_zip_file_write_callback(void *pOpaque, mz_uint64 ofs, const void *pBuf, size_t n)
	{
		(void)ofs; return MZ_FWRITE(pBuf, 1, n, (MZ_FILE*)pOpaque);
	}
	MZ_FWRITE(pBuf, 1, n, pZip->m_pState->m_pFile);
	*/

	//size_t fwrite(const void *str, size_t size, size_t count, file *file);

	/*
	MRE:
	return fwrite(data_to_write, size_of_data, count, file);
	 vs
	vm_file_write(file handle, data, length, &written);
	*/
	
	VMINT written_result;
	VMUINT written_bytes;
	VMUINT length;
	VMINT tell_position, tell_end_position, tell_origi_position;
	VMINT commit_result;
	length = (VMUINT)n;

	/*
	// Testing offset is set or not 
	tell_position = vm_file_tell(*pFile);

	// Testing: Check whats the end of the file 
	vm_file_seek(*pFile, 0, BASE_END);
	tell_end_position = vm_file_tell(*pFile);

	// seek back to tell_position 
	vm_file_seek(*pFile, tell_position, BASE_BEGIN);
	tell_origi_position = vm_file_tell(*pFile);
	*/ 

	written_result = vm_file_write(*pFile, (void *)pBuf, n, &written_bytes);
	//qn: close the file handle ? ^VS
	//Maybe we commit it ?
	commit_result = vm_file_commit(*pFile);
	if (commit_result != 0 ){
		printf("\nCouldn't commit write to file. Please check.\n");
	}
	return (unsigned int) written_result;
		//which one is correct ?
	return (unsigned int) written_bytes;
	//return written_result;

}
示例#3
0
/*****************************************************************************
 * FUNCTION
 *  mre_write_file
 * DESCRIPTION
 *  This function writes on the file given 
 * PARAMETERS
 *  file name				[IN]      name of file
 * RETURNS
 *	result                  [OUT]     some code for error and success   
*****************************************************************************/
VMINT mre_write_file (VMSTR file_name)
{
	VMFILE f_write; 
	VMCHAR f_name[MRE_STR_SIZE_MAX + 1] ;
    VMWCHAR f_wname[MRE_STR_SIZE_MAX + 1];
    VMINT written_result;
    VMUINT written;
    VMCHAR show_text[MRE_STR_SIZE_MAX + 1];
    vm_log_debug ("mre_write_file function starts with file name : %s ", file_name);
    sprintf (f_name, "%c:\\%s", mre_get_drv(), file_name);
    vm_log_debug("file name : %s", f_name);

	/* string format conversion */
	vm_ascii_to_ucs2 (f_wname, MRE_STR_SIZE_MAX, f_name);

	/* file is opened here */
	f_write = vm_file_open (f_wname, MODE_WRITE, FALSE);
    vm_log_debug("file open hdl : %d", f_write);

	if (f_write < 0)
	{
        return MRE_FILE_OPEN_FAILED;
	}

        written_result = vm_file_write (f_write, 
							//vm_gb2312_string (mre_get_textbox_text ()), 
							mre_get_textbox_text (), 
								sizeof (VMWCHAR) * (vm_wstrlen (mre_get_textbox_text ())+1),  
									& written);
        vm_log_debug("file write result : %d", written_result);
        vm_free (mre_get_textbox_text ());
        if (written_result == 0)
        {
            vm_file_close (f_write);
            return MRE_FILE_NOT_WRITTEN;
        }

	    else 
	    {
		    vm_file_close (f_write);
			sprintf (show_text, "Written in file: success");
            mre_show_text (show_text);
		    return MRE_SCILENT;
	    }
   
    //vm_log_debug ("mre_write_file function exits");
    
}
示例#4
0
/*****************************************************************************
 * FUNCTION
 *  mre_create_file
 * DESCRIPTION
 *  This function creates a file with given name in a drive set earlier 
 * PARAMETERS
 *  file name				[IN]      name of file
 * RETURNS
 *	result                  [OUT]     some code for error and success   
*****************************************************************************/
VMINT mre_create_file (VMSTR file_name)
{
    VMFILE fc;
    VMCHAR f_name[MRE_STR_SIZE_MAX + 1];
    VMWCHAR f_wname[MRE_STR_SIZE_MAX + 1];
    VMCHAR show_text[MRE_STR_SIZE_MAX + 1];
    VMUINT written;
	void *test_ptr;
	int ptr_size;
    vm_log_debug("mre_create_file function starts with file name : %s ", file_name);

	//Testing only. Result: works
	/*
	ptr_size = 1024;
	test_ptr = vm_malloc(ptr_size);
	*/

	sprintf (f_name, "%c:\\%s", mre_get_drv(), file_name);
    vm_log_file("file name\n");
	vm_log_file(f_name);
	vm_log_file("\n");
	

	/* string format conversion */
	vm_ascii_to_ucs2 (f_wname, MRE_STR_SIZE_MAX, f_name);

	/* file is created here */
	fc = vm_file_open (f_wname, MODE_CREATE_ALWAYS_WRITE, FALSE);
	vm_log_debug("file open hdl : %d", fc);
    

    if (fc < 0)
	{
		vm_log_file ("mre_create_file function exits\n");
        return MRE_FILE_CREATE_FAILED;
	}
	else 
	{
		vm_file_write (fc, "", 1,  & written);
        vm_file_close (fc);
		sprintf (show_text, "file created:%s", f_name);
        mre_show_text (show_text);
        vm_log_debug (" %s mre_create_file function exits", show_text);
		return MRE_SCILENT;
	}
    
}
extern int _write( int file, char *ptr, int len )
{
    if (file < 3) {
        int i;

        for (i = 0; i < len; i++) {
            retarget_putc(*ptr);
            ptr++;
        }
        return len ;
    } else {
        VMUINT written_bytes;

        vm_file_write(file, ptr, len, &written_bytes);

        // printf("_write() - file: %d, content: %s, len: %d, written: %d\n", file, ptr, len, written_bytes);
        return written_bytes;
    }
}
void app_log_file(char *fmt, ...)
{
    va_list args;
    VMINT drv, hdl;
    VMUINT written;
    VMINT ret;
    vm_time_t time = {0};
    char buf[LOG_BUF_SIZE] = {0};
    VMWCHAR wpath[FILE_PATH_SIZE] = {0};
    VMCHAR path[FILE_PATH_SIZE] = {0};
  
    buf[LOG_BUF_SIZE - 2] = '\r';
    buf[LOG_BUF_SIZE - 1] = '\n';

    va_start( args, fmt );
    vm_get_time(&time);
    vm_sprintf(buf+strlen(buf), "[%02d:%02d:%02d]", time.hour, time.min, time.sec);
    vm_vsprintf(buf+strlen(buf), fmt, args);
        
    drv = vm_get_removable_driver() > 0 ? vm_get_removeable_driver() : vm_get_system_driver();
    vm_sprintf(path, "%c:\\%s", drv, BT_NAME".log");

    vm_gb2312_to_ucs2(wpath, sizeof(wpath), path); 
    hdl = vm_file_open(wpath, MODE_APPEND, 0);
    if (hdl < 0)
        hdl = vm_file_open(wpath, MODE_CREATE_ALWAYS_WRITE, 0);
    else
    {
        if (!flag_delete_log)
        {
            vm_file_close(hdl);
            vm_file_delete(wpath);
            flag_delete_log = 1;
            hdl = vm_file_open(wpath, MODE_CREATE_ALWAYS_WRITE, 0);
        }
    }
    
    vm_file_write(hdl, buf, LOG_BUF_SIZE, &written);
    vm_file_close(hdl);

    va_end( args );						
}
示例#7
0
/*****************************************************************************
 * FUNCTION
 *  mre_dump_to_file
 *
 * DESCRIPTION
 *  Write response data to specified file. Show alert if failed.
 *
 * PARAMETERS
 *  tcp_buffer              [IN]   buffer pointer containing address of received buffer
 *  size                    [IN]   size of received data
 *  VMINT *layer_hdl		[IN]
 *
 * RETURNS
 *  VOID
*****************************************************************************/
void mre_dump_to_file(VMCHAR *tcp_buffer, VMINT size, VMINT *layer_hdl)
{
    VMFILE  handle;
	VMWCHAR wfile_name[MRE_FILE_NAME_MAX_SIZE ];
	VMCHAR  file_name[2 * MRE_FILE_NAME_MAX_SIZE];
	VMUINT length;
	VMINT write_status;

	sprintf(file_name,"%c:\\%s",mre_get_drv(),"TCP.txt");
	vm_ascii_to_ucs2(wfile_name, 100, file_name);
	handle = vm_file_open(wfile_name, MODE_APPEND, TRUE);
	if(handle < 0)
	{
		//mre_display_home_top_screen_ascii("File Handle Fail");
		mre_display_home_top_screen_ascii("File Handle Fail", layer_hdl);
		//vertical_scrolling_text("File Handle Fail....");
	}

	if (handle)
    {
   
	/* log information */
	vm_log_debug("File handle is %d.", handle);
	/* write response to tcp_buffer.txt file */
	write_status =  vm_file_write(handle, tcp_buffer, size, &length);

	if (write_status < 0)
	{
		 vm_log_debug("unable to write");
	}
	/* log information */
    vm_log_debug("%d bytes has been written to file.", length);
    }

	vm_file_close(handle);
}
示例#8
0
static void _vm_preload_read_data(vm_preload_ctx_t * ctx_p)
{
    VMCHAR *body = (VMCHAR *)_vm_kernel_calloc(BUFFER_LEN);
    //VMCHAR http_buffer[BUFFER_LEN] = {0};
    VMINT len = 0, i = 0;
    VMINT http_header_received = 0;
    VMINT first_downloaded = 0;
    VMINT http_header_len = 0;
    VMINT file_handle = -1;
    VMINT ret = 0;
    VMBYTE * buf = NULL;
    VMFILE f_handle = -1;
    VMUINT written = 0;
    vm_preload_recv_data_t data = {{E_PRELOAD_QUERYING, NULL}, 0, 0}; 

    
    data.head.user_data = ctx_p->user_data;

    //memset(buf, 0, sizeof(2*BUFFER_LEN));

    MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3358 , ctx_p->g_http_content_length);       

    // read the header
    if (0 == ctx_p->g_http_content_length)
    {
        
        buf = (VMBYTE *)_vm_kernel_malloc(BUFFER_LEN);
        if (NULL == buf)
        {
            MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_PRELOAD_E1, 7, __LINE__);
            return;
        }
        memset(buf, 0, sizeof(BUFFER_LEN));
        while ((ret = vm_tcp_read (ctx_p->soc_id, buf + len, BUFFER_LEN - len)) > 0)
        {
            len += ret;
            MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3369 , ret, len);         
        }
        
        for(i = 1; i < len; i++)
        {
            if (buf[i] == LF && buf[i - 1] == CR)
            {
                if (app_strnicmp((kal_char *)&buf[http_header_len], (kal_char *)CONTENT_LENGTH, strlen(CONTENT_LENGTH)) == 0)
                {
                    buf[i - 1] = '\0';
                    first_downloaded = 0;
                    ctx_p->g_http_content_length = atoi((const char *)&buf[http_header_len] + strlen(CONTENT_LENGTH) + 1);
                    ctx_p->need_recv_len = ctx_p->g_http_content_length;
                    //data.total = http_content_length;I
                    
                    ctx_p->update = 1;
                }

                http_header_len = i + 1;

                if ((i + 2) < len && 
                (buf[i + 2] == LF && buf[i + 1] == CR))
                {
					http_header_len += 2;
                    first_downloaded = len - http_header_len;					
					break;
                }
            }
        }
        
        //strncpy(body, buf + 2, first_downloaded);
        memcpy(body, &buf[http_header_len], first_downloaded);
        _vm_kernel_free(buf);
        if (!vm_preload_is_url_valid(body, ctx_p, 0))
        {
            _vm_preload_try_download(ctx_p);
            ctx_p->g_http_content_length = ctx_p->need_recv_len = 0;
            //ctx_p->update = 0;
            _vm_kernel_free(body);
            return;
        }
        
        // preparing file
        f_handle = vm_file_open(ctx_p->path, MODE_READ, 1);
        if (0 <= f_handle)
        {
            vm_file_close(f_handle);
            MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3411 );
            vm_file_delete(ctx_p->path);
        }
        f_handle = vm_file_open(ctx_p->path, MODE_CREATE_ALWAYS_WRITE, 1);
    }

    // preparing file
    if (0 > f_handle)
    {
        f_handle = vm_file_open(ctx_p->path, MODE_APPEND, 1);
    }

    if (0 > f_handle)
    {
        VMCHAR buf[260];
        vm_ucs2_to_ascii(buf, 260, ctx_p->path);
        MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3429 , buf);
        data.head.state = E_PRELOAD_ERR_PATH;
        ctx_p->status = E_PRELOAD_ERR_PATH;
        _vm_preload_clean_and_free_ctx(ctx_p, &data);
        _vm_kernel_free(body);
        return;
    }

    // no disk space
    {
        VMWCHAR drv_wname[4] = {0};  
        VMUINT size = 0;
        vm_wstrncpy(drv_wname, ctx_p->path, 1);
        size = vm_get_disk_free_space(drv_wname);

        if (size < (VMUINT)ctx_p->g_http_content_length)
        {
            MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3429 , (VMCHAR*)drv_wname);
            data.head.state = E_PRELOAD_FAILURE;
            ctx_p->status = E_PRELOAD_FAILURE;
            if (0 <= f_handle)
            {
                vm_file_close(f_handle);
				vm_file_delete(ctx_p->path);
            }               
            _vm_preload_clean_and_free_ctx(ctx_p, &data);
            _vm_kernel_free(body);
            return;
        }
     
    }


    do
    {
        len = first_downloaded;

        while ((ret = vm_tcp_read (ctx_p->soc_id, body + len, 
            (ctx_p->g_http_content_length > BUFFER_LEN ? BUFFER_LEN : ctx_p->g_http_content_length) - len)) > 0)
        {
            // would block
            if (0 == ret)
            {
                MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3448 );
                break;
            }
            len += ret;
            if (BUFFER_LEN == len)
            {
                MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3454 );
                break;
            }
        }
        
        ctx_p->g_http_content_length -= len;
        first_downloaded = 0;
    
        MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3462 , len);            
        data.head.state = E_PRELOAD_DOWNLOADING;
        ctx_p->status = (VMINT)E_PRELOAD_DOWNLOADING;
        data.total = ctx_p->need_recv_len;
        data.received = ctx_p->need_recv_len - ctx_p->g_http_content_length;
        //data.buf = body;
        //data.size = len;
        
        MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3469 , data.received, data.total);
        MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3470 );

        vm_file_write(f_handle, body, len, &written);
    
        PRELOAD_PMNG_WRAP_CALLBACK(ctx_p->p_hdl, 
            ctx_p->cb, ctx_p->res_id, (void*)&data);
        
    } while (BUFFER_LEN == len && 0 != ret); // would block break

    vm_file_close(f_handle);

    if (0 == ctx_p->g_http_content_length)
    {
        data.head.state = E_PRELOAD_DOWNLOADED;
        ctx_p->status = (VMINT)E_PRELOAD_DOWNLOADED;
        data.total = ctx_p->g_http_content_length;
        MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3485 , data.total);
        MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3486 );
        _vm_preload_clean_and_free_ctx(ctx_p, &data);
    }
    #if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
    #endif
    
    _vm_kernel_free(body);
    MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3582 );            
}