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

📄 yb_base.cpp

📁 简单实用, 功能强大的 C++ Builder 串口控件! 本控件是免费的, 不需要注册, 有关授权及许可协议详见 license.txt 文件。 1.支持二进制数据和文本数据的收发 2.支
💻 CPP
📖 第 1 页 / 共 5 页
字号:
void DateX::DateToY111(void) // _Year, _Month, _Day --> _Days111
 {
   int  i;
   long LeapYn;

   _lDays111 = (_Year-1L) * 365;

   for(i=1; i<_Month; i++)
     _lDays111+=GetMonthDays(i);
   _lDays111+=_Day;

   LeapYn =  (_Year-1L) /  4;
   LeapYn -= (_Year-1L) /100;
   LeapYn += (_Year-1L) /400;
   _lDays111+=LeapYn;
 }
//--------------------------------------------------------------------------

void DateX::Y111ToDate(void) // _Days111 --> _Year, _Month, _Day
 {
   long Days  = _lDays111;
   long Years;
   int  i;

   Years=(Days/146097L)*400;
   Days%=146097L;
   Years+=(Days/36524L)*100;
   Days%=36524L;
   Years+=(Days/1461)*4;
   Days%=1461;
   Years+=Days/365;
   Days%=365;

   if(!Days)
    {
      _Year  = Years;
      _Month = 12;
      _Day   = 31;
    }
   else
    {
      _Year  = Years+1;
      _Month = 1;

      for(i=1; Days>GetMonthDays(i); i++)
       {
         _Month++;
         Days-=GetMonthDays(i);
       }

      _Day = (int)Days;
    }
 }
//--------------------------------------------------------------------------

bool DateX::_isLeap(void)
 {
   return _Year%4 == 0 && _Year%100 != 0 || _Year%400 == 0;
 }
//--------------------------------------------------------------------------

bool DateX::_isValid(void)
 {
   DateX d(*this); d.D111 = D111;
   return (d.YearL == YearL) && (d.Month==Month) && (d.Day==Day);
 }
//--------------------------------------------------------------------------

void DateX::SetD111(long Days111)
 {
   _lDays111 = Days111;
   Y111ToDate(); // _Days111 --> _Year, _Month, _Day
   AssignDOW();  // _Days111 --> _DOW
 }

//=========================================================================//
//                              class TimeX                                //
//=========================================================================//
void TimeX::Assign(TDateTime x)
 {
   unsigned short h,n,s, v;
   DecodeTime(x, h,n,s, v);
   Assign(h,n,s);
 }

void TimeX::Assign(int Hour, int Minute, int Second)
 {
   _Hour   = Hour;
   _Minute = Minute;
   _Second = Second;
   TimeToT000();
 }

void TimeX::SetSecs000(long Second000)
 {
   _lSeconds000 = Second000;
   T000ToTime();
 }

void TimeX::TimeToT000(void)
 {
   _lSeconds000 = _Hour*3600L + _Minute*60L + _Second;
 }

void TimeX::T000ToTime(void)
 {
   long Secs = _lSeconds000;
   _Hour   = (int)(Secs / 3600);   Secs %= 3600;
   _Minute = (int)(Secs / 60  );   Secs %= 60;
   _Second = (int)(Secs       );
 }

bool TimeX::_isValid(void)
 {
   TimeX t(*this); t.S000 = S000;
   return (t.Hour == Hour) && (t.Minute == Minute) && (t.Second == Second);
 }

//=========================================================================//
//                           class DateTimeX                               //
//=========================================================================//

void DateTimeX::Assign(__int64 i)
{
  TimeStamp = i;
}

void DateTimeX::Assign(TDateTime x)
{
  unsigned short y,m,d, h,n,s, v;
  DecodeDate(x, y,m,d);
  DecodeTime(x, h,n,s, v);
  Assign(y,m,d, h,n,s);
}

void DateTimeX::Assign(DateX d, TimeX t)
{
  _xDate = d;
  _xTime = t;
}

void DateTimeX::Assign(int y, int m, int d, int h, int n, int s)
{
  _xDate.Assign(y,m,d);
  _xTime.Assign(h,n,s);
}
//--------------------------------------------------------------------------
__int64 DateTimeX::fGetTStamp(void)
{
  return (__int64)_xDate.D111*86400i64 + _xTime.S000;
}

void DateTimeX::fSetTStamp(__int64 t)
{
  _xDate.D111 = t/86400i64;
  _xTime.S000 = t%86400i64;
}
//--------------------------------------------------------------------------

