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

📄 sd_file.c

📁 ARM实例程序
💻 C
📖 第 1 页 / 共 2 页
字号:
      }
   }
}

/*----------------------------------------------------------------------------
 *        Copy a File
 *---------------------------------------------------------------------------*/
static void cmd_copy (char *par) {
   char *fname,*fnew,*fmer,*next;
   FILE *fin,*fout;
   U32 cnt,total;
   char buf[512];
   BOOL merge;

   fname = get_entry (par, &next);
   if (fname == NULL) {
      printf ("\nFilename missing.\n");
      return;
   }
   fmer = get_entry (next, &next);
   if (fmer == NULL) {
      printf ("\nNew Filename missing.\n");
      return;
   }
   fnew = get_entry (next, &next);
   if (fnew != NULL) {
      merge = __TRUE;
   }
   else {
      merge = __FALSE;
      fnew = fmer;
   }
   if ((strcmp (fname,fnew) == 0)        ||
       (merge && strcmp (fmer,fnew) == 0)) {
      printf ("\nNew name is the same.\n");
      return;
   }

   fin = fopen (fname,"r");           /* open the file for reading           */
   if (fin == NULL) {
      printf ("\nFile %s not found!\n",fname);
      return;
   }

   if (merge == __FALSE) {
      printf ("\nCopy file %s to %s\n",fname,fnew);
   }
   else {
      printf ("\nCopy file %s, %s to %s\n",fname,fmer,fnew);
   }
   fout = fopen (fnew,"w");           /* open the file for writing           */
   if (fout == NULL) {
      printf ("\nFailed to open %s for writing!\n",fnew);
      fclose (fin);
      return;
   }

   total = 0;
   while ((cnt = fread (&buf, 1, 512, fin)) != 0) {
      fwrite (&buf, 1, cnt, fout);
      total += cnt;
   }
   fclose (fin);                      /* close input file when done          */

   if (merge == __TRUE) {
      fin = fopen (fmer,"r");         /* open the file for reading           */
      if (fin == NULL) {
         printf ("\nFile %s not found!\n",fmer);
      }
      else {
         while ((cnt = fread (&buf, 1, 512, fin)) != 0) {
            fwrite (&buf, 1, cnt, fout);
            total += cnt;
         }
         fclose (fin);
      }
   }
   fclose (fout);
   dot_format (total, &buf[0]);
   printf ("\n%s bytes copied.\n", &buf[0]);
}

/*----------------------------------------------------------------------------
 *        Delete a File
 *---------------------------------------------------------------------------*/
static void cmd_delete (char *par) {
   char *fname,*next,dir;

   fname = get_entry (par, &next);
   if (fname == NULL) {
      printf ("\nFilename missing.\n");
      return;
   }

   dir = 0;
   if (*(fname + strlen(fname) - 1) == '\\') {
      dir = 1;
   }

   if (fdelete (fname) == 0) {
      if (dir) {
         printf ("\nDirectory %s deleted.\n",fname);
      }
      else {
         printf ("\nFile %s deleted.\n",fname);
      }
   }
   else {
      if (dir) {
         printf ("\nDirectory %s not found or not empty.\n",fname);
      }
      else {
         printf ("\nFile %s not found.\n",fname);
      }
   }
}

/*----------------------------------------------------------------------------
 *        Print a Flash Memory Card Directory
 *---------------------------------------------------------------------------*/
