Esempio n. 1
0
/************************************************************************
int Ui_waiter()
服务员界面
************************************************************************/
int Ui_waiter()
{  
	pList pTable;
	int n;
    FILE *fp;
	void (*pst[5])(pList pTable) = {Open_desk,Order_dishes,Settle_Accounts,turnover_search,Order_search};
	fp = File_open("./Date/Table.txt");
    pTable= File_read(fp,sizeof(struct Table_t));
	while(1)
	{	
		system("cls");
		printf("\n\n\n\t\t\t服务员功能选择界面\n\n");
		printf("\t\t\t1、开桌\n\n");
		printf("\t\t\t2、点菜\n\n");
		printf("\t\t\t3、结账\n\n");
		printf("\t\t\t4、营业额查询\n\n");
		printf("\t\t\t5、已买/未买单查询\n\n");
		printf("\t\t\t6、注销\n\n");
		printf("\t\t\t7、退出\n\n");
		printf("\t\t\t请输入您所要选择的功能:");
		n = glb_putString(NULL,'1','7',1,2);
		if (n > 0 && n <6)
		{
			pst[n-1](pTable);
		}
		else if(n == 6)
			return 0;
		else if (n == 7)
			return 1;		    				
	}	
	   
	  
}
Esempio n. 2
0
void Staff_manage()
{
	char dirname[] = "./Date/Staff.txt";
	FILE *fp;
	pList head;
	int n,count;
	fp = File_open(dirname);
	head = File_read(fp,sizeof(struct staff_t));
	count = List_count(head);
	while(1)
	{	 
		system("cls");
		printf("\n\n\n\t\t\t员工管理界面\n\n");
		printf("\t\t\t1、增加员工\n\n"); 
		printf("\t\t\t2、删除员工\n\n");
		printf("\t\t\t3、修改员工\n\n"); 
		printf("\t\t\t4、查询员工\n\n"); 
		printf("\t\t\t5、返回\n\n"); 
		printf("\t\t\t请输入要操作的功能:");
		n = glb_putString(NULL,'1','5',1,2);
		switch(n)
		{
		case 1:	
			List_print(head,staff_print);
			File_add(fp,head,sizeof(struct staff_t),staff_add);
			List_print(head,staff_print);		
			break;
		case 2:
			List_print(head,staff_print);
			File_remove(fp,head,sizeof(struct staff_t),staff_remove);
			head = File_read(fp,sizeof(struct staff_t));
			List_print(head,staff_print);			
			break;
		case 3:
			List_print(head,staff_print);
			File_updata(fp,head,sizeof(struct staff_t),staff_updata);
			head = File_read(fp,sizeof(struct staff_t));
			List_print(head,staff_print);		
			break;
		case 4:  
			staff_search(head);			
			break;
		case 5:
			break;
		}
		if (n == 5)
		{
			break;
		}
		else
		{
            printf("\n按<Esc>键返回...");
			while(getch() != 27)
			{}
		}
		
		
	}
	List_free(head);
}
Esempio n. 3
0
void Menu_manage()
{
	char dirname[] = "./Date/Menu.txt";
	FILE *fp;
	pList head;
	int n;
	fp = File_open(dirname);
	head = File_read(fp,sizeof(struct Menu_t));
	while(1)
	{	 
		system("cls");
		printf("\n\n\n\t\t\t菜谱管理界面\n\n");
		printf("\t\t\t1、增加菜谱\n\n"); 
		printf("\t\t\t2、删除菜谱\n\n");
		printf("\t\t\t3、修改菜谱\n\n"); 
		printf("\t\t\t4、查询菜谱\n\n"); 
		printf("\t\t\t5、返回\n\n"); 
		printf("\t\t\t请输入您要操作的功能:");
	    n = glb_putString(NULL,'1','5',1,2);
		switch(n)
		{
		case 1:	
			List_print(head,Menu_print);
			File_add(fp,head,sizeof(struct Menu_t),Menu_add);
			head = File_read(fp,sizeof(struct Menu_t));
			List_print(head,Menu_print);
			break;
		case 2:
			List_print(head,Menu_print);
			File_del(fp,dirname,head,sizeof(struct Menu_t),Menu_del);
			List_print(head,Menu_print);
			break;
		case 3:
			List_print(head,Menu_print);
			File_updata(fp,head,sizeof(struct Menu_t),Menu_updata);
		    head = File_read(fp,sizeof(struct Menu_t));
			List_print(head,Menu_print);
			break;
		case 4: 
	
		    Menu_search(head);
			break;
		case 5:
			break;
		}
		if (n == 5)
		{
			break;
		}
		else
		{
            printf("\n按<Esc>键返回...");
			while(getch() != 27)
			{}
		}
		
	}
	  List_free(head);
}
Esempio n. 4
0
void Table_manage()
{
	char dirname[] = "./Date/Table.txt";
	FILE *fp;
	pList head;
	int n;
	fp = File_open(dirname);
	head = File_read(fp,sizeof(struct Table_t));
	while(1)
	{	 
		system("cls");
		printf("\n\n\n\t\t\t台桌管理界面\n\n");
		printf("\t\t\t1、增加台桌\n\n"); 
		printf("\t\t\t2、删除台桌\n\n");
		printf("\t\t\t3、修改台桌\n\n"); 
		printf("\t\t\t4、查询台桌\n\n"); 
		printf("\t\t\t5、返回\n\n"); 
		printf("\t\t\t请输入您要操作的功能:");
	    n = glb_putString(NULL,'1','5',1,2);
		switch(n)
		{
		case 1:	
			List_print(head,Table_print);
			File_add(fp,head,sizeof(struct Table_t),Table_add);
			head = File_read(fp,sizeof(struct Table_t));
			List_print(head,Table_print);			
			break;
		case 2:
			List_print(head,Table_print);
			File_remove(fp,head,sizeof(struct Table_t),Table_remove);
			head = File_read(fp,sizeof(struct Table_t));
			List_print(head,Table_print);
			break;
		case 3:
			List_print(head,Table_print);
			File_updata(fp,head,sizeof(struct Table_t),Table_updata);
		    head = File_read(fp,sizeof(struct Table_t));
			List_print(head,Table_print);
			break;
		case 4: 
		    Table_search(head,3,4);
			break;
		case 5:
			break;
		}
		if (n == 5)
		{
			break;
		}
		else
		{
            printf("\n按<Esc>键返回...");
			while(getch() != 27)
			{}
		}
		
	}
	  List_free(head);
}
Esempio n. 5
0
/************************************************************************
void turnover_search(pList  pTable)
营业额查询
************************************************************************/
void turnover_search(pList  pTable)
{
  FILE *fp;
  pList pOrder,pt;
  struct Order_t *data;
  long *stardate,*enddate;
  long ordernum;// 订单号
  int a,b,c;// 用来打印年月日
  long money = 0;// 总营业额
  stardate = (long *)malloc(sizeof(long));// 开始日期
  enddate = (long *)malloc(sizeof(long));// 结束日期
  fp = File_open("./Date/Order/Order.txt");
  pOrder = File_read(fp,sizeof(struct Order_t));// 读出已买订单链表

  system("cls");
  printf("\n\t\t\t(1):当天查询\n");
  printf("\n\t\t\t(2):历史查询\n");
  printf("\n\t\t\t请输入要选择的功能:");
  if (glb_putString(NULL,'1','2',1,2) == 1)
  {
	  system("cls");
	  *stardate = Ordernum_get(NULL,NULL);// 获取当前系统日期
	  *enddate = *stardate;
  }
  else
  {
      Orderdate_input(stardate,enddate);// 输入开始和结束日期
	  printf("\n");
  }
 
  printf("\t\t%-16s%-18s%-11s\n","订单号","时间","金额");
  pt = pOrder->pNext;
  while (pt)
  {
	  data = (struct Order_t *)(pt->pData);
	  sscanf(data->ordernum,"%8ld",&ordernum);// 字符串转化成长整型
	  if ((strcmp(data->acname,acName) == 0)&&((ordernum <= *enddate) && (ordernum >= *stardate)))//
	  {
		  money = money + (data->money);// 营业额累加
         sscanf(data->ordernum,"%4d%2d%2d",&a,&b,&c);//分别取出年月日
		 printf("\t\t%-16s%d%s%2d%s%2d%-6s%s%d\n",data->ordernum,a,"年",b,"月",c,"日","¥",data->money);		 
	  }
	  pt = pt->pNext;
  } 
      printf("\n\t\t营业额总额:¥%ld\n",money);
      List_free(pOrder);
	  printf("\n\t\t按任意键返回...");
	  getch();
}
Esempio n. 6
0
//Config file
void loadConfigFile()
{
	printf("Start load config file\n");
	TSFile fileHandle;
	const int bufferSize = 1000;
	char buffer[bufferSize];
	memset(buffer,'\0', bufferSize);


	File_open(&fileHandle,"C:\\Users\\loki.chuang\\Desktop\\MobileDesc.txt", O_RDONLY);
	File_read(&fileHandle, buffer, bufferSize);
	File_close(&fileHandle);

	printAllChar(buffer, bufferSize);
}
Esempio n. 7
0
void init(char *name){

    WindowWidth = lcd_getwidth()/8;
    WindowHeight = lcd_getheight()/16;

    cursor.relative.x = 0;
    cursor.relative.y = 0;
    cursor.absolute.x = 0;
    cursor.absolute.y = 0;

    File_open(name);
    cursor.str = head;

    View_redraw(&cursor);
    lcd_locate(0, 0);
}
Esempio n. 8
0
int File_copyTo( const IFile* self, const IPath* targetPath, int force )
{
	bool status = 0;

	char* target = NULL;
	IFile* target_file = NULL;
	IDirectory* target_dir = new_Directory_path( targetPath );
	if ( Directory_exists( target_dir ) )
	{
		target = CharString_cat3( Path_getCommon( targetPath ), "/", Path_getBasename( self->path ) );
	} else {
		target = CharString_copy( Path_getCommon( targetPath ) );
	}

	target_file = new_File( target );
	if ( !File_exists( target_file ) || force )
	{
		if ( File_open( target_file, "w" ) )
		{
			byte buffer[256];
			int read;
		
			while ( (read = File_read( self, buffer, 256 )) )
			{
				File_write( target_file, buffer, read );
				status = 1;
			}
			if ( !status )
			{
				fprintf( stdout, "File.c::copyTo: could not copy data\n" );
			}
		}
		File_close( target_file );
	}		
	free_Directory( target_dir );
	free_File( target_file );
	free_CharString( target );	

	return status;
}
Esempio n. 9
0
/************************************************************************
void Order_search(pList  pTable)
已买/未买单查询
************************************************************************/
void Order_search(pList  pTable)
{
	int n;
	FILE *fp;
	pList pOrder;
	fp = File_open("./Date/Cache/Order.txt");
    pOrder = File_read(fp,sizeof(struct Order_t));// 读出未买单链表
    while (1)
    {
    system("cls");
	printf("\n\n\n\t\t\t已买/未买单查询界面\n\n");
	printf("\t\t\t1、未买单查询\n\n");
	printf("\t\t\t2、已买单查询\n\n");
	printf("\t\t\t3、退出\n\n");
	printf("\t\t\t请输入您所要选择的功能:");
	n = glb_putString(NULL,'1','3',1,2);
	switch(n)
	{
	case 1:
		Order_search_unbuy(pOrder);// 未买单查询函数 
		getch();
		break;
	case 2:
		Order_search_buy();// 已买单查询函数
		getch();
		break;
	case 3:
		
		break;
	}
	 if (n == 3)
	 {
		break;
	 }
   
	}
     List_free(pOrder);// 释放链表
}
Esempio n. 10
0
struct PointArray* closest_pair(char *filename){
    struct Connection *conn = File_open(filename, "rt");
    struct PointArray *point_array = Connection_process(conn);
    Connection_close(conn);
    struct PointArraySplitted *split_array = split_point_array(point_array);

