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

📄 sft.cpp

📁 通过串口发送接受文件
💻 CPP
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <dir.h>
USERES("SFT.res");
USEFORM("Main.cpp", frmMain);
USEFORM("Setup.cpp", frmSetup);
USEUNIT("Comport.cpp");
USEFORM("Message.cpp", frmMessage);
USEFORM("Upload.cpp", frmUpload);
USEUNIT("ModemDialer.cpp");
USEFORM("Dialer.cpp", frmDialer);
USEUNIT("ModemAnswer.cpp");
USEFORM("AnswerCall.cpp", frmAnswer);
USEFORM("About.cpp", frmAbout);
USEFORM("Download.cpp", frmDownload);
USEUNIT("_SFT.cpp");
//---------------------------------------------------------------------------
#include "main.h"

void CommandHelp(void)
{
  String Help;
  Help=     "SFT命令行方式使用说明:\r\n\r\n";
  Help=Help+"SFT -upload filename1 filename2 filename3(如果使用带空格的长文件名,\r\n";
  Help=Help+"                                           要在两端加双引号)\r\n";
  Help=Help+"SFT -download downloadpathname(要下载到的目录)\r\n";
  Help=Help+"SFT -download downloadfilename(要下载为的文件名)\r\n\r\n";
  Help=Help+"注意:SFT的通信设置由SFT.ini决定,也就是在Window下使用时的设置\r\n";
  Help=Help+"     如果省略filename和downloadpathname,则由SFT.ini决定它们\r\n\r\n";
  Help=Help+"例: SFT -upload data0.dat data1.dat data2.dat\r\n";
  Help=Help+"    SFT -upload \"c:\\my documents\\data0.dat\" \"c:\\my documents\\data1.dat\"\r\n";
  Help=Help+"    SFT -download c:\\my documents\r\n";
  Help=Help+"    SFT -download c:\\my documents\\data.txt\r\n";
  Application->MessageBox(Help.c_str(),"命令行(Command Line)帮助",MB_OK);
  return;
}

extern void LoadSettings(void);
bool CommandUpload=False;
bool CommandDownload=False;
TStringList *CommandUploadFiles=new TStringList;
char CommandDownloadPath[256];
char CommandDownloadFileName[256];

static void StrTrim(char *str)
{
  int i;
  for( i=strlen(str)-1;i>=0;i-- )
    {
    if( str[i]!=' ' )break;
    };
  str[i+1]=0;
  for( i=0;i<(int)strlen(str);i++ )
    {
    if( str[i]!=' ' )break;
    };
  memcpy( str,&str[i],strlen(str)-i+1 );
}

static bool GetFile( char *str,char *filename )
{
  int i,k;
  int flag;
  filename[0]=0;
  k=0;
  flag=0;
  for( i=0;i<(int)strlen(str);i++ )
    {
    if( flag==0 )
      {
      if( str[i]==' ' )
        {
        if( k>0 )
          {
          filename[k]=0;
          memcpy( str,&str[i],strlen(str)-i+1 );
          return True;
          }
        else
          {
          continue;
          };
        };
      };
    if( flag==1 )
      {
      if( str[i]=='\"' )
        {
        filename[k]=0;
        memcpy( str,&str[i],strlen(str)-i+1 );
        return True;
        };
      };
    if( flag==0 )
      {
      if( str[i]=='\"' )
        {
        flag=1;
        continue;
        };
      };
    filename[k]=str[i];
    k++;
    };
  if( k>0 )
    {
    filename[k]=0;
    memcpy( str,&str[i],strlen(str)-i+1 );
    return True;
    };
  return False;
} 

//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR Command, int)
{
  char ss[256];
  strcpy( ss,Command );
  StrTrim(ss);
  strcpy( CommandDownloadFileName,"" );
  if( strlen(ss)>0 )
    {
    if( strncmp(Command,"/?",2)==0 )
      {
      CommandHelp();
      return 0;
      }
    else if( strncmpi(Command,"-download",9)==0 )
      {
      String Path;
      CommandDownload=True;
      strcpy( CommandDownloadPath,&Command[9] );
      StrTrim(CommandDownloadPath);
      if( CommandDownloadPath[0]=='\"' )
        memcpy( CommandDownloadPath,&CommandDownloadPath[1],strlen(CommandDownloadPath) );
      if( CommandDownloadPath[strlen(CommandDownloadPath)-1]=='\"' )
        CommandDownloadPath[strlen(CommandDownloadPath)-1]=0;
      Path = CommandDownloadPath;
      if( strlen(ExtractFileName(Path).c_str())>0 )
        {
        strcpy( CommandDownloadFileName,Path.c_str() );
        strcpy( CommandDownloadPath,ExtractFilePath(Path).c_str() );
        };
      StrTrim(CommandDownloadPath);
      }
    else
      {
      char str[256],filename[256];
      CommandUpload=True;
      strcpy( str,Command );
      if( strncmpi(Command,"-upload",7)==0 )strcpy(str,&Command[7]);
      while( GetFile(str,filename)==True )
        {
        String FullFileName,FileName;
        String Path;
        char buf[256],buf1[256];
        StrTrim(filename);
        if( strlen(filename)==0 )continue;
        FullFileName=filename;
        Path = ExtractFilePath( FullFileName );
        strcpy( buf,Path.c_str() );
        if( buf[0]==0 )
          {
          getcwd( buf,255 );
          if( buf[strlen(buf)-1]!='\\' )strcat( buf,"\\" );
          strcpy( buf1,buf );
          strcat( buf1,filename );
          strcpy( filename,buf1 );
          };
        if( strlen(filename)>0 )CommandUploadFiles->Add(filename);
        };
      };
    };
  try
        {
                 Application->Initialize();
                 Application->Title = "SFT";
                 Application->CreateForm(__classid(TfrmMain), &frmMain);
         Application->CreateForm(__classid(TfrmSetup), &frmSetup);
         Application->CreateForm(__classid(TfrmMessage), &frmMessage);
         Application->CreateForm(__classid(TfrmUpload), &frmUpload);
         Application->CreateForm(__classid(TfrmDialer), &frmDialer);
         Application->CreateForm(__classid(TfrmAnswer), &frmAnswer);
         Application->CreateForm(__classid(TfrmAbout), &frmAbout);
         Application->CreateForm(__classid(TfrmDownload), &frmDownload);
         LoadSettings();
                 if( CommandUpload==True )
                   {
                   if( CommandUploadFiles->Count>0 )
                     {
                     int i;
                     frmMain->ListBox1->Clear();
                     for( i=0;i<CommandUploadFiles->Count;i++ )
                       {
                       frmMain->ListBox1->Items->Add(CommandUploadFiles->Strings[i]);
                       };
                     };
                   };
                 if( CommandDownload==True )
                   {
                    char buf[256];
                    strcpy( buf,CommandDownloadPath );
                    try
                      {
                      frmMain->DriveComboBox1->Drive=buf[0];
                      frmMain->DirectoryListBox1->Directory = CommandDownloadPath;
                      }
                    catch(...)
                      {
                      };
                   };
                 Application->Run();
        }
        catch (Exception &exception)
        {
                 Application->ShowException(&exception);
        }
        return 0;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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