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

📄 base_obj.cpp

📁 a program that generates a pulse-width modulated (PWM)signal.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//      Overloaded left-shift operator to add character arrays into a CString object 
//      in much the same way as the << operator works in cout.  

CString& CString::operator<< (const char *StrToAdd)
    {
    size_t BytesCopied;                 //  How many bytes copied into buffer this time 
    size_t AddLength;                   //  Length of the string given in 'StrToAdd'


    //  If this object is already full and we have created another object to hold the 
    //  overflow, just send the string along to that next object 
    if (NextString != NULL)
        (*NextString) << (char *)StrToAdd;

    //  If this string object's buffer couldn't be allocated, we've filled memory, so 
    //  give up on trying to store further items - just dump them (it's safer this way!) 
    //  If there's room, copy as much as we can into buffer of this CString object.  If
    //  there's still more data to be held, make a new object to hold it.  
    else if (BufferSize > 0)
        {
        AddLength = strlen (StrToAdd);                  //  How big is string to store 
        BytesCopied = minimum (AddLength, SizeLeft);    //  How many bytes we'll copy 
        strncat (TheBuffer, StrToAdd, SizeLeft);        //  Copy as much as we can 
        SizeLeft -= BytesCopied;                        //  How much space is left 

        //  If there's uncopied text left over, make a new CString to hold it and add 
        //  in the remaining part of the string 
        if (SizeLeft <= 0)
            {
            NextString = new CString ((size_t)(BufferSize + 1));
            if (NextString != NULL)
                (*NextString) << (StrToAdd + BytesCopied);
            }
        }

    return (*this);
    }


//-------------------------------------------------------------------------------------
//  Operators: << (for everything except character pointers) 
//      These overloaded add-accumulate operators convert the given data into a char-
//      acter string and then call the character string version to appends it to the 
//      file object.  COMPILER DEPENDENCY NOTE:  Visual C++ doesn't support the normal 
//      itoa() function, so you'll have to make changes if you port these functions.  

CString& CString::operator<< (char aChar)
    {
    char aBuf[4];                   //  Room for any possible character as a string 


    itoa ((int)aChar, aBuf, 10);   //  Convert the character to a signed integer,
    *this << aBuf;                  //  then add it to the string 
    return (*this);                 //  Return a reference to the file object 
    }

CString& CString::operator<< (unsigned char aChar)
    {
    char aBuf[4];                               //  Room for any character as a string 

    itoa ((unsigned int)aChar, aBuf, 10);       //  Convert this one to *unsigned* int
    *this << aBuf; 
    return (*this);
    }

CString& CString::operator<< (int aNumber)
    {
    char aBuf[18];                  //  Give ample room for any possible int as string 

    itoa (aNumber, aBuf, 10);      //  Use itoa() to convert integer to string
    *this << aBuf;                  //  Call << to add string to 'this' file object
    return (*this);
    }

CString& CString::operator<< (unsigned int aNumber)
    {
    char aBuf[36];

    //  An odd way to get an unsigned int converted into a character string without
    //  allowing the value to represent a negative number, even if the first bit is 1
    *this << ultoa (((unsigned long)((unsigned)aNumber)), aBuf, 10);
    return (*this);
    }

CString& CString::operator<< (short aNumber)
    {
    char aBuf[36];

    //  This conversion should work on any system where shorts are shorter than longs
    //  (though you may need to hunt for a replacement for the _ltoa() function)
    *this << ltoa ((long)aNumber, aBuf, 10);
    return (*this);
    }

CString& CString::operator<< (unsigned short aNumber)
    {
    char aBuf[36];

    *this << ultoa (((unsigned long)aNumber), aBuf, 10);
    return (*this);
    }

CString& CString::operator<< (long aNumber)
    {
    char aBuf[36];

    *this << ltoa (aNumber, aBuf, 10);
    return (*this);
    }

CString& CString::operator<< (unsigned long aNumber)
    {
    char aBuf[36];

    *this << ultoa (aNumber, aBuf, 10);
    return (*this);
    }

CString& CString::operator<< (float aNumber)
    {
    char aBuf[36];

    sprintf (aBuf, "%g", aNumber);
    *this << aBuf;
    return (*this);
    }

CString& CString::operator<< (double aNumber)
    {
    char aBuf[36];

    sprintf (aBuf, "%lg", aNumber);
    *this << aBuf;
    return (*this);
    }

CString& CString::operator<< (long double aNumber)
    {
    char aBuf[36];

    sprintf (aBuf, "%Lg", aNumber);
    *this << aBuf;
    return (*this);
    }


//=====================================================================================
//  Class: CFileObj
//      In order to make dealing with data files more streamlined, we here implement
//      a class which opens and closes a file and can give whoever needs it a pointer
//      to that file.  The file pointer can be used for normal C reading and writing.
//      Usually, the file buffer is used so writing can be done quickly in real time 
//      and the file I/O can be done later.  For the user, writing to the file object 
//      is done with << operators in a manner similar to the way you use cout.  
//=====================================================================================

//-------------------------------------------------------------------------------------
//  Constructor: CFileObj (version for file with buffer) 
//      This constructor opens up a data file to which we can write, allocates space 
//      for a buffer in which to store stuff, and sets the allow-writing flag.  If the
//      buffer size is zero, it sets the use-no-buffer flag.  

CFileObj::CFileObj (const char *aName, const char *aMode, size_t BufferSize)
    {
    NeedToClose = false;                        //  Unless we open a file, why close it

    FileHandle = fopen (aName, aMode);          //  Try opening that file
    if (FileHandle != NULL)
        NeedToClose = true;

    Buffer = NULL;                              //  Default: no buffer is used
    if (BufferSize > 0)                         //  If we are using a buffer
        Buffer = new CString (BufferSize);
    }