    struct OrderedPointArray *ordered1 = malloc(sizeof(struct OrderedPointArray));
    ordered1->x_ordered = split_array->array1;
    ordered1->y_ordered = split_array->array1;

    struct OrderedPointArray *ordered2 = malloc(sizeof(struct OrderedPointArray));
    ordered2->x_ordered = split_array->array2;
    ordered2->y_ordered = split_array->array2;

    struct PointArray *result;
    struct OrderedPointArray *result1 = split_sort(ordered1);
    struct OrderedPointArray *result2 = split_sort(ordered2);

    struct OrderedPointArray *result_merged = merge_point_array(result1, result2);
    divide_conquer(result_merged->x_ordered, result_merged->y_ordered);

    return result;
}
Esempio n. 11
0
Errors Command_compare(StringList                      *archiveFileNameList,
                       EntryList                       *includeEntryList,
                       PatternList                     *excludePatternList,
                       JobOptions                      *jobOptions,
                       ArchiveGetCryptPasswordFunction archiveGetCryptPasswordFunction,
                       void                            *archiveGetCryptPasswordUserData
                      )
{
  byte              *archiveBuffer,*buffer;
  FragmentList      fragmentList;
  String            archiveFileName;
  Errors            failError;
  Errors            error;
  ArchiveInfo       archiveInfo;
  ArchiveFileInfo   archiveFileInfo;
  ArchiveEntryTypes archiveEntryType;
  FragmentNode      *fragmentNode;

  assert(archiveFileNameList != NULL);
  assert(includeEntryList != NULL);
  assert(excludePatternList != NULL);
  assert(jobOptions != NULL);

  /* allocate resources */
  archiveBuffer = (byte*)malloc(BUFFER_SIZE);
  if (archiveBuffer == NULL)
  {
    HALT_INSUFFICIENT_MEMORY();
  }
  buffer = malloc(BUFFER_SIZE);
  if (buffer == NULL)
  {
    free(archiveBuffer);
    HALT_INSUFFICIENT_MEMORY();
  }
  FragmentList_init(&fragmentList);
  archiveFileName = String_new();

  failError = ERROR_NONE;
  while (   !StringList_empty(archiveFileNameList)
         && (failError == ERROR_NONE)
        )
  {
    StringList_getFirst(archiveFileNameList,archiveFileName);
    printInfo(1,"Comparing archive '%s':\n",String_cString(archiveFileName));

    /* open archive */
    error = Archive_open(&archiveInfo,
                         archiveFileName,
                         jobOptions,
                         archiveGetCryptPasswordFunction,
                         archiveGetCryptPasswordUserData
                        );
    if (error != ERROR_NONE)
    {
      printError("Cannot open archive file '%s' (error: %s)!\n",
                 String_cString(archiveFileName),
                 Errors_getText(error)
                );
      if (failError == ERROR_NONE) failError = error;
      continue;
    }

    /* read files */
    while (   !Archive_eof(&archiveInfo)
           && (failError == ERROR_NONE)
          )
    {
      /* get next archive entry type */
      error = Archive_getNextArchiveEntryType(&archiveInfo,
                                              &archiveFileInfo,
                                              &archiveEntryType
                                             );
      if (error != ERROR_NONE)
      {
        printError("Cannot not read next entry in archive '%s' (error: %s)!\n",
                   String_cString(archiveFileName),
                   Errors_getText(error)
                  );
        if (failError == ERROR_NONE) failError = error;
        break;
      }

      switch (archiveEntryType)
      {
        case ARCHIVE_ENTRY_TYPE_FILE:
          {
            String       fileName;
            FileInfo     fileInfo;
            uint64       fragmentOffset,fragmentSize;
            FragmentNode *fragmentNode;
//            FileInfo   localFileInfo;
            FileHandle   fileHandle;
            bool         equalFlag;
            uint64       length;
            ulong        n;
            ulong        diffIndex;

            /* read file */
            fileName = String_new();
            error = Archive_readFileEntry(&archiveInfo,
                                          &archiveFileInfo,
                                          NULL,
                                          NULL,
                                          NULL,
                                          fileName,
                                          &fileInfo,
                                          &fragmentOffset,
                                          &fragmentSize
                                         );
            if (error != ERROR_NONE)
            {
              printError("Cannot not read 'file' content of archive '%s' (error: %s)!\n",
                         String_cString(archiveFileName),
                         Errors_getText(error)
                        );
              String_delete(fileName);
              if (failError == ERROR_NONE) failError = error;
              break;
            }

            if (   (List_empty(includeEntryList) || EntryList_match(includeEntryList,fileName,PATTERN_MATCH_MODE_EXACT))
                && !PatternList_match(excludePatternList,fileName,PATTERN_MATCH_MODE_EXACT)
               )
            {
              printInfo(2,"  Compare file '%s'...",String_cString(fileName));

              /* check file */
              if (!File_exists(fileName))
              {
                printInfo(2,"FAIL!\n");
                printError("File '%s' not found!\n",String_cString(fileName));
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = ERROR_FILE_NOT_FOUND;
                }
                break;
              }
              if (File_getType(fileName) != FILE_TYPE_FILE)
              {
                printInfo(2,"FAIL!\n");
                printError("'%s' is not a file!\n",String_cString(fileName));
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = ERROR_WRONG_FILE_TYPE;
                }
                break;
              }

              /* get file fragment list */
              fragmentNode = FragmentList_find(&fragmentList,fileName);
              if (fragmentNode == NULL)
              {
                fragmentNode = FragmentList_add(&fragmentList,fileName,fileInfo.size);
              }
//FragmentList_print(fragmentNode,String_cString(fileName));

              /* open file */
              error = File_open(&fileHandle,fileName,FILE_OPENMODE_READ);
              if (error != ERROR_NONE)
              {
                printInfo(2,"FAIL!\n");
                printError("Cannot open file '%s' (error: %s)\n",
                           String_cString(fileName),
                           Errors_getText(error)
                          );
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = error;
                }
                continue;
              }

              /* check file size */
              if (fileInfo.size != File_getSize(&fileHandle))
              {
                printInfo(2,"FAIL!\n");
                printError("'%s' differ in size: expected %lld bytes, found %lld bytes\n",
                           String_cString(fileName),
                           fileInfo.size,
                           File_getSize(&fileHandle)
                          );
                File_close(&fileHandle);
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = ERROR_FILES_DIFFER;
                }
                continue;
              }

              /* check file content */
              error = File_seek(&fileHandle,fragmentOffset);
              if (error != ERROR_NONE)
              {
                printInfo(2,"FAIL!\n");
                printError("Cannot read file '%s' (error: %s)\n",
                           String_cString(fileName),
                           Errors_getText(error)
                          );
                File_close(&fileHandle);
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = error;
                }
                continue;
              }
              length    = 0;
              equalFlag = TRUE;
              diffIndex = 0;
              while ((length < fragmentSize) && equalFlag)
              {
                n = MIN(fragmentSize-length,BUFFER_SIZE);

                /* read archive, file */
                error = Archive_readData(&archiveFileInfo,archiveBuffer,n);
                if (error != ERROR_NONE)
                {
                  printInfo(2,"FAIL!\n");
                  printError("Cannot not read content of archive '%s' (error: %s)!\n",
                             String_cString(archiveFileName),
                             Errors_getText(error)
                            );
                  if (failError == ERROR_NONE) failError = error;
                  break;
                }
                error = File_read(&fileHandle,buffer,n,NULL);
                if (error != ERROR_NONE)
                {
                  printInfo(2,"FAIL!\n");
                  printError("Cannot read file '%s' (error: %s)\n",
                             String_cString(fileName),
                             Errors_getText(error)
                            );
                  if (jobOptions->stopOnErrorFlag)
                  {
                    failError = error;
                  }
                  break;
                }

                /* compare */
                diffIndex = compare(archiveBuffer,buffer,n);
                equalFlag = (diffIndex >= n);
                if (!equalFlag)
                {
                  printInfo(2,"FAIL!\n");
                  printError("'%s' differ at offset %llu\n",
                             String_cString(fileName),
                             fragmentOffset+length+(uint64)diffIndex
                            );
                  if (jobOptions->stopOnErrorFlag)
                  {
                    failError = ERROR_FILES_DIFFER;
                  }
                  break;
                }

                length += n;
              }
              File_close(&fileHandle);
              if (failError != ERROR_NONE)
              {
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
                continue;
              }

#if 0
              /* get local file info */
              /* check file time, permissions, file owner/group */
#endif /* 0 */
              printInfo(2,"ok\n");

              /* add fragment to file fragment list */
              FragmentList_addEntry(fragmentNode,fragmentOffset,fragmentSize);

              /* discard fragment list if file is complete */
              if (FragmentList_checkEntryComplete(fragmentNode))
              {
                FragmentList_remove(&fragmentList,fragmentNode);
              }

              /* free resources */
            }
            else
            {
              /* skip */
              printInfo(3,"  Compare '%s'...skipped\n",String_cString(fileName));
            }

            /* close archive file, free resources */
            Archive_closeEntry(&archiveFileInfo);
            String_delete(fileName);
          }
          break;
        case ARCHIVE_ENTRY_TYPE_IMAGE:
          {
            String       imageName;
            DeviceInfo   deviceInfo;
            uint64       blockOffset,blockCount;
            FragmentNode *fragmentNode;
            DeviceHandle deviceHandle;
            bool         equalFlag;
            uint64       block;
            ulong        bufferBlockCount;
            ulong        diffIndex;

            /* read image */
            imageName = String_new();
            error = Archive_readImageEntry(&archiveInfo,
                                           &archiveFileInfo,
                                           NULL,
                                           NULL,
                                           NULL,
                                           imageName,
                                           &deviceInfo,
                                           &blockOffset,
                                           &blockCount
                                          );
            if (error != ERROR_NONE)
            {
              printError("Cannot not read 'image' content of archive '%s' (error: %s)!\n",
                         String_cString(archiveFileName),
                         Errors_getText(error)
                        );
              String_delete(imageName);
              if (failError == ERROR_NONE) failError = error;
              break;
            }

            if (   (List_empty(includeEntryList) || EntryList_match(includeEntryList,imageName,PATTERN_MATCH_MODE_EXACT))
                && !PatternList_match(excludePatternList,imageName,PATTERN_MATCH_MODE_EXACT)
               )
            {
              printInfo(2,"  Compare image '%s'...",String_cString(imageName));

              /* check if device exists */
              if (!File_exists(imageName))
              {
                printInfo(2,"FAIL!\n");
                printError("Device '%s' not found!\n",String_cString(imageName));
                Archive_closeEntry(&archiveFileInfo);
                String_delete(imageName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = ERROR_FILE_NOT_FOUND;
                }
                break;
              }

              /* get image fragment list */
              fragmentNode = FragmentList_find(&fragmentList,imageName);
              if (fragmentNode == NULL)
              {
                fragmentNode = FragmentList_add(&fragmentList,imageName,deviceInfo.size);
              }

              /* open device */
              error = Device_open(&deviceHandle,imageName,DEVICE_OPENMODE_READ);
              if (error != ERROR_NONE)
              {
                printInfo(2,"FAIL!\n");
                printError("Cannot open file '%s' (error: %s)\n",
                           String_cString(imageName),
                           Errors_getText(error)
                          );
                Archive_closeEntry(&archiveFileInfo);
                String_delete(imageName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = error;
                }
                continue;
              }

              /* check image size */
              if (deviceInfo.size != Device_getSize(&deviceHandle))
              {
                printInfo(2,"FAIL!\n");
                printError("'%s' differ in size: expected %lld bytes, found %lld bytes\n",
                           String_cString(imageName),
                           deviceInfo.size,
                           Device_getSize(&deviceHandle)
                          );
                Device_close(&deviceHandle);
                Archive_closeEntry(&archiveFileInfo);
                String_delete(imageName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = ERROR_FILES_DIFFER;
                }
                continue;
              }

              /* check image content */
              error = Device_seek(&deviceHandle,blockOffset*(uint64)deviceInfo.blockSize);
              if (error != ERROR_NONE)
              {
                printInfo(2,"FAIL!\n");
                printError("Cannot read file '%s' (error: %s)\n",
                           String_cString(imageName),
                           Errors_getText(error)
                          );
                Device_close(&deviceHandle);
                Archive_closeEntry(&archiveFileInfo);
                String_delete(imageName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = error;
                }
                continue;
              }
              block     = 0LL;
              equalFlag = TRUE;
              diffIndex = 0;
              while ((block < blockCount) && equalFlag)
              {
                assert(deviceInfo.blockSize > 0);
                bufferBlockCount = MIN(blockCount-block,BUFFER_SIZE/deviceInfo.blockSize);

                /* read archive, file */
                error = Archive_readData(&archiveFileInfo,archiveBuffer,bufferBlockCount*deviceInfo.blockSize);
                if (error != ERROR_NONE)
                {
                  printInfo(2,"FAIL!\n");
                  printError("Cannot not read content of archive '%s' (error: %s)!\n",
                             String_cString(archiveFileName),
                             Errors_getText(error)
                            );
                  if (failError == ERROR_NONE) failError = error;
                  break;
                }
                error = Device_read(&deviceHandle,buffer,bufferBlockCount*deviceInfo.blockSize,NULL);
                if (error != ERROR_NONE)
                {
                  printInfo(2,"FAIL!\n");
                  printError("Cannot read file '%s' (error: %s)\n",
                             String_cString(imageName),
                             Errors_getText(error)
                            );
                  if (jobOptions->stopOnErrorFlag)
                  {
                    failError = error;
                  }
                  break;
                }

                /* compare */
                diffIndex = compare(archiveBuffer,buffer,bufferBlockCount*deviceInfo.blockSize);
                equalFlag = (diffIndex >= bufferBlockCount*deviceInfo.blockSize);
                if (!equalFlag)
                {
                  printInfo(2,"FAIL!\n");
                  printError("'%s' differ at offset %llu\n",
                             String_cString(imageName),
                             blockOffset*(uint64)deviceInfo.blockSize+block*(uint64)deviceInfo.blockSize+(uint64)diffIndex
                            );
                  if (jobOptions->stopOnErrorFlag)
                  {
                    failError = ERROR_FILES_DIFFER;
                  }
                  break;
                }

                block += (uint64)bufferBlockCount;
              }
              Device_close(&deviceHandle);
              if (failError != ERROR_NONE)
              {
                Archive_closeEntry(&archiveFileInfo);
                String_delete(imageName);
                continue;
              }

#if 0
              /* get local file info */
              /* check file time, permissions, file owner/group */
#endif /* 0 */
              printInfo(2,"ok\n");

              /* add fragment to file fragment list */
              FragmentList_addEntry(fragmentNode,blockOffset*(uint64)deviceInfo.blockSize,blockCount*(uint64)deviceInfo.blockSize);

              /* discard fragment list if file is complete */
              if (FragmentList_checkEntryComplete(fragmentNode))
              {
                FragmentList_remove(&fragmentList,fragmentNode);
              }

              /* free resources */
            }
            else
            {
              /* skip */
              printInfo(3,"  Compare '%s'...skipped\n",String_cString(imageName));
            }

            /* close archive file, free resources */
            Archive_closeEntry(&archiveFileInfo);
            String_delete(imageName);
          }
          break;
        case ARCHIVE_ENTRY_TYPE_DIRECTORY:
          {
            String   directoryName;
            FileInfo fileInfo;
//            String   localFileName;
//            FileInfo localFileInfo;

            /* read directory */
            directoryName = String_new();
            error = Archive_readDirectoryEntry(&archiveInfo,
                                               &archiveFileInfo,
                                               NULL,
                                               NULL,
                                               directoryName,
                                               &fileInfo
                                              );
            if (error != ERROR_NONE)
            {
              printError("Cannot not read 'directory' content of archive '%s' (error: %s)!\n",
                         String_cString(archiveFileName),
                         Errors_getText(error)
                        );
              String_delete(directoryName);
              if (failError == ERROR_NONE) failError = error;
              break;
            }

            if (   (List_empty(includeEntryList) || EntryList_match(includeEntryList,directoryName,PATTERN_MATCH_MODE_EXACT))
                && !PatternList_match(excludePatternList,directoryName,PATTERN_MATCH_MODE_EXACT)
               )
            {
              printInfo(2,"  Compare directory '%s'...",String_cString(directoryName));

              /* check directory */
              if (!File_exists(directoryName))
              {
                printInfo(2,"FAIL!\n");
                printError("Directory '%s' does not exists!\n",String_cString(directoryName));
                Archive_closeEntry(&archiveFileInfo);
                String_delete(directoryName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = ERROR_FILE_NOT_FOUND;
                }
                break;
              }
              if (File_getType(directoryName) != FILE_TYPE_DIRECTORY)
              {
                printInfo(2,"FAIL!\n");
                printError("'%s' is not a directory!\n",
                           String_cString(directoryName)
                          );
                Archive_closeEntry(&archiveFileInfo);
                String_delete(directoryName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = ERROR_WRONG_FILE_TYPE;
                }
                break;
              }

#if 0
              /* get local file info */
              error = File_getFileInfo(directoryName,&localFileInfo);
              if (error != ERROR_NONE)
              {
                printError("Cannot not read local directory '%s' (error: %s)!\n",
                           String_cString(directoryName),
                           Errors_getText(error)
                          );
                Archive_closeEntry(&archiveFileInfo);
                String_delete(directoryName);
                if (failError == ERROR_NONE) failError = error;
                break;
              }

              /* check file time, permissions, file owner/group */
#endif /* 0 */
              printInfo(2,"ok\n");

              /* free resources */
            }
            else
            {
              /* skip */
              printInfo(3,"  Compare '%s'...skipped\n",String_cString(directoryName));
            }

            /* close archive file */
            Archive_closeEntry(&archiveFileInfo);
            String_delete(directoryName);
          }
          break;
        case ARCHIVE_ENTRY_TYPE_LINK:
          {
            String   linkName;
            String   fileName;
            FileInfo fileInfo;
            String   localFileName;
//            FileInfo localFileInfo;

            /* read link */
            linkName = String_new();
            fileName = String_new();
            error = Archive_readLinkEntry(&archiveInfo,
                                          &archiveFileInfo,
                                          NULL,
                                          NULL,
                                          linkName,
                                          fileName,
                                          &fileInfo
                                         );
            if (error != ERROR_NONE)
            {
              printError("Cannot not read 'link' content of archive '%s' (error: %s)!\n",
                         String_cString(archiveFileName),
                         Errors_getText(error)
                        );
              String_delete(fileName);
              String_delete(linkName);
              if (failError == ERROR_NONE) failError = error;
              break;
            }

            if (   (List_empty(includeEntryList) || EntryList_match(includeEntryList,linkName,PATTERN_MATCH_MODE_EXACT))
                && !PatternList_match(excludePatternList,linkName,PATTERN_MATCH_MODE_EXACT)
               )
            {
              printInfo(2,"  Compare link '%s'...",String_cString(linkName));

              /* check link */
              if (!File_exists(linkName))
              {
                printInfo(2,"FAIL!\n");
                printError("Link '%s' -> '%s' does not exists!\n",
                           String_cString(linkName),
                           String_cString(fileName)
                          );
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
                String_delete(linkName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = ERROR_FILE_NOT_FOUND;
                }
                break;
              }
              if (File_getType(linkName) != FILE_TYPE_LINK)
              {
                printInfo(2,"FAIL!\n");
                printError("'%s' is not a link!\n",
                           String_cString(linkName)
                          );
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
                String_delete(linkName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = ERROR_WRONG_FILE_TYPE;
                }
                break;
              }

              /* check link content */
              localFileName = String_new();
              error = File_readLink(linkName,localFileName);
              if (error != ERROR_NONE)
              {
                printError("Cannot not read local file '%s' (error: %s)!\n",
                           String_cString(linkName),
                           Errors_getText(error)
                          );
                String_delete(localFileName);
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
                String_delete(linkName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = error;
                }
                break;
              }
              if (!String_equals(fileName,localFileName))
              {
                printInfo(2,"FAIL!\n");
                printError("Link '%s' does not contain file '%s'!\n",
                           String_cString(linkName),
                           String_cString(fileName)
                          );
                String_delete(localFileName);
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
                String_delete(linkName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = ERROR_FILES_DIFFER;
                }
                break;
              }
              String_delete(localFileName);

#if 0
              /* get local file info */
              error = File_getFileInfo(linkName,&localFileInfo);
              if (error != ERROR_NONE)
              {
                printError("Cannot not read local file '%s' (error: %s)!\n",
                           String_cString(linkName),
                           Errors_getText(error)
                          );
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
                String_delete(linkName);
                if (failError == ERROR_NONE) failError = error;
                break;
              }

              /* check file time, permissions, file owner/group */
#endif /* 0 */
              printInfo(2,"ok\n");

              /* free resources */
            }
            else
            {
              /* skip */
              printInfo(3,"  Compare '%s'...skipped\n",String_cString(linkName));
            }

            /* close archive file */
            Archive_closeEntry(&archiveFileInfo);
            String_delete(fileName);
            String_delete(linkName);
          }
          break;
        case ARCHIVE_ENTRY_TYPE_SPECIAL:
          {
            String   fileName;
            FileInfo fileInfo;
            FileInfo localFileInfo;

            /* read special */
            fileName = String_new();
            error = Archive_readSpecialEntry(&archiveInfo,
                                             &archiveFileInfo,
                                             NULL,
                                             NULL,
                                             fileName,
                                             &fileInfo
                                            );
            if (error != ERROR_NONE)
            {
              printError("Cannot not read 'special' content of archive '%s' (error: %s)!\n",
                         String_cString(archiveFileName),
                         Errors_getText(error)
                        );
              String_delete(fileName);
              if (failError == ERROR_NONE) failError = error;
              break;
            }

            if (   (List_empty(includeEntryList) || EntryList_match(includeEntryList,fileName,PATTERN_MATCH_MODE_EXACT))
                && !PatternList_match(excludePatternList,fileName,PATTERN_MATCH_MODE_EXACT)
               )
            {
              printInfo(2,"  Compare special device '%s'...",String_cString(fileName));

              /* check special device */
              if (!File_exists(fileName))
              {
                printInfo(2,"FAIL!\n");
                printError("Special device '%s' does not exists!\n",
                           String_cString(fileName)
                          );
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = ERROR_FILE_NOT_FOUND;
                }
                break;
              }
              if (File_getType(fileName) != FILE_TYPE_SPECIAL)
              {
                printInfo(2,"FAIL!\n");
                printError("'%s' is not a special device!\n",
                           String_cString(fileName)
                          );
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = ERROR_WRONG_FILE_TYPE;
                }
                break;
              }

              /* check special settings */
              error = File_getFileInfo(fileName,&localFileInfo);
              if (error != ERROR_NONE)
              {
                printError("Cannot not read local file '%s' (error: %s)!\n",
                           String_cString(fileName),
                           Errors_getText(error)
                          );
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = error;
                }
                break;
              }
              if (fileInfo.specialType != localFileInfo.specialType)
              {
                printError("Different types of special device '%s'!\n",
                           String_cString(fileName)
                          );
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
                if (jobOptions->stopOnErrorFlag)
                {
                  failError = error;
                }
                break;
              }
              if (   (fileInfo.specialType == FILE_SPECIAL_TYPE_CHARACTER_DEVICE)
                  || (fileInfo.specialType == FILE_SPECIAL_TYPE_BLOCK_DEVICE)
                 )
              {
                if (fileInfo.major != localFileInfo.major)
                {
                  printError("Different major numbers of special device '%s'!\n",
                             String_cString(fileName)
                            );
                  Archive_closeEntry(&archiveFileInfo);
                  String_delete(fileName);
                  if (jobOptions->stopOnErrorFlag)
                  {
                    failError = error;
                  }
                  break;
                }
                if (fileInfo.minor != localFileInfo.minor)
                {
                  printError("Different minor numbers of special device '%s'!\n",
                             String_cString(fileName)
                            );
                  Archive_closeEntry(&archiveFileInfo);
                  String_delete(fileName);
                  if (jobOptions->stopOnErrorFlag)
                  {
                    failError = error;
                  }
                  break;
                }
              }

