Exemple #1
0
int main(){
	//printf("1erwerwer\n");
	mf_handle_t mf = mf_open("test");
	//printf("2\n");
	char buf[20	] = "1111";
	mf_write(mf, buf, 4, 3);
	//printf("3\n");
	mf_read(mf, buf, 10, 1);
	//printf("4\n");
	printf("%s\n %d\n", buf, (int)mf_file_size(mf));
	//printf("%d\n", 40*1024*(1024*1024/sysconf(_SC_PAGE_SIZE)));
	
	mf_close(mf);
	//printf("5\n");
	return 0;
}
Exemple #2
0
/*
 * Sync the memory file *mfp to disk.
 * Flags:
 *  MFS_ALL	If not given, blocks with negative numbers are not synced,
 *		even when they are dirty!
 *  MFS_STOP	Stop syncing when a character becomes available, but sync at
 *		least one block.
 *  MFS_FLUSH	Make sure buffers are flushed to disk, so they will survive a
 *		system crash.
 *  MFS_ZERO	Only write block 0.
 *
 * Return FAIL for failure, OK otherwise
 */
    int
mf_sync(memfile_T *mfp, int flags)
{
    int		status;
    bhdr_T	*hp;
#if defined(SYNC_DUP_CLOSE)
    int		fd;
#endif
    int		got_int_save = got_int;

    if (mfp->mf_fd < 0)	    /* there is no file, nothing to do */
    {
	mfp->mf_dirty = FALSE;
	return FAIL;
    }

    /* Only a CTRL-C while writing will break us here, not one typed
     * previously. */
    got_int = FALSE;

    /*
     * sync from last to first (may reduce the probability of an inconsistent
     * file) If a write fails, it is very likely caused by a full filesystem.
     * Then we only try to write blocks within the existing file. If that also
     * fails then we give up.
     */
    status = OK;
    for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
	if (((flags & MFS_ALL) || hp->bh_bnum >= 0)
		&& (hp->bh_flags & BH_DIRTY)
		&& (status == OK || (hp->bh_bnum >= 0
		    && hp->bh_bnum < mfp->mf_infile_count)))
	{
	    if ((flags & MFS_ZERO) && hp->bh_bnum != 0)
		continue;
	    if (mf_write(mfp, hp) == FAIL)
	    {
		if (status == FAIL)	/* double error: quit syncing */
		    break;
		status = FAIL;
	    }
	    if (flags & MFS_STOP)
	    {
		/* Stop when char available now. */
		if (ui_char_avail())
		    break;
	    }
	    else
		ui_breakcheck();
	    if (got_int)
		break;
	}

    /*
     * If the whole list is flushed, the memfile is not dirty anymore.
     * In case of an error this flag is also set, to avoid trying all the time.
     */
    if (hp == NULL || status == FAIL)
	mfp->mf_dirty = FALSE;

    if ((flags & MFS_FLUSH) && *p_sws != NUL)
    {
#if defined(UNIX)
# ifdef HAVE_FSYNC
	/*
	 * most Unixes have the very useful fsync() function, just what we need.
	 * However, with OS/2 and EMX it is also available, but there are
	 * reports of bad problems with it (a bug in HPFS.IFS).
	 * So we disable use of it here in case someone tries to be smart
	 * and changes os_os2_cfg.h... (even though there is no __EMX__ test
	 * in the #if, as __EMX__ does not have sync(); we hope for a timely
	 * sync from the system itself).
	 */
#  if defined(__EMX__)
   error "Don't use fsync with EMX! Read emxdoc.doc or emxfix01.doc for info."
#  endif
	if (STRCMP(p_sws, "fsync") == 0)
	{
	    if (fsync(mfp->mf_fd))
		status = FAIL;
	}
	else
# endif
	    /* OpenNT is strictly POSIX (Benzinger) */
	    /* Tandem/Himalaya NSK-OSS doesn't have sync() */
	    /* No sync() on Stratus VOS */
# if defined(__OPENNT) || defined(__TANDEM) || defined(__VOS__)
	    fflush(NULL);
# else
	    sync();
# endif
#endif
#ifdef VMS
	if (STRCMP(p_sws, "fsync") == 0)
	{
	    if (fsync(mfp->mf_fd))
		status = FAIL;
	}
#endif
#ifdef SYNC_DUP_CLOSE
	/*
	 * Win32 is a bit more work: Duplicate the file handle and close it.
	 * This should flush the file to disk.
	 */
	if ((fd = dup(mfp->mf_fd)) >= 0)
	    close(fd);
#endif
#ifdef AMIGA
# if defined(__AROS__) || defined(__amigaos4__)
	if (fsync(mfp->mf_fd) != 0)
	    status = FAIL;
# else
	/*
	 * Flush() only exists for AmigaDos 2.0.
	 * For 1.3 it should be done with close() + open(), but then the risk
	 * is that the open() may fail and lose the file....
	 */
#  ifdef FEAT_ARP
	if (dos2)
#  endif
#  ifdef SASC
	{
	    struct UFB *fp = chkufb(mfp->mf_fd);

	    if (fp != NULL)
		Flush(fp->ufbfh);
	}
#  else
#   if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__)
	{
#    if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__)
	    /* Have function (in libnix at least),
	     * but ain't got no prototype anywhere. */
	    extern unsigned long fdtofh(int filedescriptor);
#    endif
#    if !defined(__libnix__)
	    fflush(NULL);
#    else
	    BPTR fh = (BPTR)fdtofh(mfp->mf_fd);

	    if (fh != 0)
		Flush(fh);
#    endif
	}
#   else /* assume Manx */
	    Flush(_devtab[mfp->mf_fd].fd);
#   endif
#  endif
# endif
#endif /* AMIGA */
    }
int main(int argc, char *argv[])
{
  int status;
  unsigned char *p;
  int k;

  TSTTITLE("TESTING WRITE MIDIFILE");
  TSTSECTION("Buffer write function");

  TSTGROUP("Buffer sizing");

  status = chk_evt_buf(16);
  TST("Room allocated", (status == 0 && (evt_buf_sz - evt_buf_wm) >= 1024));

  TSTGROUP("Buffer writing");
  p=evt_buf;
  b_write8(0xAB);
  b_write7(0xCD);
  
  #if 0
  b_write16(0xFEDA); /* 1111 1110 1101 1010*/
  b_write14(0xFEDA);
  b_write32(0xFEEDBAC0);
  #endif

  TST("Write  8 bits",(p[0] == 0xAB));
  TST("Write  7 bits",(p[1] == (0xCD & 0x7F)));
  #if 0
  TST("Write 16 bits",(p[2] == 0xFE && p[3] == 0xDA)); /*1111 1110 1101 1010*/
  TST("Write 14 bits",(p[4] == 0x7D && p[5] == 0x5A)); /*0111 1101 0101 1010*/
  TSTONFAIL("Bytes written: %02x %02x",p[4],p[5]);
  #endif

  TSTGROUP("Raw write track");
  h_writetrack = (t_writetrack) mywritetrack1;
  h_error = (t_error) mf_null_handler;

  status = write_track(1);
  TST("write_track() returned unsuccessfully",(status == 309));
  TSTONFAIL("return code: %d\n",status);
  midi_file = fopen("test_file.mid","wb");
  status = write_track(1);
  TST("write_track() returned successfully",(status == 0));
  fclose(midi_file);

  midi_file = fopen("test_file.mid","rb");
  TST("File reopened for read",(midi_file != NULL));

  if (midi_file) {
    /* Now, if everything went well the file should contain:
    *   MTrk 00 00 00 0C 00 90 3C 64 60 3E 64 83 60 FF 2F 00
    */
    TSTWRITE("#");
    p = "MTrk\0\0\0\x0C\0\x90\x3C\x64\x60\x3E\x64\x83\x60\xFF\x2F\0";
    for (k=0; k<20; k++) {
      if (p[k] != fgetc(midi_file)) break;
      TSTWRITE(" %02X",p[k]);
    }
    TSTWRITE("\n");
    TST("File written correctly",(k == 20));

    fclose(midi_file);
    midi_file = NULL;
  }

  h_writetrack = (t_writetrack) mywritetrack2;
  status = mf_write("test_file.mid",0,1,96);
  TST("mf_write succeeded",(status == 0));

  TSTDONE();

  if (midi_file) fclose(midi_file);

  return (0);
}
//串口接收数据保存在文件中
//轮询timeout次还没数据读入就退出
//cycletime 读文件时轮询扫描的周期 单位ms
void m_creatFile(char * pathname ,int timeout,u16 cycletime)
{
		u8 t=0;	
		mf_open((u8*)pathname,FA_OPEN_ALWAYS| FA_READ|FA_WRITE);
	
		while(1)
		{
				/*
				---------------------串口状态-------------------------
						//串口1中断服务程序
						//注意,读取USARTx->SR能避免莫名其妙的错误   	
						//u8 USART_RX_BUF[USART_REC_LEN];     //接收缓冲,最大USART_REC_LEN个字节.
						//接收状态
						//bit15,	接收完成标志
						//bit14,	接收到0x0d
						//bit13~0,	接收到的有效字节数目
						//u16 USART_RX_STA=0;       //接收状态标记	
				---------------------FATFS函数-------------------------
						//void mymemset(void *s,u8 c,u32 count);	//设置内存
						//void mymemcpy(void *des,void *src,u32 n);//复制内存
						//void myfree(u8 memx,void *ptr);  			//内存释放(外部调用)		
						//u8 mf_write(u8*dat,u16 len);

				*/
				
				//成功读入一行数据
				if(USART_RX_STA&0x8000)
				{

						//串口收到数据长度
						int len = USART_RX_STA&0x3fff; 
					
						//开辟空间
						char * conn_recv = (char*)mymalloc(SRAMIN,len+1);
						mymemset(conn_recv,0,len+1);	//设置内存
						
						//取串口的数据 
						mymemcpy(conn_recv, USART_RX_BUF,len);
				


						//显示到屏幕
						com_statusBar(conn_recv);
					
						//发送$$开始的字符串表示文件接收完成
						if(conn_recv[0]=='$'&&conn_recv[1]=='$')
						{
								//关闭文件
								mf_close();
								//释放内存
								myfree(SRAMIN,conn_recv); 
								//状态复位
								USART_RX_STA=0;
								//break;
								return ;
						}
						//状态复位
						USART_RX_STA=0;
					
						//串口接收到的数据保存到本地
						mf_write((u8*)conn_recv,len);
						//写换行符\n
						mf_write((u8*)"\n",1);
						
						//释放内存
						myfree(SRAMIN,conn_recv); 
						
						t=0;
				}
			
				t++; 
				//超时退出
				if(t == timeout){
					return;
				}
				//串口轮询时间与等于200ms
				delay_ms(cycletime);		 			   
				LED0=!LED0;
		} 
}