static void cmd_dir (char *par) {
   U32 fsize,files,dirs,i;
   char temp[32],*mask,*next,ch;
   FINFO info;

   mask = get_entry (par, &next);
   if (mask == NULL) {
      mask = "*.*";
   }

   printf ("\nFile System Directory...");
   files = 0;
   dirs  = 0;
   fsize = 0;
   info.fileID  = 0;
   while (ffind (mask,&info) == 0) {
      if (info.attrib & ATTR_DIRECTORY) {
         i = 0;
         while (strlen(info.name+i) > 41) {
            ch = info.name[i+41];
            info.name[i+41] = 0;
            printf ("\n%-41s", &info.name[i]);
            info.name[i+41] = ch;
            i += 41;
         }
         printf ("\n%-41s    <DIR>       ", &info.name[i]);
         printf ("  %02d.%02d.%04d  %02d:%02d",
                  info.time.day, info.time.mon, info.time.year,
                  info.time.hr, info.time.min);
         dirs++;
      }
      else {
         dot_format (info.size, &temp[0]);
         i = 0;
         while (strlen(info.name+i) > 41) {
            ch = info.name[i+41];
            info.name[i+41] = 0;
            printf ("\n%-41s", &info.name[i]);
            info.name[i+41] = ch;
            i += 41;
         }
         printf ("\n%-41s %14s ", &info.name[i], temp);
         printf ("  %02d.%02d.%04d  %02d:%02d",
                  info.time.day, info.time.mon, info.time.year,
                  info.time.hr, info.time.min);
         fsize += info.size;
         files++;
      }
   }
   if (info.fileID == 0) {
      printf ("\nNo files...");
   }
   else {
      dot_format (fsize, &temp[0]);
      printf ("\n              %9d File(s)    %21s bytes", files, temp);
   }
   dot_format (ffree(""), &temp[0]);
   if (dirs) {
      printf ("\n              %9d Dir(s)     %21s bytes free.\n", dirs, temp);
   }
   else {
      printf ("\n%56s bytes free.\n",temp);
   }
}

/*----------------------------------------------------------------------------
 *        Format a Flash Memory Card
 *---------------------------------------------------------------------------*/
static void cmd_format (char *par) {
   char *label,*next;
   U32 retv;

   label = get_entry (par, &next);
   if (label == NULL) {
      label = "KEIL";
   }
   printf ("\nFormat Flash Memory Card? [Y/N]\n");
   retv = getkey();
   if (retv == 'y' || retv == 'Y') {
      /* Format the Card with Label "KEIL". "*/
      if (fformat (label) == 0) {
         printf ("Memory Card Formatted.\n");
         printf ("Card Label is %s\n",label);
      }
      else {
         printf ("Formatting failed.\n");
      }
   }
}

/*----------------------------------------------------------------------------
 *        Display Command Syntax help
 *---------------------------------------------------------------------------*/
static void cmd_help (char *par) {
   printf (help);
}

/*----------------------------------------------------------------------------
 *        Initialize a Flash Memory Card
 *---------------------------------------------------------------------------*/
static void init_card (void) {
   U32 retv;

   while ((retv = finit ()) != 0) {           /* Wait until the Card is ready*/
      if (retv == 1) {
         printf ("\nSD/MMC Init Failed");
         printf ("\nInsert Memory card and press key...\n");
         getkey ();
      }
      else {
         printf ("\nSD/MMC Card is Unformatted");
         strcpy (&in_line[0], "KEIL\r\n");
         cmd_format (&in_line[0]);
      }
   }
}

/*----------------------------------------------------------------------------
 *        Main: 
 *---------------------------------------------------------------------------*/
int main (void) {
   char *sp,*cp,*next;
   U32 i;

   init_comm ();                              /* init communication interface*/
//   init_display ();
   printf (intro);                            /* display example info        */
   printf (help);
   printf (reading_free);

   init_card ();
   while (1) {
      printf ("\nCmd> ");                     /* display prompt              */
      fflush (stdout);
                                              /* get command line input      */
      if (getline (in_line, sizeof (in_line)) == __FALSE) {
         continue;
      }

      sp = get_entry (&in_line[0], &next);
      if (*sp == 0) {
         continue;
      }
      for (cp = sp; *cp && *cp != ' '; cp++) {
         *cp = toupper (*cp);                 /* command to upper-case       */
      }
      for (i = 0; i < CMD_COUNT; i++) {
         if (strcmp (sp, (const S8 *)&cmd[i].val)) {
            continue;
         }
         init_card();                         /* check if card is removed    */
         cmd[i].func (next);                  /* execute command function    */
         break;
      }
      if (i == CMD_COUNT) {
        printf ("\nCommand error\n");
      }
   }
}


/*----------------------------------------------------------------------------
 * end of file
 *---------------------------------------------------------------------------*/

⌨️ 快捷键说明

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