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

📄 main.c

📁 ATMEL单片机可用的文件系统源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/***********************************************************************/
/*                                                                     */
/*   Module:  main.c                                                   */
/*   Release: 2004.5                                                   */
/*   Version: 2004.2                                                   */
/*   Purpose: Binary Search Application                                */
/*                                                                     */
/***********************************************************************/
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <targetos.h>
#include <kernel.h>
#include <sys.h>
#include <pccard.h>
#include "../../posix.h"

/***********************************************************************/
/* Configuration                                                       */
/***********************************************************************/
#define HI_KEY_NUM      15000 /* RAND_MAX */
#define CREATE_DATA     TRUE
#define ID_REG          0
#define CWD_WD1         1
#define CWD_WD2         2
#define RFS_NAME        "rfs"

/***********************************************************************/
/* Macro Definitions                                                   */
/***********************************************************************/
#define NUM_FSYS        (sizeof(Volume) / sizeof(char *))

/***********************************************************************/
/* Type Definitions                                                    */
/***********************************************************************/
typedef struct
{
  ui32 key;
  ui32 data;
} Record;

/***********************************************************************/
/* Global Data Declarations                                            */
/***********************************************************************/
static char *Volume[] =
{
#if INC_NAND_FS
  "nand",
#endif
#if INC_NOR_FS
  "nor",
#endif
#if NUM_RFS_VOLS
  RFS_NAME,
#endif
#if PCCARD_SUPPORT
  "ata",
#endif
#if INC_FAT_FIXED
  "fat",
#endif
#if NUM_NAND_FTLS
  "ftld",
#endif
};

/***********************************************************************/
/* Local Function Definitions                                          */
/***********************************************************************/

/***********************************************************************/
/* bsearch_app: Perform binary search on file system data              */
/*                                                                     */
/*      Inputs: vol_name = name of file system volume                  */
/*              creat_data = TRUE to format volume and write data      */
/*                                                                     */
/***********************************************************************/
static void bsearch_app(char *vol_name, int creat_data)
{
  off_t low, high, pos, sc;
  int rc, key, fid, i;
  Record record;
  clock_t delta, sample;
  char path[PATH_MAX];
  union vstat stats;

  /*-------------------------------------------------------------------*/
  /* If requested, format volume and write data.                       */
  /*-------------------------------------------------------------------*/
  if (creat_data)
  {
    printf("Formating \"%s\" volume\n", vol_name);
    taskSleep(1);
    sample = clock();
    if (format(vol_name))
      SysFatalError(errno);
    delta = clock() - sample;
    printf("Elapsed time for format is %u seconds\n",
           (delta + (CLOCKS_PER_SEC / 2)) / CLOCKS_PER_SEC);
  }

  /*-------------------------------------------------------------------*/
  /* Mount the volume and change to its root directory.                */
  /*-------------------------------------------------------------------*/
  printf("Mounting \"%s\" volume\n", vol_name);
  taskSleep(1);
  sample = clock();
  if (mount(vol_name))
    SysFatalError(errno);
  delta = clock() - sample;
  printf("Elapsed time for mount is %u seconds\n",
         (delta + (CLOCKS_PER_SEC / 2)) / CLOCKS_PER_SEC);
  snprintf(path, PATH_MAX, "/%s", vol_name);
  if (chdir(path))
    SysFatalError(errno);

  /*-------------------------------------------------------------------*/
  /* Display volume geometry and total size.                           */
  /*-------------------------------------------------------------------*/
  if (vstat(vol_name, &stats))
    SysFatalError(errno);
  else
  {
    long size, ones, tenths;

    printf("Volume has %ld sectors of %ld bytes each: ",
           stats.ffs.num_sects, stats.ffs.sect_size);
    size = stats.ffs.num_sects * stats.ffs.sect_size;
    if (size / (1024 * 1024))
    {
      size += (1024 * 1024) / 20;
      ones = size / (1024 * 1024);
      tenths = (10 * (size % (1024 * 1024))) / (1024 * 1024);
      printf("%d.%dMB\n", ones, tenths);
    }
    else if (size / 1024)
    {
      size += 1024 / 20;
      ones = size / 1024;
      tenths = (10 * (size % 1024)) / 1024;
      printf("%d.%dKB\n", ones, tenths);
    }
    else
      printf("%d bytes\n", size);
  }

  /*-------------------------------------------------------------------*/
  /* Open the file for reads and writes, creating it if necessary.     */
  /*-------------------------------------------------------------------*/
  if (creat_data)
    fid = open("records", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IXUSR);
  else
    fid = open("records", O_RDONLY);
  if (fid == -1)
    SysFatalError(errno);

  /*-------------------------------------------------------------------*/
  /* If needed, create multiple records, with increasing key values.   */
  /*-------------------------------------------------------------------*/
  if (creat_data)
  {
    printf("Creating file with monotonically ordered keys\n");
    taskSleep(1);
    sample = clock();
    record.data = 0x12345678;
    for (i = 0; i <= HI_KEY_NUM; ++i)
    {
      record.key = i;
      rc = write(fid, &record, sizeof(Record));
      if (rc != sizeof(Record))
        SysFatalError(errno);
    }
    delta = clock() - sample;
    printf("Elapsed time to create data is %u seconds\n",
           (delta + (CLOCKS_PER_SEC / 2)) / CLOCKS_PER_SEC);
  }

  /*-------------------------------------------------------------------*/
  /* Flush all record data to the flash volume.                        */
  /*-------------------------------------------------------------------*/
  rc = sync(fid);
  if (rc)
    SysFatalError(errno);

  /*-------------------------------------------------------------------*/
  /* Use repeatable "random" sequence for consistent measurements.     */
  /*-------------------------------------------------------------------*/
  srand(0x1234);

  /*-------------------------------------------------------------------*/
  /* Synchronize with tick interrupt and get a starting time stamp.    */
  /*-------------------------------------------------------------------*/
  printf("Beginning binary search test loop\n");
  taskSleep(OsTicksPerSec / 2);
  sample = clock();

  /*-------------------------------------------------------------------*/
  /* Make multiple queries for records matching key value.             */
  /*-------------------------------------------------------------------*/
  for (i = 0; i < 5000; ++i)
  {
    /*-----------------------------------------------------------------*/
    /* Choose a random value to search for.                            */
    /*-----------------------------------------------------------------*/
    key = rand() % HI_KEY_NUM;

    /*-----------------------------------------------------------------*/
    /* Implement binary search algorithm.                              */
    /*-----------------------------------------------------------------*/
    low = 0; high = HI_KEY_NUM;
    for (;;)
    {
      /*---------------------------------------------------------------*/
      /* Seek to half-way point.                                       */
      /*---------------------------------------------------------------*/
      pos = (low + high) >> 1;
      sc = lseek(fid, pos * sizeof(Record), SEEK_SET);
      if (sc == (off_t)-1)

⌨️ 快捷键说明

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