#if 0

              /* check file time, permissions, file owner/group */
#endif /* 0 */

              printInfo(2,"ok\n");

              /* free resources */
            }
            else
            {
              /* skip */
              printInfo(3,"  Compare '%s'...skipped\n",String_cString(fileName));
            }

            /* close archive file */
            Archive_closeEntry(&archiveFileInfo);
            String_delete(fileName);
          }
          break;
        default:
          #ifndef NDEBUG
            HALT_INTERNAL_ERROR_UNHANDLED_SWITCH_CASE();
          #endif /* NDEBUG */
          break; /* not reached */
      }
    }

    /* close archive */
    Archive_close(&archiveInfo);
  }

  /* check fragment lists */
  for (fragmentNode = fragmentList.head; fragmentNode != NULL; fragmentNode = fragmentNode->next)
  {
    if (!FragmentList_checkEntryComplete(fragmentNode))
    {
      printInfo(0,"Warning: incomplete entry '%s'\n",String_cString(fragmentNode->name));
      if (failError == ERROR_NONE) failError = ERROR_FILE_INCOMPLETE;
    }
  }

  /* free resources */
  String_delete(archiveFileName);
  FragmentList_done(&fragmentList);
  free(buffer);
  free(archiveBuffer);

  return failError;
}
Esempio n. 12
0
Errors Command_restore(StringList                      *archiveFileNameList,
                       PatternList                     *includePatternList,
                       PatternList                     *excludePatternList,
                       JobOptions                      *jobOptions,
                       ArchiveGetCryptPasswordFunction archiveGetCryptPasswordFunction,
                       void                            *archiveGetCryptPasswordUserData,
                       RestoreStatusInfoFunction       restoreStatusInfoFunction,
                       void                            *restoreStatusInfoUserData,
                       bool                            *pauseFlag,
                       bool                            *requestedAbortFlag
                      )
{
    RestoreInfo       restoreInfo;
    byte              *buffer;
    FileFragmentList  fileFragmentList;
    String            archiveFileName;
    Errors            error;
    ArchiveInfo       archiveInfo;
    ArchiveFileInfo   archiveFileInfo;
    FileTypes         fileType;
    FileFragmentNode  *fileFragmentNode;

    assert(archiveFileNameList != NULL);
    assert(includePatternList != NULL);
    assert(excludePatternList != NULL);
    assert(jobOptions != NULL);

    /* initialize variables */
    restoreInfo.includePatternList           = includePatternList;
    restoreInfo.excludePatternList           = excludePatternList;
    restoreInfo.jobOptions                   = jobOptions;
    restoreInfo.pauseFlag                    = pauseFlag;
    restoreInfo.requestedAbortFlag           = requestedAbortFlag;
    restoreInfo.error                        = ERROR_NONE;
    restoreInfo.statusInfoFunction           = restoreStatusInfoFunction;
    restoreInfo.statusInfoUserData           = restoreStatusInfoUserData;
    restoreInfo.statusInfo.doneFiles         = 0L;
    restoreInfo.statusInfo.doneBytes         = 0LL;
    restoreInfo.statusInfo.skippedFiles      = 0L;
    restoreInfo.statusInfo.skippedBytes      = 0LL;
    restoreInfo.statusInfo.errorFiles        = 0L;
    restoreInfo.statusInfo.errorBytes        = 0LL;
    restoreInfo.statusInfo.fileName          = String_new();
    restoreInfo.statusInfo.fileDoneBytes     = 0LL;
    restoreInfo.statusInfo.fileTotalBytes    = 0LL;
    restoreInfo.statusInfo.storageName       = String_new();
    restoreInfo.statusInfo.storageDoneBytes  = 0LL;
    restoreInfo.statusInfo.storageTotalBytes = 0LL;

    /* allocate resources */
    buffer = malloc(BUFFER_SIZE);
    if (buffer == NULL)
    {
        HALT_INSUFFICIENT_MEMORY();
    }
    FileFragmentList_init(&fileFragmentList);
    archiveFileName = String_new();

    while (   ((restoreInfo.requestedAbortFlag == NULL) || !(*restoreInfo.requestedAbortFlag))
              && !StringList_empty(archiveFileNameList)
              && (restoreInfo.error == ERROR_NONE)
          )
    {
        /* pause */
        while ((restoreInfo.pauseFlag != NULL) && (*restoreInfo.pauseFlag))
        {
            Misc_udelay(500*1000);
        }

        StringList_getFirst(archiveFileNameList,archiveFileName);
        printInfo(0,"Restore archive '%s':\n",String_cString(archiveFileName));

        /* open archive */
        error = Archive_open(&archiveInfo,
                             archiveFileName,
                             jobOptions,
                             archiveGetCryptPasswordFunction,
                             archiveGetCryptPasswordUserData
                            );
        if (error != ERROR_NONE)
        {
            printError("Cannot open archive file '%s' (error: %s)!\n",
                       String_cString(archiveFileName),
                       Errors_getText(error)
                      );
            if (restoreInfo.error == ERROR_NONE) restoreInfo.error = error;
            continue;
        }
        String_set(restoreInfo.statusInfo.storageName,archiveFileName);
        updateStatusInfo(&restoreInfo);

        /* read files */
        while (   ((restoreInfo.requestedAbortFlag == NULL) || !(*restoreInfo.requestedAbortFlag))
                  && !Archive_eof(&archiveInfo)
                  && (restoreInfo.error == ERROR_NONE)
              )
        {
            /* pause */
            while ((restoreInfo.pauseFlag != NULL) && (*restoreInfo.pauseFlag))
            {
                Misc_udelay(500*1000);
            }

            /* get next file type */
            error = Archive_getNextFileType(&archiveInfo,
                                            &archiveFileInfo,
                                            &fileType
                                           );
            if (error != ERROR_NONE)
            {
                printError("Cannot not read next entry in archive '%s' (error: %s)!\n",
                           String_cString(archiveFileName),
                           Errors_getText(error)
                          );
                if (restoreInfo.error == ERROR_NONE) restoreInfo.error = error;
                break;
            }

            switch (fileType)
            {
            case FILE_TYPE_FILE:
            {
                String           fileName;
                FileInfo         fileInfo;
                uint64           fragmentOffset,fragmentSize;
                FileFragmentNode *fileFragmentNode;
                String           destinationFileName;
                String           directoryName;
//            FileInfo         localFileInfo;
                FileHandle       fileHandle;
                uint64           length;
                ulong            n;

                /* read file */
                fileName = String_new();
                error = Archive_readFileEntry(&archiveInfo,
                                              &archiveFileInfo,
                                              NULL,
                                              NULL,
                                              NULL,
                                              fileName,
                                              &fileInfo,
                                              &fragmentOffset,
                                              &fragmentSize
                                             );
                if (error != ERROR_NONE)
                {
                    printError("Cannot not read 'file' content of archive '%s' (error: %s)!\n",
                               String_cString(archiveFileName),
                               Errors_getText(error)
                              );
                    String_delete(fileName);
                    if (restoreInfo.error == ERROR_NONE) restoreInfo.error = error;
                    continue;
                }

                if (   (List_empty(includePatternList) || PatternList_match(includePatternList,fileName,PATTERN_MATCH_MODE_EXACT))
                        && !PatternList_match(excludePatternList,fileName,PATTERN_MATCH_MODE_EXACT)
                   )
                {
                    String_set(restoreInfo.statusInfo.fileName,fileName);
                    restoreInfo.statusInfo.fileDoneBytes = 0LL;
                    restoreInfo.statusInfo.fileTotalBytes = fragmentSize;
                    updateStatusInfo(&restoreInfo);

                    /* get destination filename */
                    destinationFileName = getDestinationFileName(String_new(),
                                          fileName,
                                          jobOptions->directory,
                                          jobOptions->directoryStripCount
                                                                );


                    /* check if file fragment exists */
                    fileFragmentNode = FileFragmentList_findFile(&fileFragmentList,fileName);
                    if (fileFragmentNode != NULL)
                    {
                        if (!jobOptions->overwriteFilesFlag && FileFragmentList_checkExists(fileFragmentNode,fragmentOffset,fragmentSize))
                        {
                            printInfo(1,"  Restore file '%s'...skipped (file part %ll..%ll exists)\n",
                                      String_cString(destinationFileName),
                                      fragmentOffset,
                                      (fragmentSize > 0)?fragmentOffset+fragmentSize-1:fragmentOffset
                                     );
                            String_delete(destinationFileName);
                            Archive_closeEntry(&archiveFileInfo);
                            String_delete(fileName);
                            continue;
                        }
                    }
                    else
                    {
                        if (!jobOptions->overwriteFilesFlag && File_exists(destinationFileName))
                        {
                            printInfo(1,"  Restore file '%s'...skipped (file exists)\n",String_cString(destinationFileName));
                            String_delete(destinationFileName);
                            Archive_closeEntry(&archiveFileInfo);
                            String_delete(fileName);
                            continue;
                        }
                        fileFragmentNode = FileFragmentList_addFile(&fileFragmentList,fileName,fileInfo.size);
                    }

                    printInfo(2,"  Restore file '%s'...",String_cString(destinationFileName));

                    /* create directory if not existing */
                    directoryName = File_getFilePathName(String_new(),destinationFileName);
                    if (!File_exists(directoryName))
                    {
                        /* create directory */
                        error = File_makeDirectory(directoryName,
                                                   FILE_DEFAULT_USER_ID,
                                                   FILE_DEFAULT_GROUP_ID,
                                                   fileInfo.permission
                                                  );
                        if (error != ERROR_NONE)
                        {
                            printInfo(2,"FAIL!\n");
                            printError("Cannot create directory '%s' (error: %s)\n",
                                       String_cString(directoryName),
                                       Errors_getText(error)
                                      );
                            String_delete(directoryName);
                            String_delete(destinationFileName);
                            Archive_closeEntry(&archiveFileInfo);
                            String_delete(fileName);
                            if (restoreInfo.error == ERROR_NONE) restoreInfo.error = error;
                            continue;
                        }

                        /* set owner ship */
                        error = File_setOwner(directoryName,
                                              (jobOptions->owner.userId  != FILE_DEFAULT_USER_ID )?jobOptions->owner.userId:fileInfo.userId,
                                              (jobOptions->owner.groupId != FILE_DEFAULT_GROUP_ID)?jobOptions->owner.groupId:fileInfo.groupId
                                             );
                        if (error != ERROR_NONE)
                        {
                            if (jobOptions->stopOnErrorFlag)
                            {
                                printInfo(2,"FAIL!\n");
                                printError("Cannot set owner ship of directory '%s' (error: %s)\n",
                                           String_cString(directoryName),
                                           Errors_getText(error)
                                          );
                                String_delete(directoryName);
                                String_delete(destinationFileName);
                                Archive_closeEntry(&archiveFileInfo);
                                String_delete(fileName);
                                if (restoreInfo.error == ERROR_NONE) restoreInfo.error = error;
                                continue;
                            }
                            else
                            {
                                printWarning("Cannot set owner ship of directory '%s' (error: %s)\n",
                                             String_cString(directoryName),
                                             Errors_getText(error)
                                            );
                            }
                        }
                    }
                    String_delete(directoryName);

                    /* write file data */
//if (fileFragmentNode == NULL) File_delete(destinationFileName,TRUE);
                    error = File_open(&fileHandle,destinationFileName,FILE_OPENMODE_WRITE);
                    if (error != ERROR_NONE)
                    {
                        printInfo(2,"FAIL!\n");
                        printError("Cannot create/write to file '%s' (error: %s)\n",
                                   String_cString(destinationFileName),
                                   Errors_getText(error)
                                  );
                        String_delete(destinationFileName);
                        Archive_closeEntry(&archiveFileInfo);
                        String_delete(fileName);
                        if (jobOptions->stopOnErrorFlag)
                        {
                            restoreInfo.error = error;
                        }
                        continue;
                    }
                    error = File_seek(&fileHandle,fragmentOffset);
                    if (error != ERROR_NONE)
                    {
                        printInfo(2,"FAIL!\n");
                        printError("Cannot write file '%s' (error: %s)\n",
                                   String_cString(destinationFileName),
                                   Errors_getText(error)
                                  );
                        File_close(&fileHandle);
                        String_delete(destinationFileName);
                        Archive_closeEntry(&archiveFileInfo);
                        String_delete(fileName);
                        if (jobOptions->stopOnErrorFlag)
                        {
                            restoreInfo.error = error;
                        }
                        continue;
                    }

                    length = 0;
                    while (   ((restoreInfo.requestedAbortFlag == NULL) || !(*restoreInfo.requestedAbortFlag))
                              && (length < fragmentSize)
                          )
                    {
                        /* pause */
                        while ((restoreInfo.pauseFlag != NULL) && (*restoreInfo.pauseFlag))
                        {
                            Misc_udelay(500*1000);
                        }

                        n = MIN(fragmentSize-length,BUFFER_SIZE);

                        error = Archive_readFileData(&archiveFileInfo,buffer,n);
                        if (error != ERROR_NONE)
                        {
                            printInfo(2,"FAIL!\n");
                            printError("Cannot not read content of archive '%s' (error: %s)!\n",
                                       String_cString(archiveFileName),
                                       Errors_getText(error)
                                      );
                            if (restoreInfo.error == ERROR_NONE) restoreInfo.error = error;
                            break;
                        }
                        error = File_write(&fileHandle,buffer,n);
                        if (error != ERROR_NONE)
                        {
                            printInfo(2,"FAIL!\n");
                            printError("Cannot write file '%s' (error: %s)\n",
                                       String_cString(destinationFileName),
                                       Errors_getText(error)
                                      );
                            if (jobOptions->stopOnErrorFlag)
                            {
                                restoreInfo.error = error;
                            }
                            break;
                        }
                        restoreInfo.statusInfo.fileDoneBytes += n;
                        updateStatusInfo(&restoreInfo);

                        length += n;
                    }
                    if (File_getSize(&fileHandle) > fileInfo.size)
                    {
                        File_truncate(&fileHandle,fileInfo.size);
                    }
                    File_close(&fileHandle);
                    if ((restoreInfo.requestedAbortFlag != NULL) && (*restoreInfo.requestedAbortFlag))
                    {
                        printInfo(2,"ABORTED\n");
                        String_delete(destinationFileName);
                        Archive_closeEntry(&archiveFileInfo);
                        String_delete(fileName);
                        continue;
                    }
#if 0
                    if (restoreInfo.error != ERROR_NONE)
                    {
                        String_delete(destinationFileName);
                        Archive_closeEntry(&archiveFileInfo);
                        String_delete(fileName);
                        continue;
                    }
#endif /* 0 */

                    /* set file time, file owner/group */
                    if (jobOptions->owner.userId  != FILE_DEFAULT_USER_ID ) fileInfo.userId  = jobOptions->owner.userId;
                    if (jobOptions->owner.groupId != FILE_DEFAULT_GROUP_ID) fileInfo.groupId = jobOptions->owner.groupId;
                    error = File_setFileInfo(destinationFileName,&fileInfo);
                    if (error != ERROR_NONE)
                    {
                        if (jobOptions->stopOnErrorFlag)
                        {
                            printInfo(2,"FAIL!\n");
                            printError("Cannot set file info of '%s' (error: %s)\n",
                                       String_cString(destinationFileName),
                                       Errors_getText(error)
                                      );
                            String_delete(destinationFileName);
                            Archive_closeEntry(&archiveFileInfo);
                            String_delete(fileName);
                            restoreInfo.error = error;
                            continue;
                        }
                        else
                        {
                            printWarning("Cannot set file info of '%s' (error: %s)\n",
                                         String_cString(destinationFileName),
                                         Errors_getText(error)
                                        );
                        }
                    }

                    /* add fragment to file fragment list */
                    FileFragmentList_add(fileFragmentNode,fragmentOffset,fragmentSize);
//FileFragmentList_print(fileFragmentNode,String_cString(fileName));

                    /* discard fragment list if file is complete */
                    if (FileFragmentList_checkComplete(fileFragmentNode))
                    {
                        FileFragmentList_removeFile(&fileFragmentList,fileFragmentNode);
                    }

                    /* free resources */
                    String_delete(destinationFileName);

                    printInfo(2,"ok\n");
                }
                else
                {
                    /* skip */
                    printInfo(3,"  Restore '%s'...skipped\n",String_cString(fileName));
                }

                /* close archive file, free resources */
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
            }
            break;
            case FILE_TYPE_DIRECTORY:
            {
                String   directoryName;
                FileInfo fileInfo;
                String   destinationFileName;
//            FileInfo localFileInfo;

                /* read directory */
                directoryName = String_new();
                error = Archive_readDirectoryEntry(&archiveInfo,
                                                   &archiveFileInfo,
                                                   NULL,
                                                   NULL,
                                                   directoryName,
                                                   &fileInfo
                                                  );
                if (error != ERROR_NONE)
                {
                    printError("Cannot not read 'directory' content of archive '%s' (error: %s)!\n",
                               String_cString(archiveFileName),
                               Errors_getText(error)
                              );
                    String_delete(directoryName);
                    if (restoreInfo.error == ERROR_NONE) restoreInfo.error = error;
                    break;
                }

                if (   (List_empty(includePatternList) || PatternList_match(includePatternList,directoryName,PATTERN_MATCH_MODE_EXACT))
                        && !PatternList_match(excludePatternList,directoryName,PATTERN_MATCH_MODE_EXACT)
                   )
                {
                    String_set(restoreInfo.statusInfo.fileName,directoryName);
                    restoreInfo.statusInfo.fileDoneBytes = 0LL;
                    restoreInfo.statusInfo.fileTotalBytes = 00L;
                    updateStatusInfo(&restoreInfo);

                    /* get destination filename */
                    destinationFileName = getDestinationFileName(String_new(),
                                          directoryName,
                                          jobOptions->directory,
                                          jobOptions->directoryStripCount
                                                                );


                    /* check if directory already exists */
                    if (!jobOptions->overwriteFilesFlag && File_exists(destinationFileName))
                    {
                        printInfo(1,
                                  "  Restore directory '%s'...skipped (file exists)\n",
                                  String_cString(destinationFileName)
                                 );
                        String_delete(destinationFileName);
                        Archive_closeEntry(&archiveFileInfo);
                        String_delete(directoryName);
                        continue;
                    }

                    printInfo(2,"  Restore directory '%s'...",String_cString(destinationFileName));

                    /* create directory */
                    error = File_makeDirectory(destinationFileName,
                                               FILE_DEFAULT_USER_ID,
                                               FILE_DEFAULT_GROUP_ID,
                                               fileInfo.permission
                                              );
                    if (error != ERROR_NONE)
                    {
                        printInfo(2,"FAIL!\n");
                        printError("Cannot create directory '%s' (error: %s)\n",
                                   String_cString(destinationFileName),
                                   Errors_getText(error)
                                  );
                        String_delete(destinationFileName);
                        Archive_closeEntry(&archiveFileInfo);
                        String_delete(directoryName);
                        if (jobOptions->stopOnErrorFlag)
                        {
                            restoreInfo.error = error;
                        }
                        continue;
                    }

                    /* set file time, file owner/group */
                    if (jobOptions->owner.userId  != FILE_DEFAULT_USER_ID ) fileInfo.userId  = jobOptions->owner.userId;
                    if (jobOptions->owner.groupId != FILE_DEFAULT_GROUP_ID) fileInfo.groupId = jobOptions->owner.groupId;
                    error = File_setFileInfo(destinationFileName,&fileInfo);
                    if (error != ERROR_NONE)
                    {
                        if (jobOptions->stopOnErrorFlag)
                        {
                            printInfo(2,"FAIL!\n");
                            printError("Cannot set directory info of '%s' (error: %s)\n",
                                       String_cString(destinationFileName),
                                       Errors_getText(error)
                                      );
                            String_delete(destinationFileName);
                            Archive_closeEntry(&archiveFileInfo);
                            String_delete(directoryName);
                            if (jobOptions->stopOnErrorFlag)
                            {
                                restoreInfo.error = error;
                            }
                            continue;
                        }
                        else
                        {
                            printWarning("Cannot set directory info of '%s' (error: %s)\n",
                                         String_cString(destinationFileName),
                                         Errors_getText(error)
                                        );
                        }
                    }

                    /* free resources */
                    String_delete(destinationFileName);

                    printInfo(2,"ok\n");
                }
                else
                {
                    /* skip */
                    printInfo(3,"  Restore '%s'...skipped\n",String_cString(directoryName));
                }

                /* close archive file */
                Archive_closeEntry(&archiveFileInfo);
                String_delete(directoryName);
            }
            break;
            case FILE_TYPE_LINK:
            {
                String   linkName;
                String   fileName;
                FileInfo fileInfo;
                String   destinationFileName;
//            FileInfo localFileInfo;

                /* read link */
                linkName = String_new();
                fileName = String_new();
                error = Archive_readLinkEntry(&archiveInfo,
                                              &archiveFileInfo,
                                              NULL,
                                              NULL,
                                              linkName,
                                              fileName,
                                              &fileInfo
                                             );
                if (error != ERROR_NONE)
                {
                    printError("Cannot not read 'link' content of archive '%s' (error: %s)!\n",
                               String_cString(archiveFileName),
                               Errors_getText(error)
                              );
                    String_delete(fileName);
                    String_delete(linkName);
                    if (restoreInfo.error == ERROR_NONE) restoreInfo.error = error;
                    break;
                }

                if (   (List_empty(includePatternList) || PatternList_match(includePatternList,linkName,PATTERN_MATCH_MODE_EXACT))
                        && !PatternList_match(excludePatternList,linkName,PATTERN_MATCH_MODE_EXACT)
                   )
                {
                    String_set(restoreInfo.statusInfo.fileName,linkName);
                    restoreInfo.statusInfo.fileDoneBytes = 0LL;
                    restoreInfo.statusInfo.fileTotalBytes = 00L;
                    updateStatusInfo(&restoreInfo);

                    /* get destination filename */
                    destinationFileName = getDestinationFileName(String_new(),
                                          linkName,
                                          jobOptions->directory,
                                          jobOptions->directoryStripCount
                                                                );


                    /* create link */
                    if (!jobOptions->overwriteFilesFlag && File_exists(destinationFileName))
                    {
                        printInfo(1,
                                  "  Restore link '%s'...skipped (file exists)\n",
                                  String_cString(destinationFileName)
                                 );
                        String_delete(destinationFileName);
                        Archive_closeEntry(&archiveFileInfo);
                        String_delete(fileName);
                        String_delete(linkName);
                        if (jobOptions->stopOnErrorFlag)
                        {
                            restoreInfo.error = ERROR_FILE_EXITS;
                        }
                        continue;
                    }

                    printInfo(2,"  Restore link '%s'...",String_cString(destinationFileName));

                    error = File_makeLink(destinationFileName,fileName);
                    if (error != ERROR_NONE)
                    {
                        printInfo(2,"FAIL!\n");
                        printError("Cannot create link '%s' -> '%s' (error: %s)\n",
                                   String_cString(destinationFileName),
                                   String_cString(fileName),
                                   Errors_getText(error)
                                  );
                        String_delete(destinationFileName);
                        Archive_closeEntry(&archiveFileInfo);
                        String_delete(fileName);
                        String_delete(linkName);
                        if (jobOptions->stopOnErrorFlag)
                        {
                            restoreInfo.error = error;
                        }
                        continue;
                    }

                    /* set file time, file owner/group */
                    if (jobOptions->owner.userId  != FILE_DEFAULT_USER_ID ) fileInfo.userId  = jobOptions->owner.userId;
                    if (jobOptions->owner.groupId != FILE_DEFAULT_GROUP_ID) fileInfo.groupId = jobOptions->owner.groupId;
                    error = File_setFileInfo(destinationFileName,&fileInfo);
                    if (error != ERROR_NONE)
                    {
                        if (jobOptions->stopOnErrorFlag)
                        {
                            printInfo(2,"FAIL!\n");
                            printError("Cannot set file info of '%s' (error: %s)\n",
                                       String_cString(destinationFileName),
                                       Errors_getText(error)
                                      );
                            String_delete(destinationFileName);
                            Archive_closeEntry(&archiveFileInfo);
                            String_delete(fileName);
                            String_delete(linkName);
                            if (jobOptions->stopOnErrorFlag)
                            {
                                restoreInfo.error = error;
                            }
                            continue;
                        }
                        else
                        {
                            printWarning("Cannot set file info of '%s' (error: %s)\n",
                                         String_cString(destinationFileName),
                                         Errors_getText(error)
                                        );
                        }
                    }

                    /* free resources */
                    String_delete(destinationFileName);

                    printInfo(2,"ok\n");
                }
                else
                {
                    /* skip */
                    printInfo(3,"  Restore '%s'...skipped\n",String_cString(linkName));
                }

                /* close archive file */
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
                String_delete(linkName);
            }
            break;
            case FILE_TYPE_SPECIAL:
            {
                String   fileName;
                FileInfo fileInfo;
                String   destinationFileName;
//            FileInfo localFileInfo;

                /* read special device */
                fileName = String_new();
                error = Archive_readSpecialEntry(&archiveInfo,
                                                 &archiveFileInfo,
                                                 NULL,
                                                 NULL,
                                                 fileName,
                                                 &fileInfo
                                                );
                if (error != ERROR_NONE)
                {
                    printError("Cannot not read 'special' content of archive '%s' (error: %s)!\n",
                               String_cString(archiveFileName),
                               Errors_getText(error)
                              );
                    String_delete(fileName);
                    if (restoreInfo.error == ERROR_NONE) restoreInfo.error = error;
                    break;
                }

                if (   (List_empty(includePatternList) || PatternList_match(includePatternList,fileName,PATTERN_MATCH_MODE_EXACT))
                        && !PatternList_match(excludePatternList,fileName,PATTERN_MATCH_MODE_EXACT)
                   )
                {
                    String_set(restoreInfo.statusInfo.fileName,fileName);
                    restoreInfo.statusInfo.fileDoneBytes = 0LL;
                    restoreInfo.statusInfo.fileTotalBytes = 00L;
                    updateStatusInfo(&restoreInfo);

                    /* get destination filename */
                    destinationFileName = getDestinationFileName(String_new(),
                                          fileName,
                                          jobOptions->directory,
                                          jobOptions->directoryStripCount
                                                                );


                    /* create special device */
                    if (!jobOptions->overwriteFilesFlag && File_exists(destinationFileName))
                    {
                        printInfo(1,
                                  "  Restore special device '%s'...skipped (file exists)\n",
                                  String_cString(destinationFileName)
                                 );
                        String_delete(destinationFileName);
                        Archive_closeEntry(&archiveFileInfo);
                        String_delete(fileName);
                        if (jobOptions->stopOnErrorFlag)
                        {
                            restoreInfo.error = ERROR_FILE_EXITS;
                        }
                        continue;
                    }

                    printInfo(2,"  Restore special device '%s'...",String_cString(destinationFileName));

                    error = File_makeSpecial(destinationFileName,
                                             fileInfo.specialType,
                                             fileInfo.major,
                                             fileInfo.minor
                                            );
                    if (error != ERROR_NONE)
                    {
                        printInfo(2,"FAIL!\n");
                        printError("Cannot create special device '%s' (error: %s)\n",
                                   String_cString(fileName),
                                   Errors_getText(error)
                                  );
                        String_delete(destinationFileName);
                        Archive_closeEntry(&archiveFileInfo);
                        String_delete(fileName);
                        if (jobOptions->stopOnErrorFlag)
                        {
                            restoreInfo.error = error;
                        }
                        continue;
                    }

                    /* set file time, file owner/group */
                    if (jobOptions->owner.userId  != FILE_DEFAULT_USER_ID ) fileInfo.userId  = jobOptions->owner.userId;
                    if (jobOptions->owner.groupId != FILE_DEFAULT_GROUP_ID) fileInfo.groupId = jobOptions->owner.groupId;
                    error = File_setFileInfo(destinationFileName,&fileInfo);
                    if (error != ERROR_NONE)
                    {
                        if (jobOptions->stopOnErrorFlag)
                        {
                            printInfo(2,"FAIL!\n");
                            printError("Cannot set file info of '%s' (error: %s)\n",
                                       String_cString(destinationFileName),
                                       Errors_getText(error)
                                      );
                            String_delete(destinationFileName);
                            Archive_closeEntry(&archiveFileInfo);
                            String_delete(fileName);
                            if (jobOptions->stopOnErrorFlag)
                            {
                                restoreInfo.error = error;
                            }
                            continue;
                        }
                        else
                        {
                            printWarning("Cannot set file info of '%s' (error: %s)\n",
                                         String_cString(destinationFileName),
                                         Errors_getText(error)
                                        );
                        }
                    }

                    /* free resources */
                    String_delete(destinationFileName);

                    printInfo(2,"ok\n");
                }
                else
                {
                    /* skip */
                    printInfo(3,"  Restore '%s'...skipped\n",String_cString(fileName));
                }

                /* close archive file */
                Archive_closeEntry(&archiveFileInfo);
                String_delete(fileName);
            }
            break;
            default:
#ifndef NDEBUG
                HALT_INTERNAL_ERROR_UNHANDLED_SWITCH_CASE();
#endif /* NDEBUG */
                break; /* not reached */
            }
        }

        /* close archive */
        Archive_close(&archiveInfo);
    }

    /* check fragment lists */
    if ((restoreInfo.requestedAbortFlag == NULL) || !(*restoreInfo.requestedAbortFlag))
    {
        for (fileFragmentNode = fileFragmentList.head; fileFragmentNode != NULL; fileFragmentNode = fileFragmentNode->next)
        {
            if (!FileFragmentList_checkComplete(fileFragmentNode))
            {
                printInfo(0,"Warning: incomplete file '%s'\n",String_cString(fileFragmentNode->fileName));
                if (restoreInfo.error == ERROR_NONE) restoreInfo.error = ERROR_FILE_INCOMPLETE;
            }
        }
    }

    /* free resources */
    String_delete(archiveFileName);
    FileFragmentList_done(&fileFragmentList);
    free(buffer);
    String_delete(restoreInfo.statusInfo.fileName);
    String_delete(restoreInfo.statusInfo.storageName);

    if ((restoreInfo.requestedAbortFlag == NULL) || !(*restoreInfo.requestedAbortFlag))
    {
        return restoreInfo.error;
    }
    else
    {
        return ERROR_ABORTED;
    }
}
Esempio n. 13
0
/************************************************************************
void  Open_desk(pList pTable)
开桌
************************************************************************/
void  Open_desk(pList pTable)
{	
	pList pt;
	int n;
	struct Table_t *ps;
	char str[8] = {0};
    FILE *fp;
	fp = File_open("./Date/Table.txt");

	while (1)
	{
		system("cls");
    n = Table_search(pTable,0,3);// 打印空闲和预定的台桌
	if (n == 0)
	{
		printf(",暂无空闲桌子,按任意键返回...");
		getch();
		return;
	}
    printf("请输入要开桌的台桌号:");
	glb_putString(str,'0','9',3,2);// str存放要开桌的台桌号
    pt = pTable->pNext;
	while(pt != NULL)
	 { 	  
		ps = (struct Table_t *)(pt->pData);
		if (strcmp(ps->acNo,str) == 0)
		{	
			if ((ps->state == 0)||(ps->state == 3))
     		break;
		}
		pt = pt->pNext;
	 }
	
	 if (pt == NULL)
	  {
		  memset(str,0,sizeof(char));
		  printf("\n台桌号有误或不可用,请重新输入,按任意键继续...");
		  getch();
	  }
	  else
		  break;
	}
	  
	     if (ps->state == 3)// 若为预定状态,直接跳转为点菜界面
	     {
			 ps->state = 2;//占用,转到点菜界面
			 File_write(fp,pTable,sizeof(struct Table_t),0);// 更新台桌文件
			 printf("\n开桌成功,按任意键继续...");
			 getch();
			 Order_dishes(pTable);// 跳转到点菜界面
			 return;
	     }
	     printf("\n是否马上开始点菜(y)确定、(n)返回:");
	     if(glb_putString(NULL,0,0,1,1) != 'y')
		 {     
			 ps->state = 3;//预定状态
			 File_write(fp,pTable,sizeof(struct Table_t),0);
			 printf("\n预定成功,按任意键继续...");
			 getch();
		 }  
		 else 
		 {
			 ps->state = 2;//占用,转到点菜界面
			 File_write(fp,pTable,sizeof(struct Table_t),0);
			 printf("\n开桌成功,按任意键继续...");
			 getch();
			 Order_dishes(pTable);
			 return;
		 }			 
	   
}
Esempio n. 14
0
/************************************************************************
void Settle_Accounts(pList pTable)
结账功能
************************************************************************/
void Settle_Accounts(pList pTable)
{
	FILE *fp;
	int money;
	pList pt,pTitle,pOrder,ps;
	struct Order_t * data;
	char txt[] = ".txt";
    char str[6] = {0};
	char str1[6] = {0};
    char tabpath[30] ="./Date/Cache/";// 未结详单路径
	fp = File_open("./Date/Cache/Order.txt");// 打开未买订单文件
	pOrder = File_read(fp,sizeof(struct Order_t));
	while(1)
	{	
	system("cls");
	pt = pOrder->pNext;
	if (pt == NULL)//判断当前是否有未买订单
	{
		printf("\n目前没有未买订单,按任意键返回...");
		return;
	}
	printf("\t\t%12s%12s%11s%11s\n","订单号","台桌号","金额","状态");
	while (pt)
	{
		data = (struct Order_t *)(pt->pData);
		printf("\t\t%12s%12s%11d%11s\n",data->ordernum,data->tablenum,data->money,"未支付");// 打印所有未结订单信息
		pt = pt->pNext;
	}  
	printf("\n请输入要结账的台桌号(3位):");
	glb_putString(str,'0','9',3,2);	
	ps = pOrder;
	pt = pOrder->pNext;
	while (pt)
	{
		data = (struct Order_t *)(pt->pData);
		if (strcmp(data->tablenum,str) == 0)// 找出要结账的订单结点
			break;
		ps = pt;
		pt = pt->pNext;
	}
	if (pt == NULL)// 未找到订单,作提示
	{
		printf("\n\n台桌号无订单或有误,按<Esc>键返回、其他键重新输入:");
		if (getch() == 27)
		return;	
		memset(str,0,sizeof(char));
	}
	else
	   break;
	}	
	strcat(tabpath,str);// 获取详单的路径
	strcat(tabpath,txt);
	fp = File_open(tabpath);// 打开将结账的详单文件
	pTitle = File_read(fp,sizeof(struct Title_t));// 读出未买详单链表
	Title_print(pTitle,data);// 打印未买详单信息
	printf("\n订单号为%s是否结账,(y)确定、(n)返回:",data->ordernum);
	if (glb_putString(NULL,0,0,1,1) == 'y')
	{
		printf("\n实收金额:");
		money = glb_putString(str1,'0','9',5,2);// 输入金额
		if (money < data->money)// 实收金额不可小于订单金额
		{
			printf("\n实收金额小于订单金额,无法结账,按任意键返回...");
			getch();
			return;
		}
		printf("\n回找金额:%d",money - (data->money));
		fp = File_open("./Date/Title/Title.txt");
		File_write(fp,pTitle,sizeof(struct Title_t),2);//写入已结详单链表
		
		fp = File_open("./Date/Order/Order.txt");
		fseek(fp,0,2);
		strcpy(data->acname,acName);//写入结账服务员登录名到订单结构体中
		fwrite(data,sizeof(struct Order_t),1,fp);//写入已结订单的结点到另一个文件
		fflush(fp);
		
		ps->pNext = pt->pNext;// 删除结点
		free(pt);//释放已结订单的结点
		fp = fopen("./Date/Cache/Order.txt","w+");
		File_write(fp,pOrder,sizeof(struct Order_t),0);//刷新未结订单的文件
		
		pt = pTable->pNext;
		while(pt)
		{
			if(strcmp(((struct Table_t *)(pt->pData))->acNo,str) == 0)// 找到已结账台桌结点
			{
				((struct Table_t *)(pt->pData))->state = 0;// 桌子状态改为空闲
				fp = File_open("./Date/Table.txt");
				File_write(fp,pTable,sizeof(struct Table_t),0);// 更新台桌信息文件
				break;
			}
			pt = pt->pNext;
		}
		printf("\n结账成功");			   
	}
	else
		printf("\n结账失败");
	printf(",按任意键返回...");
	getch();
	List_free(pTitle);
    List_free(pOrder);
}
Esempio n. 15
0
/************************************************************************
void Order_dishes(pList pTable)
点菜
************************************************************************/
void Order_dishes(pList pTable)
{
	FILE *fd;
	int n,count1;
	pList pMenu,pOrder,pTitle,pt;
    struct Order_t *pNew,*pdata;
	char txt[] = ".txt";
    char str[8] = {0};
    char tabpath[30] ="./Date/Cache/";// 详单的相对路径

	fd = File_open("./Date/Menu.txt");
	pMenu = File_read(fd,sizeof(struct Menu_t));	
	fd = File_open("./Date/Cache/Order.txt");
    pOrder = File_read(fd,sizeof(struct Order_t));

	while (1)
	{
		system("cls");
	n = Table_search(pTable,2,4);// 判断是否有可用桌子
	if (n == 0)
	{		
		printf(",暂无可用桌子,按任意键继续...");
		getch();
		return;
	}
	printf("请输入要点菜的台桌号:");
	glb_putString(str,'0','9',3,2);
    strcat(tabpath,str);
	strcat(tabpath,txt);// 获取详单的路径
    pt = pOrder->pNext;
	while(pt)
	{
         pdata = (struct Order_t *)(pt->pData);
		if (strcmp(pdata->tablenum,str) == 0)// 判断当前未买订单中是否存在此台桌的订单
		{
			pNew = pdata;
			fd = File_open(tabpath);
	        pTitle = File_read(fd,sizeof(struct Title_t));
			break;
		}
		pt = pt->pNext;
	}

    if (pt == NULL)// 未买订单不存在输入台桌的订单
    {	
        pt = pTable ->pNext;
		while(pt)
		{
			if (strcmp(((struct Table_t *)(pt->pData))->acNo,str) == 0)
			{
				if (((struct Table_t *)(pt->pData))->state == 2)
				{
					pNew = Order_init(str);// 初始化一个订单结点
					List_add(pOrder,pNew);
					File_write(fd,pOrder,sizeof(struct Order_t),0);// 写入未买订单文件
					fd = fopen(tabpath,"w+");// 已w+方式打开详单文件,便于开始点菜
		            pTitle = File_read(fd,sizeof(struct Title_t));
					break;
				}
				else if (((struct Table_t *)(pt->pData))->state == 0)
				{
					printf("\n此桌号为空闲需先开桌才能点菜,是否开桌,(y)开桌、(n)返回:");
					if (glb_putString(NULL,0,0,1,1) == 'y')
						Open_desk(pTable);// 跳转到开桌界面
					return;
				}		
		
			}
		               pt = pt->pNext;
		
		}
		if (pt == NULL)
		{
			memset(str,0,sizeof(char));
			strcpy(tabpath,"./Date/Cache/");
			printf("\n输入的桌号有误或者处于维修状态,请重新输入,按任意键继续...");
			getch();
		}
		else
			break;		
    }
	else
		break;
}
     Order_print(pMenu,pTitle,pNew);
	 count1 = List_count(pMenu);// 获取菜谱的个数
	 while (1)	
{
	JR_EmptyRaw(count1+3,count1+12);// 清空菜谱下面的几行信息
	JR_SetCursor(count1+3,0);
	printf("\t\t(1):点菜\n");
	printf("\t\t(2):退菜\n");
	printf("\t\t(3):返回\n");
	printf("\t\t请输入要操作的功能:");
    n = glb_putString(NULL,'1','3',1,2);
	switch(n)
	{ 
	 case 1:
		 {
			 Order_add(count1,pNew,pTitle,pOrder,pMenu,tabpath); // 加菜功能函数
		     break;
		 }	
	 case 2:
		 {
			 Order_del(count1,pNew,pTitle,pMenu,pOrder,tabpath);// 减菜功能函数
			 break;
		 }
	 case 3:
		     break;
		
	}	
	        
	 if (n == 3)
	 {
		 break;
	 }
	 	   		   
	}	 
	 List_free(pOrder);
	 List_free(pMenu);
	 List_free(pTitle);
}
int main(void) {
        int fd;
        InputStream_T in = NULL;

        Bootstrap(); // Need to initialize library

        printf("============> Start InputStream Tests\n\n");

        printf("=> Test0: create/destroy the stream\n");
        {
                in = InputStream_new(File_open(DATA, "r"));
                assert(!InputStream_isClosed(in));
                File_close(InputStream_getDescriptor(in));
                InputStream_free(&in);
                assert(in == NULL);
        }
        printf("=> Test0: OK\n\n");

        printf("=> Test1: get/set timeout\n");
        {
                assert((fd = File_open(DATA, "r")) >= 0);
                in = InputStream_new(fd);
                printf("\tCurrent timeout: %lldms\n", (long long)InputStream_getTimeout(in));
                InputStream_setTimeout(in, TIMEOUT);
                assert(InputStream_getTimeout(in) == TIMEOUT);
                printf("\tTimeout set to:  %dms\n", TIMEOUT);
                File_close(fd);
                InputStream_free(&in);
        }
        printf("=> Test1: OK\n\n");

        printf("=> Test2: read file by characters\n");
        {
                int byte;
                int byteno = 0;
                char content[][1] = {"l", "i", "n", "e", "1", "\n",
                                     "l", "i", "n", "e", "2", "\n",
                                     "l", "i", "n", "e", "3", "\n"};
                assert((fd = File_open(DATA, "r")) >= 0);
                in = InputStream_new(fd);
                while ((byte = InputStream_read(in)) > 0) {
                        assert(byte == *content[byteno++]);
                }
                File_close(fd);
                InputStream_free(&in);
        }
        printf("=> Test2: OK\n\n");

        printf("=> Test3: read file by lines\n");
        {
                int lineno = 0;
                char line[STRLEN];
                char content[][STRLEN] = {"line1\n", "line2\n", "line3\n"};
                assert((fd = File_open(DATA, "r")) >= 0);
                in = InputStream_new(fd);
                while (InputStream_readLine(in, line, sizeof(line))) {
                        assert(Str_isEqual(content[lineno++], line));
                }
                File_close(fd);
                InputStream_free(&in);
        }
        printf("=> Test3: OK\n\n");

        printf("=> Test4: read file by bytes\n");
        {
                char array[STRLEN];
                char content[] = "line1\nline2\nline3\n";
                memset(array, 0, STRLEN);
                assert((fd = File_open(DATA, "r")) >= 0);
                in = InputStream_new(fd);
                while (InputStream_readBytes(in, array, sizeof(array)-1)) {
                        assert(Str_isEqual(content, array));
                }
                File_close(fd);
                InputStream_free(&in);
        }
        printf("=> Test4: OK\n\n");

        printf("=> Test5: read a large file\n");
        {
                if ((fd = File_open("/usr/share/dict/words", "r")) >= 0) {
                        int n = 0;
                        char array[2][STRLEN + 1];
                        in = InputStream_new(fd);
                        for (int i = 0; ((n = InputStream_readBytes(in, array[i], STRLEN)) > 0); i = i ? 0 : 1)
                                assert(strncmp(array[0], array[1], STRLEN/2) != 0); // ensure that InputStream buffer is filled anew
                        File_rewind(fd);
                        // Test read data larger than InputStream's internal buffer
                        int filesize = (int)File_size("/usr/share/dict/words");
                        char *bigarray = CALLOC(1, filesize + 1);
                        n = InputStream_readBytes(in, bigarray, filesize);
                        assert(n == filesize);
                        File_close(fd);
                        InputStream_free(&in);
                        FREE(bigarray);
                } else 
                        ERROR("\t/usr/share/dict/words not available -- skipping test\n");
        }
        printf("=> Test5: OK\n\n");

        printf("=> Test6: wrong descriptor - expecting read fail\n");
        {
                in = InputStream_new(999);
                TRY
                        assert(InputStream_read(in) != -1);
                        printf("Test6: Failed");
                        exit(1); // Should not come here
                CATCH(AssertException)
                        // Passed
                END_TRY;
                InputStream_free(&in);
        }
        printf("=> Test6: OK\n\n");


        printf("============> InputStream Tests: OK\n\n");

        return 0;
}
Esempio n. 17
0
void Profit_statist()
{
	pList pOrder,pStaff;
	pList p1,p2;
	struct Order_t *Odate;
	struct staff_t *Sdate;
	FILE *fp;
	long money,income;// 总金额
	long *stardate,*enddate;
    long ordernum;// 订单号
	money = income = 0;
	stardate = (long *)malloc(sizeof(long));// 开始日期
    enddate = (long *)malloc(sizeof(long));// 结束日期
	fp = File_open("./Date/Order/Order.txt");
    pOrder= File_read(fp,sizeof(struct Order_t));
	fp = File_open("./Date/Staff.txt");
    pStaff= File_read(fp,sizeof(struct staff_t));

	system("cls");
	printf("\n\t\t\t(1):当天查询\n");
	printf("\n\t\t\t(2):历史查询\n");
	printf("\n\t\t\t请输入要选择的功能:");
	if (glb_putString(NULL,'1','2',1,2) == 1)
	{
		system("cls");
		*stardate = Ordernum_get(NULL,NULL);// 获取当前系统日期
		*enddate = *stardate;
	}
	else
	{
		Orderdate_input(stardate,enddate);// 输入开始和结束日期
		printf("\n");
	}
    printf("\t\t\t%-15s%s\n","服务员","营收");
	p1 = pStaff->pNext;
	while (p1)
	{
		Sdate = (struct staff_t *)(p1->pData);
		p2 = pOrder->pNext;
		while(p2)
		{
			Odate = (struct Order_t *)(p2->pData);
			sscanf(Odate->ordernum,"%8ld",&ordernum);// 获取订单号上的日期
			if ((strcmp(Sdate->acName,Odate->acname) == 0)&&(ordernum <= *enddate) && (ordernum >= *stardate))
			{
				money = money + (Odate->money);
			}
			p2 = p2->pNext;
		}
		if ((Sdate->role == 0))// 判断是否为服务员
		{
			printf("\t\t\t%-15s%s%ld\n",Sdate->acName,"¥",money);
			income = income + money;// 累加每个服务员的营业额
			money = 0;
		}	
		p1 = p1->pNext;
	}

	    printf("\n\t\t\t总营业额:¥%ld\n",income);
		printf("\n\t\t\t请按任意键返回...");
		getch();
}