bool DateTimeX::fIsValid(void)
{
  DateTimeX x(TimeStamp);
  return (x.Year == Year) && (x.Month == Month) && (x.Day == Day)
      && (x.Hour == Hour) && (x.Minute == Minute) && (x.Second == Second);
}
//--------------------------------------------------------------------------

bool DateTimeX::SetToSystem(void)
{
  if(!IsValid)
    return false;

  struct date dd;
  struct time tt;

  tt.ti_hour = Hour;
  tt.ti_min  = Minute;
  tt.ti_sec  = 0;
  tt.ti_hund = 0;
  settime(&tt);

  dd.da_year = Year;
  dd.da_mon  = Month;
  dd.da_day  = Day;
  setdate(&dd);

  tt.ti_hour = Hour;
  tt.ti_min  = Minute;
  tt.ti_sec  = Second;
  tt.ti_hund = 0;
  settime(&tt);

  return true;
}

//=========================================================================//
//                               TRelPath                                  //
//=========================================================================//
TRelPath::TRelPath(TVictorString asRelative)
{
  Initialize(ptExecutable, "", asRelative);
}
//--------------------------------------------------------------------------

TRelPath::TRelPath(TPathType ptType, TVictorString asRelative)
{
  Initialize(ptType, "", asRelative);
}
//--------------------------------------------------------------------------

TRelPath::TRelPath(TVictorString asRootPath, TVictorString asRelative)
{
  Initialize(ptCustom, asRootPath, asRelative);
}
//--------------------------------------------------------------------------

void TRelPath::Initialize(TPathType ptType, TVictorString asRootPath, TVictorString asRelative) //Called internally
{
  //--- Execute Full Path-FileName ---
  if(_argc>0)
   {
     _asExeFulln = _argv[0];
   }
  else
   {
     char ExeFullName[MAXPATH+1];
     GetModuleFileName(0, ExeFullName, MAXPATH);
     _asExeFulln = ExeFullName;
   }

  //--- PathType ----
  SetPathType(ptType);

  //--- RootPath ----
  if(ptType==ptCustom)
    SetRootPath(asRootPath);

  //--- Relative ----
  SetRelative(asRelative);
}
//--------------------------------------------------------------------------

TVictorString TRelPath::FileName(TVictorString s)  //RootPath+Relative+s
{
  TVictorString RetVal = _asRootPath + _asRelative;

  if(s.IsEmpty())
   {
     RetVal += TBinFileFuncs::GetFileName(_asExeFulln);
   }
  else
   {
     RetVal += s;
   }
  return RetVal;
}
//--------------------------------------------------------------------------

TVictorString TRelPath::FileName(TVictorString s, TVictorString e)  //RootPath+Relative+s+"."+e
{
  TVictorString RetVal = FileName(s);
  if(!e.IsEmpty())
    RetVal += "." + e;
  return RetVal;
}
//--------------------------------------------------------------------------

TVictorString TRelPath::Extension(TVictorString s)  //RootPath+Relative+ExeFileName+s
{
  TVictorString RetVal = _asRootPath + _asRelative;

  TVictorString FnExt = TBinFileFuncs::GetFileName(_asExeFulln);
  if(s.IsEmpty())
   {
     RetVal += FnExt;
   }
  else
   {
     TVictorString Fname = TBinFileFuncs::GetFullNameWithoutExt(FnExt);
     RetVal += Fname +"."+ s;
   }

  return RetVal;
}
//--------------------------------------------------------------------------

TVictorString TRelPath::FormatName(const char *fmt, ...)
{
  char s[MAXPATH];
  va_list argptr;

  va_start(argptr, fmt);
  vsprintf(s, fmt, argptr);
  va_end(argptr);

  return FileName(s);
}
//--------------------------------------------------------------------------

TVictorString TRelPath::FormatExtension(const char *fmt, ...) //printf(fmt, ...);
{
  char s[MAXPATH];
  va_list argptr;

  va_start(argptr, fmt);
  vsprintf(s, fmt, argptr);
  va_end(argptr);

  return Extension(s);
}
//--------------------------------------------------------------------------