//-------------------------------------------------------------------------------------
//  Constructor: CFileObj (no-buffer version)
//      This constructor opens up a data file to which we can write and sets the allow
//      writing flag.  It also sets the use-no-buffer flag.

CFileObj::CFileObj (const char *aName, const char *aMode)
    {
    NeedToClose = false;                        //  Unless we open a file, why close it

    Buffer = NULL;                              //  Don't use a buffer at all 
    FileHandle = fopen (aName, aMode);
    if (FileHandle != NULL)                     //  But still make sure the file's open
        NeedToClose = true;
    }


//-------------------------------------------------------------------------------------
//  Constructor: CFileObj (given a file pointer) 
//      This constructor creates a CFileObj object which just writes to the file whose
//      handle is given in the argument without using a buffer.  The file should 
//      already be open.  

CFileObj::CFileObj (FILE *aFile)
    {
    NeedToClose = false;                    //  Someone else's file; they'll close it
    Buffer = NULL;
    FileHandle = aFile;
    }


//-------------------------------------------------------------------------------------
//  Destructor: ~CFileObj
//      This destructor closes the file (flushing the file buffer automatically to 
//      disk) and frees the CString (memory buffer) memory, if one is used.  

CFileObj::~CFileObj (void)
    {
    Flush ();                   //  Get buffer contents into file 

    if (NeedToClose == true)    //  Then close the file
        fclose (FileHandle);

    if (Buffer != NULL)         //  And delete the buffer
        delete (Buffer);
    }


//-------------------------------------------------------------------------------------
//  Function: Flush 
//      This function flushes the contents of the file buffer to the disk file.  
//      Having done so it also clears out the buffer.  

void CFileObj::Flush (void)
    {
    if ((Buffer != NULL) && (FileHandle != NULL))   //  Make sure file & buffer are OK
        {
        Buffer->WriteToFile (FileHandle);
        *Buffer = "";
        }
    }


//-------------------------------------------------------------------------------------
//  Operator: << (for character pointer) 
//      Overloaded left-shift operator to add character strings into a file object in 
//      the same way as the << operator works in cout.  

CFileObj& CFileObj::operator<< (const char *NewString)
    {
    if (Buffer != NULL)             //  If we're using the file in buffered mode, 
        *Buffer << NewString;       //  "add" the text into the CString buffer object 

    else if (FileHandle != NULL)
        while (*NewString != '\0')              //  If not in buffered mode, write 
            fputc (*NewString++, FileHandle);   //  directly to the file 

    return (*this);                 //  Return a reference to the file object 
    }


//-------------------------------------------------------------------------------------
//  Operators: << (for everything except character pointers) 
//      These overloaded add-accumulate operators convert the given data into a char- 
//      acter string and then call the character string version to appends it to the 
//      file object.  COMPILER DEPENDENCY NOTE:  Visual C++ doesn't support the normal 
//      itoa() function, so you'll have to make changes if you port these functions.  

CFileObj& CFileObj::operator<< (char aChar)
    {
    char aBuf[4];                   //  Room for any possible character as a string 


    itoa ((int)aChar, aBuf, 10);    //  Convert the character to a signed integer,
    *this << aBuf;                  //  then add it to the string
    return (*this);                 //  Return a reference to the file object
    }

CFileObj& CFileObj::operator<< (unsigned char aChar)
    {
    char aBuf[4];                               //  Room for any character as a string

    itoa ((unsigned int)aChar, aBuf, 10);       //  Convert this one to *unsigned* int
    *this << aBuf;
    return (*this);
    }

CFileObj& CFileObj::operator<< (int aNumber)
    {
    char aBuf[18];                  //  Give ample room for any possible int as string 

    itoa (aNumber, aBuf, 10);       //  Use itoa() to convert integer to string
    *this << aBuf;                  //  Call << to add string to 'this' file object
    return (*this);
    }

CFileObj& CFileObj::operator<< (unsigned int aNumber)
    {
    char aBuf[36];

    //  An odd way to get an unsigned int converted into a character string without
    //  allowing the value to represent a negative number, even if the first bit is 1
    *this << ultoa (((unsigned long)((unsigned)aNumber)), aBuf, 10);
    return (*this);
    }

CFileObj& CFileObj::operator<< (short aNumber)
    {
    char aBuf[36];

    //  This conversion should work on any system where shorts are shorter than longs
    //  (though you may need to hunt for a replacement for the _ltoa() function)
    *this << ltoa ((long)aNumber, aBuf, 10);
    return (*this);
    }

CFileObj& CFileObj::operator<< (unsigned short aNumber)
    {
    char aBuf[36];

    *this << ultoa (((unsigned long)aNumber), aBuf, 10);
    return (*this);
    }

CFileObj& CFileObj::operator<< (long aNumber)
    {
    char aBuf[36];

    *this << ltoa (aNumber, aBuf, 10);
    return (*this);
    }

CFileObj& CFileObj::operator<< (unsigned long aNumber)
    {
    char aBuf[36];

    *this << ultoa (aNumber, aBuf, 10);
    return (*this);
    }

CFileObj& CFileObj::operator<< (float aNumber)
    {
    char aBuf[36];

    sprintf (aBuf, "%g", aNumber);
    *this << aBuf;
    return (*this);
    }

CFileObj& CFileObj::operator<< (double aNumber)
    {
    char aBuf[36];

    sprintf (aBuf, "%lg", aNumber);
    *this << aBuf;
    return (*this);
    }

CFileObj& CFileObj::operator<< (long double aNumber)
    {
    char aBuf[36]; 

    sprintf (aBuf, "%Lg", aNumber);
    *this << aBuf; 
    return (*this);
    }
    
    

⌨️ 快捷键说明

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