void TRelPath::SetPathType(TPathType t)
{
  char szBuffer[MAXPATH+1];
  //--- _RootPath ---
  _PathType = t;
  switch(_PathType)
   {
     case ptExecutable: //执行文件的路径
       _asRootPath = TBinFileFuncs::GetPathFromFullName(_asExeFulln);
       break;

     case ptWindows   : //Windows 路径 "C:\\WINDOWS\\"
       if(GetWindowsDirectory(szBuffer, MAXPATH))
         _asRootPath = TBinFileFuncs::AddBackSlash(szBuffer);
       else
         _asRootPath = "C:\\WINDOWS\\";
       break;

     case ptSystem    : //System 路径 "C:\\WINDOWS\\SYSTEM"
       if(GetSystemDirectory(szBuffer, MAXPATH))
         _asRootPath = TBinFileFuncs::AddBackSlash(szBuffer);
       else
         _asRootPath = "C:\\WINDOWS\\SYSTEM\\";
       break;

     case ptCurrent   : //当前路径
       if(GetCurrentDirectory(MAXPATH, szBuffer))
         _asRootPath = TBinFileFuncs::AddBackSlash(szBuffer);
       else
         _asRootPath = "";
       break;

     case ptTemporary: //临时路径
       if(GetTempPath(MAXPATH, szBuffer))
         _asRootPath = TBinFileFuncs::AddBackSlash(szBuffer);
       else
         _asRootPath = "";
       break;

     case ptModule: //Module Path
       if(GetModuleFileName(HInstance, szBuffer, MAXPATH))
        {
          _asExeFulln = szBuffer;
          _asRootPath = TBinFileFuncs::GetPathFromFullName(_asExeFulln);
        }
       else
        {
          _asRootPath = "";
        }
       break;

     case ptProgFiles: // C:\\Program Files
       if(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, SHGFP_TYPE_CURRENT, szBuffer) != E_INVALIDARG)
         _asRootPath = TBinFileFuncs::AddBackSlash(szBuffer);
       else
         _asRootPath = "C:\\Program Files\\";
       break;

     case ptProgCommon: // C:\\Program Files\\Common Files
       if(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES_COMMON, NULL, SHGFP_TYPE_CURRENT, szBuffer) != E_INVALIDARG)
         _asRootPath = TBinFileFuncs::AddBackSlash(szBuffer);
       else
         _asRootPath = "C:\\Program Files\\Common Files\\";
       break;

     case ptDesktop: // C:\\Documents and Settings\\User Name\\Desktop
       if(SHGetFolderPath(NULL, CSIDL_DESKTOP, NULL, SHGFP_TYPE_CURRENT, szBuffer) != E_INVALIDARG)
         _asRootPath = TBinFileFuncs::AddBackSlash(szBuffer);
       else
         _asRootPath = "C:\\WINDOWS\\DESKTOP\\";
       break;

     case ptMyDocuments: // C:\\Documents and Settings\\User Name\\My Documents
       if(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, szBuffer) != E_INVALIDARG)
         _asRootPath = TBinFileFuncs::AddBackSlash(szBuffer);
       else
         _asRootPath = "C:\\My Documents\\";
       break;

     case ptAppData: // C:\\Documents and Settings\\username\\Application Data
       if(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, szBuffer) != E_INVALIDARG)
         _asRootPath = TBinFileFuncs::AddBackSlash(szBuffer);
       else
         _asRootPath = "C:\\WINDOWS\\Application Data\\";
       break;

     case ptStartMenu: // C:\\Documents and Settings\\username\\Start Menu
       if(SHGetFolderPath(NULL, CSIDL_STARTMENU, NULL, SHGFP_TYPE_CURRENT, szBuffer) != E_INVALIDARG)
         _asRootPath = TBinFileFuncs::AddBackSlash(szBuffer);
       else
         _asRootPath = "C:\\WINDOWS\\Start Menu\\";
       break;

     case ptStMenuProg: // C:\\Documents and Settings\\username\\Start Menu\\Programs
       if(SHGetFolderPath(NULL, CSIDL_PROGRAMS, NULL, SHGFP_TYPE_CURRENT, szBuffer) != E_INVALIDARG)
         _asRootPath = TBinFileFuncs::AddBackSlash(szBuffer);
       else
         _asRootPath = "C:\\WINDOWS\\Start Menu\\Programs\\";
       break;

     case ptStMenuStartup: // C:\\Documents and Settings\\username\\Start Menu\\Programs\\Startup
       if(SHGetFolderPath(NULL, CSIDL_STARTUP, NULL, SHGFP_TYPE_CURRENT, szBuffer) != E_INVALIDARG)
         _asRootPath = TBinFileFuncs::AddBackSlash(szBuffer);
       else
         _asRootPath = "C:\\WINDOWS\\Start Menu\\Startup\\";
       break;

     default: //ptCustom,

⌨️ 快捷键说明

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