// Riff.cpp : 콘솔응용프로그램에대한진입점을정의합니다.

//

 

#include "stdafx.h"

#include <string.h>

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

typedef unsigned int       uint32_t;

typedef unsigned short int uint16_t;

typedef signed   short int int16_t;

typedef signed         int int32_t;

typedef unsigned char      byte_t;

 

inline uint32_t convert_endian_4byte (uint32_t v)

{

        return (((v>>24)&0xff)       )|

               (((v>>16)&0xff) << 8  )|

               (((v>> 8)&0xff) << 16 )|

               (((v    )&0xff) << 24 );

}

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

const uint32_t FOURCC_RIFF = 0x46464952; 

const uint32_t FOURCC_AVI  = 0x20495641; 

const uint32_t FOURCC_LIST = 0x5453494C; 

const uint32_t FOURCC_hdrl = 0x6C726468; 

const uint32_t FOURCC_avih = 0x68697661; 

const uint32_t FOURCC_strl = 0x6C727473; 

const uint32_t FOURCC_strh = 0x68727473; 

const uint32_t FOURCC_strf = 0x66727473; 

const uint32_t FOURCC_STRD = 0x64727473; 

const uint32_t FOURCC_vids = 0x73646976; 

const uint32_t FOURCC_auds = 0x73647561; 

const uint32_t FOURCC_INFO = 0x4F464E49; 

const uint32_t FOURCC_ISFT = 0x54465349; 

const uint32_t FOURCC_idx1 = 0x31786469; 

const uint32_t FOURCC_movi = 0x69766F6D; 

const uint32_t FOURCC_JUNK = 0x4B4E554A; 

const uint32_t FOURCC_vprp = 0x70727076; 

const uint32_t FOURCC_PAD  = 0x20444150; 

const uint32_t FOURCC_DIV3 = 861292868; 

const uint32_t FOURCC_DIVX = 1482049860; 

const uint32_t FOURCC_XVID = 1145656920; 

const uint32_t FOURCC_DX50 = 808802372; 

 

#define FOURCC(a,b,c,d) (((a)<<24)|((b)<<16)|((c)<<8)|(d))

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

typedef struct _riff_header_t

{

        uint32_t riff ;

        uint32_t size ;

        uint32_t type ;

} riff_header_t;

 

typedef struct _list_header_t

{

        uint32_t list   ;

        uint32_t size   ;

        uint32_t fourcc ;

} list_header_t;

 

typedef struct _chunk_header_t

{

        uint32_t fourcc ;

        uint32_t size   ;

} chunk_header_t;

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

typedef struct _MainAVIHeader_t

{

        uint32_t MicroSecPerFrame;   // frame display rate (or 0)

        uint32_t MaxBytesPerSec;     // max. transfer rate

        uint32_t PaddingGranularity; // pad to multiples of this size;

        uint32_t Flags;              // the ever-present flags

        uint32_t TotalFrames;        // # frames in file

        uint32_t InitialFrames;

        uint32_t Streams;

        uint32_t SuggestedBufferSize;

        uint32_t Width;

        uint32_t Height;

        uint32_t Reserved[4];

} MainAVIHeader_t;

 

typedef struct _RECT_t

{ 

    int16_t left; 

    int16_t top;  

    int16_t right;

    int16_t bottom;

} RECT_t; 

 

typedef struct _AVIStreamHeader_t

{             

    uint32_t fccType;

    uint32_t fccHandler;        

    uint32_t Flags;             

    uint16_t Priority;          

    uint16_t Language;          

    uint32_t InitialFrames;     

    uint32_t Scale;             

    uint32_t Rate;              

    uint32_t Start;             

    uint32_t Length;            

    uint32_t SuggestedBufferSize;

    uint32_t Quality;           

    uint32_t SampleSize;        

    RECT_t   FrameRect;         

} AVIStreamHeader_t; 

 

// strl - strh==vids  strf

typedef struct _BitmapInfoHeader_t

{ 

    uint32_t  biSize; 

    int32_t   biWidth; 

    int32_t   biHeight; 

    uint16_t  biPlanes; 

    uint16_t  biBitCount; 

    uint32_t  biCompression; 

    uint32_t  biSizeImage; 

    int32_t   biXPelsPerMeter; 

    int32_t   biYPelsPerMeter; 

    uint32_t  biClrUsed; 

    uint32_t  biClrImportant; 

} BitmapInfoHeader_t; 

 

// strl - strh==auds  strf

typedef struct _WaveFormatEx_t

{ 

    uint16_t FormatTag; 

    uint16_t Channels;        

    uint32_t SamplesPerSec;  

    uint32_t AvgBytesPerSec; 

    uint16_t BlockAlign;      

    uint16_t BitsPerSample;   

    uint16_t Size;           

} WaveFormatEx_t;

 

// AVI

typedef struct _avi_info_t

{

        uint32_t HasVideo;

        uint32_t HasAudio;

 

        uint32_t avi_size;

        uint32_t movi_size;

       

        uint32_t audio_data_size;

        uint32_t audio_data_count;

        uint32_t video_data_size;

        uint32_t video_data_count;

 

        MainAVIHeader_t    Header;

        AVIStreamHeader_t  Video;             // strh

        BitmapInfoHeader_t VideoBitmapInfo;   // strf

        AVIStreamHeader_t  Audio;             // strh

        WaveFormatEx_t     AudioWaveFormatEx; // strf

} avi_info_t;

 

bool read_avi_header (const char* filepath, avi_info_t* ai)

{

        FILE* fp;

 

        fp = fopen (filepath, "rb");

        if (0==fp)

        {

               return false;

        }

 

        //--------------------------------------------------------------------------

        // RIFF header

        //--------------------------------------------------------------------------

        riff_header_t riff_header;

 

        // [RIFF] size [AVI ]

        fread(&riff_header, sizeof(riff_header), 1, fp);

        printf ("%c%c%c%c %d %c%c%c%c \r\n",

               (riff_header.riff>> 0)&0xff,

               (riff_header.riff>> 8)&0xff,

               (riff_header.riff>>16)&0xff,

               (riff_header.riff>>24)&0xff,

                riff_header.size ,

               (riff_header.type>> 0)&0xff,

               (riff_header.type>> 8)&0xff,

               (riff_header.type>>16)&0xff,

               (riff_header.type>>24)&0xff

                );

 

        if (riff_header.riff != 0x46464952) // "RIFF"

        {

               fclose (fp);

               return false;

        }

 

        if (riff_header.type != 0x20495641) // "AVI "

        {

               fclose (fp);

               return false;

        }

        ai->avi_size = riff_header.size;

 

        //--------------------------------------------------------------------------

        // RIFF body

        //--------------------------------------------------------------------------

        int32_t  seek_size;

 

        uint32_t id;

        int32_t  size;

        uint32_t fourcc;

 

        uint32_t data_size;

 

        MainAVIHeader_t    MainAVIHeader;

        AVIStreamHeader_t  AVIStreamHeader;

        BitmapInfoHeader_t BitmapInfoHeader;

        WaveFormatEx_t     WaveFormatEx;

        uint32_t           strh = 0;

 

        while ( !feof(fp) )

        {

               printf ("%8d: ", ftell (fp));

 

               if (0==fread(&id, 4, 1, fp))

               {

                       break;

               }

               if (0==fread(&size,4, 1, fp))

               {

                       break;

               }

               printf ("<%c%c%c%c> %d \r\n",

                       (id>> 0)&0xff,

                       (id>> 8)&0xff,

                       (id>>16)&0xff,

                       (id>>24)&0xff,

                       size        );

               if (0==id)

               {

                       break;

               }

               if (0>=size)

               {

                       break;

               }

 

               // "LIST"

               if (id==0x5453494c)

               {

                       if (0==fread(&fourcc,  4, 1, fp))

                       {

                              break;

                       }

 

                       printf ("          %c%c%c%c\r\n",

                              (fourcc>> 0)&0xff,

                              (fourcc>> 8)&0xff,

                              (fourcc>>16)&0xff,

                              (fourcc>>24)&0xff);

 

                       if (fourcc == 0x69766F6D)

                       {

                              ai->movi_size = size;

                       }

               }

               else

               {

                       seek_size = (int32_t) size;

                       data_size = (int32_t) size;

 

                       // "avih"

                       if (id==0x68697661)

                       {

                              data_size = sizeof(MainAVIHeader);

                              if (0==fread (&MainAVIHeader, data_size, 1, fp))

                              {

                                      break;

                              }

                              memcpy (&ai->Header, &MainAVIHeader, data_size);

                             

                              seek_size = ( size!=data_size ) ?  size-data_size : 0;

                       }

 

                       // "strh"

                       if (id==0x68727473)

                       {

                              data_size = sizeof(AVIStreamHeader);

                              if (0==fread (&AVIStreamHeader, data_size, 1, fp))

                              {

                                      break;

                              }

 

                              printf ("          %c%c%c%c:%c%c%c%c\r\n",

                                      (AVIStreamHeader.fccType   >> 0)&0xff,

                                      (AVIStreamHeader.fccType   >> 8)&0xff,

                                      (AVIStreamHeader.fccType   >>16)&0xff,

                                      (AVIStreamHeader.fccType   >>24)&0xff,

                                      (AVIStreamHeader.fccHandler>> 0)&0xff,

                                      (AVIStreamHeader.fccHandler>> 8)&0xff,

                                      (AVIStreamHeader.fccHandler>>16)&0xff,

                                      (AVIStreamHeader.fccHandler>>24)&0xff);

 

                              strh = 0;

                              // "vids"

                              if (0x73646976==AVIStreamHeader.fccType)

                              {

                                      strh = 1;

                                      memcpy(&ai->Video, &AVIStreamHeader, data_size);

                                      ai->HasVideo = 1;

                              }

                              // "auds"

                              if (0x73647561==AVIStreamHeader.fccType)

                              {

                                      strh = 2;

                                      memcpy(&ai->Audio, &AVIStreamHeader, data_size);

                                      ai->HasAudio = 1;

                              }

 

                              seek_size = ( size!=data_size ) ?  size-data_size : 0;

                       }

 

                       // "strf"

                       if (id==0x66727473)

                       {

                              data_size = 0;

 

                              if (1==strh)

                              {

                                      data_size = sizeof(BitmapInfoHeader);

                                      if (0==fread (&BitmapInfoHeader, data_size, 1, fp))

                                      {

                                             break;

                                      }

                                      memcpy(&ai->VideoBitmapInfo, &BitmapInfoHeader, data_size);

                              }

                              if (2==strh)

                              {

                                      data_size = sizeof(WaveFormatEx);

                                      if (0==fread (&WaveFormatEx, data_size, 1, fp))

                                      {

                                             break;

                                      }

                                      memcpy(&ai->AudioWaveFormatEx, &WaveFormatEx, data_size);

                              }

 

                              seek_size = ( size!=data_size ) ?  size-data_size : 0;

                       }

 

                       // "00dc" : Compressed video frame

                       if ((id&0xffff0000)==0x63640000)

                       {

                              ai->video_data_size += size;

                              ai->video_data_count++;

                       }

                       // "00db" : Uncompressed video frame

                       if ((id&0xffff0000)==0x62640000)

                       {

                              ai->video_data_size += size;

                              ai->video_data_count++;

                       }

                       // "00pc" : Palette change

                       if ((id&0xffff0000)==0x63700000)

                       {

                              ai->video_data_size += size;

                              ai->video_data_count++;

                       }

                       // "00wb" : Audio data

                       if ((id&0xffff0000)==0x62770000)

                       {

                              ai->audio_data_size += size;

                              ai->audio_data_count++;

                       }

 

                       //

                       if ( 0!=seek_size )

                       {

                              if ( data_size != size )

                              {

                                      printf ("          # data_size(%d) != size(%d)\r\n", data_size, size);

                              }

                              if (0!=(seek_size%2))

                              {

                                      seek_size+=(seek_size%2);

                              }

                              if (-1==fseek (fp, seek_size, SEEK_CUR))

                              {

                                      break;

                              }

                       }

               }

        }

 

        fclose (fp);

 

        return true;

}

 

int _tmain(int argc, _TCHAR* argv[])

{

        avi_info_t avi;

 

        memset (&avi,0,sizeof(avi));

 

        if (true==read_avi_header ("d:\\test.avi", &avi))

        {

               printf ("\r\n");

               printf ("OK\r\n");

 

               double fps;

               double duration;

              

               double VideoFramesPerSec;

               double AudioBlockPerSec ;

 

               int HeaderSize;

               int VideoSize ;

               int AudioSize ;

 

               double VideoBitRate;

               double AudioBitRate;

               double FileBitRate;

 

               fps      = 1000000.0f/(double)avi.Header.MicroSecPerFrame;

               duration = avi.Header.TotalFrames / fps;

 

               AudioBlockPerSec  = (double)avi.Audio.Rate / (double)avi.Audio.Scale;

               VideoFramesPerSec = (double)avi.Video.Rate / (double)avi.Video.Scale;

              

               /*

               HeaderSize = avi.Header.TotalFrames * 8 * (avi.audio_data_count + 1);

               AudioSize  = (int)((avi.Audio.Length * avi.AudioWaveFormatEx.AvgBytesPerSec)/AudioBlockPerSec) * avi.audio_data_count;

               VideoSize  = avi.movi_size - HeaderSize - AudioSize;

               */

               HeaderSize = avi.movi_size - avi.video_data_size - avi.audio_data_size;

               AudioSize  = avi.audio_data_size;

               VideoSize  = avi.video_data_size;

 

               FileBitRate  = avi.avi_size * 8.0 / duration / 1000.0;

               AudioBitRate = avi.AudioWaveFormatEx.AvgBytesPerSec*8.0/1000.0;

               VideoBitRate = FileBitRate - AudioBitRate; // Windows AVI File Property

               VideoBitRate = (VideoSize * VideoFramesPerSec * 8.0)/(avi.Header.TotalFrames*1000.0);

 

               printf ("\r\n");

 

                if (avi.HasVideo)

               {

                       printf ("[비디오] \r\n");

                       printf ("길이          = %5.3f \r\n", duration);

                       printf ("프레임너비   = %d\r\n", avi.Header.Width);

                       printf ("프레임높이   = %d\r\n", avi.Header.Height);

                       printf ("데이터속도   = %5.3f kbps \r\n", VideoBitRate);                    

                       printf ("총비트전송률= %5.3f kbps\r\n", FileBitRate);

                       printf ("프레임속도   = %5.3f 프레임/\r\n", fps);

 

                       printf ("\r\n");

               }

 

               if (avi.HasAudio)

               {

                       printf ("[오디오] \r\n");

                       printf ("비트전송율      = %5.3f kbps \r\n", AudioBitRate);

                       printf ("채널            = %d \r\n", avi.AudioWaveFormatEx.Channels);

                       printf ("오디오샘플속도= %5.3f KHz\r\n", AudioBlockPerSec / 1000);

 

                       printf ("\r\n");

               }

        }

 

        return 0;

}

 

/*

 

RIFF 2317980 AVI

      12: <LIST> 8818

          hdrl

      24: <avih> 56

      88: <LIST> 4244

          strl

     100: <strh> 56

          vids:

     164: <strf> 40

     212: <JUNK> 4120

    4340: <LIST> 4222

          strl

    4352: <strh> 56

          auds:

    4416: <strf> 18

          # data_size(20) != size(18)

    4442: <JUNK> 4120

    8570: <LIST> 260

          odml

    8582: <dmlh> 248

    8838: <LIST> 28

          INFO

    8850: <ISFT> 15

    8874: <JUNK> 1358

   10240: <LIST> 2281204

          movi

   10252: <01wb> 24000

   34260: <00dc> 22306

   56574: <01wb> 1920

   58502: <00dc> 159

   58670: <01wb> 1920

...

 2290948: <00dc> 159

 2291116: <00dc> 159

 2291284: <00dc> 159

 2291452: <idx1> 26528

 2317988: <JUNK> 340

 2318336:

OK

 

[비디오]

길이          = 33.400

프레임너비   = 640

프레임높이   = 480

데이터속도   = 159.297 kbps

총비트전송률= 555.205 kbps

프레임속도   = 25.000 프레임/

 

[오디오]

비트전송율      = 384.000 kbps

채널            = 5

오디오샘플속도= 48.000 KHz

 

*/

Posted by 셈말짓기 :

#. ReBar 컨트롤에 집어 넣을 윈도우 구현

   - TB_BUTTONCOUNT, TB_GETITEMRECT 2개의 메세지 구현해주면 ReBar에 넣을수있다.

 

 


/////////////////////////////////////////////////////////////////////////////
class C_WTLWINVIEW_View : public CWindowImpl<C_WTLWINVIEW_View>
{
public:
 DECLARE_WND_CLASS(NULL)

 BOOL PreTranslateMessage(MSG* pMsg)
 {
  pMsg;
  return FALSE;
 }

 BEGIN_MSG_MAP(C_WTLWINVIEW_View)
  MESSAGE_HANDLER ( WM_PAINT            , OnPaint              )
  MESSAGE_HANDLER ( TB_BUTTONCOUNT      , OnTB_ButtonCount     )
  MESSAGE_HANDLER ( TB_GETITEMRECT      , OnTB_GetItemRect     )
  MESSAGE_HANDLER ( TB_GETEXTENDEDSTYLE , OnTB_GetExtendedStyle)
  MESSAGE_HANDLER ( TB_SETEXTENDEDSTYLE , OnTB_SetExtendedStyle)
 END_MSG_MAP()

 // Handler prototypes (uncomment arguments if needed):
 // LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
 // LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
 // LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

 LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
 {
  CPaintDC dc(m_hWnd);

  //TODO: Add your drawing code here

  return 0;
 }
 LRESULT OnTB_ButtonCount(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
 {
  // return int item count
  // wParam int 0
  // lParam int 0
  return 1;
 }
 LRESULT OnTB_GetItemRect(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
 {
  // return BOOL  success
  // wParam int   button index
  // lParam RECT* button rect

  LPRECT lpRect = (LPRECT)lParam;

  lpRect->left  =0;
  lpRect->top   =0;
  lpRect->right =100;
  lpRect->bottom=20;

  return 1;
 }
 LRESULT OnTB_GetExtendedStyle(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
 {
  // return DWORD style
  // wParam int   0
  // lParam int   0
  return 0;
 }
 LRESULT OnTB_SetExtendedStyle(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
 {
  // return DWORD style
  // wParam int   0
  // lParam int   new style
  return 0;
 }
};

WTL::CFrameWindowImplBase::AddSimpleReBarBandCtrl()

 

/////////////////////////////////////////////////////////////////////////////

//

// Class: CMyToolWindow

//

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

class CMyToolWindow : public CWindowImpl<CMyToolWindow>

{

public:

        //DECLARE_WND_CLASS(NULL)

        DECLARE_WND_CLASS_EX (NULL, CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS, (HBRUSH)(COLOR_APPWORKSPACE))

 

        BOOL PreTranslateMessage(MSG* pMsg)

        {

               pMsg;

               return FALSE;

        }

 

        BEGIN_MSG_MAP(CMyToolWindow)

               MESSAGE_HANDLER(TB_BUTTONCOUNT, OnToolBarButtonCount)

               MESSAGE_HANDLER(TB_GETITEMRECT, OnToolBarItemRect   )

               MESSAGE_HANDLER(WM_PAINT, OnPaint)

        END_MSG_MAP()

 

        LRESULT OnToolBarButtonCount(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)

        {

               return 1;

        }

 

        LRESULT OnToolBarItemRect   (UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)

        {

               int    nButtonCount = (int)   wParam;

               LPRECT pRect        = (LPRECT)lParam;

 

               pRect->left   =0;

               pRect->top    =0;

 

               if (nButtonCount>0)

                       pRect->right  =100*nButtonCount;

               else

                       pRect->right  =100;

               pRect->bottom =25;

 

               return 1;

        }

 

        LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)

        {

               CPaintDC dc(m_hWnd);

 

               //TODO: Add your drawing code here

               dc.TextOut (0,0,_T("xxx"));

               return 0;

        }

};

 

 

 

#. MainFrame 윈도우에서

 

CMyToolWindow m_MyToolWindow;

 

LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)

{

        HWND hWndCmdBar;

        HWND hWndToolBar;

 

        hWndCmdBar = m_CmdBar.Create(m_hWnd, rcDefault, NULL, ATL_SIMPLE_CMDBAR_PANE_STYLE);

        m_CmdBar.AttachMenu(GetMenu());

        m_CmdBar.LoadImages(IDR_MAINFRAME);

        SetMenu(NULL);

 

        hWndToolBar = CreateSimpleToolBarCtrl(m_hWnd, IDR_MAINFRAME, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE);

 

        m_MyToolWindow.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, WS_EX_STATICEDGE);

 

        // Rebar

        CreateSimpleReBar(ATL_SIMPLE_REBAR_NOBORDER_STYLE);

        AddSimpleReBarBand(hWndCmdBar);

        AddSimpleReBarBand(hWndToolBar, NULL, TRUE);

        AddSimpleReBarBand(m_MyToolWindow, _T("MyToolWindow"), TRUE, 100, TRUE);

 

        // StatusBar

        CreateSimpleStatusBar();

 

        // View

        m_hWndClient = m_view.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, WS_EX_CLIENTEDGE);

 

        UIAddToolBar(hWndToolBar);

        UISetCheck(ID_VIEW_TOOLBAR, 1);

        UISetCheck(ID_VIEW_STATUS_BAR, 1);

 

        // register object for message filtering and idle updates

        CMessageLoop* pLoop = _Module.GetMessageLoop();

        ATLASSERT(pLoop != NULL);

        pLoop->AddMessageFilter(this);

        pLoop->AddIdleHandler(this);

 

        return 0;

}

 

Posted by 셈말짓기 :

TCHAR      text[128];

SYSTEMTIME st;

int        result = 0;

 

GetLocalTime (&st);

 

result = GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, NULL, text, 128);

if (result)

{

        // printf (text);

}

 

result = GetTimeFormat(LOCALE_USER_DEFAULT, /*LOCALE_NOUSEROVERRIDE*/0, &st, _T("HH:mm:ss"), text, 128);

if (result)

{

        // printf (text);

}

 

Posted by 셈말짓기 :

# link 기본
#     g++ -o hellox -Wall -D_REENTRANT         \
#         /usr/lib/i386-linux-gnu/libpthread.a \
#         /usr/X11R6/lib/libX11.so             \
#         ./libprint.so                        \
#         ./hellox.o                          
# g++
#     -l라이브러리파일명중lib제외한부분[libXXXX.a]
#         [예] -lpthread -> libpthread.a
#     -L라이브러리파일libXXXX.a찾을경로
#         [예] $(CXX) -o hellox -Wall -D_REENTRANT -lpthread /usr/X11R6/lib/libX11.so hellox.o
#              $(CXX) -o hellox -Wall -D_REENTRANT -lpthread -lX11 hellox.o -L/usr/X11R6/lib
#     -Wl,ld명령파라메터
#         ld명령에 파라메터 전송 구분자, 구분자,는 공백문자로 변경됨
#         -Wl,이후 공백문자를 사용하지말것

LIB_PTHREAD     = /usr/lib/i386-linux-gnu/libpthread.a

INC_X11_DESKTOP = /usr/X11R6/include
LIB_X11_DESKTOP = /usr/X11R6/lib/libX11.so
LIB_X11_DEUTA   = /opt/mft1-gcc/target/extended/xlib6g/usr/X11R6/lib/libX11.so.6

ifeq '$(MAKECMDGOALS)' ''
prepared_error:
 @echo "No target!"
endif

ifeq '$(MAKECMDGOALS)' 'all'
INC_X11 = $(INC_X11_DESKTOP)
LIB_X11 = $(LIB_X11_DESKTOP)
endif

compile:
 @echo ""
 @echo "============================================================================="
 @echo "compile"
 @echo "============================================================================="
 $(CXX) -o hellox.o    -ansi -pedantic -Wall -I$(INC_X11) -c  hellox.c
 $(CXX) -o libprint.so -ansi -pedantic -Wall -fPIC -shared    libprint.c -Wl,-Map,libprint.map

link:
 @echo ""
 @echo "============================================================================="
 @echo "link"
 @echo "============================================================================="
 $(CXX) -o hellox      -Wall -D_REENTRANT $(LIB_PTHREAD) $(LIB_X11) libprint.so hellox.o -Wl,--rpath,.,-Map,hellox.map

clean:
 @echo ""
 @echo "============================================================================="
 @echo "clean"
 @echo "============================================================================="
 rm -f *.o
 rm -f *.so
 rm -f *.map
 rm -f hellox
 
all: clean compile link

execute:
 @echo ""
 @echo "============================================================================="
 @echo "execute"
 @echo "============================================================================="
 ./hellox

show_make:
 @echo ""
 @echo "============================================================================="
 @echo "show make variables"
 @echo "============================================================================="
 @echo "MAKEFILES    = $(MAKEFILES)"
 @echo "VPATH        = $(VPATH)"
 @echo "SHELL        = $(SHELL)"
 @echo "MAKESHELL    = $(MAKESHELL)"
 @echo "MAKE         = $(MAKE)"
 @echo "MAKELEVEL    = $(MAKELEVEL)"
 @echo "MAKEFLAGS    = $(MAKEFLAGS)"
 @echo "MAKECMDGOALS = $(MAKECMDGOALS)"
 @echo "CURDIR       = $(CURDIR)"
 @echo "SUFFIXES     = $(SUFFIXES)"

show_rpath:
 @echo ""
 @echo "============================================================================="
 @echo "show rpath"
 @echo "============================================================================="
 objdump -x hellox |grep RPATH
# readelf -d hellox |head -20

Posted by 셈말짓기 :


/////////////////////////////////////////////////////////////////////////////

//

// File: FSMSample.cpp

//

// Created by MOON, Eui-kwon.

// Created on Aug-10th, 2011.

//

// 교육용으로작성한FSM Sample.

// 예전에FSM 공부할때cdplayer라는샘플코드를본것같은데코드는없고

// 웹에상태천이표만있어서그기준으로코드를작성함.

//

// machine, state, event 각각의인터페이스는비지터패턴형식으로구성함.

//

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

 

 

 

#include "stdafx.h"

#include <assert.h>

 

/////////////////////////////////////////////////////////////////////////////

//

// Finite state machine

//

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

namespace fsm

{

 

 

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

class event;

class state;

class machine;

 

 

 

/////////////////////////////////////////////////////////////////////////////

//

// Interface

//

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

class machine

{

private:

        state* _state;

       

public:

        machine();

        virtual ~machine();

 

public:

        virtual void   transition (state*);

        virtual void   notify     (event*);

        virtual state* get_state  (void  ) const;

};

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

class event

{

public:

        event();

        virtual ~event();

 

public:

        virtual void notify (machine*, state*);

};

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

class state

{

public:

        state();

        virtual ~state();

       

public:

        virtual void on_entry   (machine*, state*);

        virtual void on_exit    (machine*, state*);

        virtual void on_event   (machine*, event*);

 

public:

        virtual void transition (machine*, state*);

};

 

 

 

 

/////////////////////////////////////////////////////////////////////////////

//

// Implementation

//

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

event:: event(){}

event::~event(){}

 

void event::notify (machine* m, state*s)

{

        if (s)

        {

               s->on_event (m, this);

        }

}

 

//===========================================================================

state:: state(){}

state::~state(){}

 

void state::on_entry (machine*, state*) {}

void state::on_exit  (machine*, state*) {}

void state::on_event (machine*, event*) {}

 

void state::transition (machine* m, state* s)

{

        if (m)

        {

               m->transition (s);

        }

}

 

//===========================================================================

machine::machine():

        _state (0)

{

}

 

machine::~machine()

{

}

 

state* machine::get_state (void) const

{

        return _state;

}

 

void machine::transition (state* current)

{      

        state* previous;

 

        previous = _state;

        if (previous)

        {

               previous->on_exit (this, current );

        }

 

        _state = current;

        if (current)

        {

               current ->on_entry (this, previous);

        }

}

 

void machine::notify (event* e)

{

        if (e)

        {

               e->notify (this, _state);

        }

}

 

}; // end of "namespace fsm"

 

 

 

/////////////////////////////////////////////////////////////////////////////

//

// Sample

//

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

#define fsm_trace  printf

#define fsm_assert assert

 

//===========================================================================

enum cdplayer_event_code_t

{

        ec_play        = 0,

        ec_open_close  = 1,

        ec_cd_detected = 2,

        ec_stop        = 3,

        ec_pause       = 4

};

 

//===========================================================================

class cdplayer_event : public fsm::event

{

public:

        const cdplayer_event_code_t _code;

 

        explicit cdplayer_event (cdplayer_event_code_t code) : _code(code)

        {

        }

 

        void notify (fsm::machine* m, fsm::state*s)

        {

               switch (_code)

               {

               case ec_play       : fsm_trace("\t\tnotify: play        \r\n"); break;

               case ec_open_close : fsm_trace("\t\tnotify: open_close  \r\n"); break;

               case ec_cd_detected: fsm_trace("\t\tnotify: cd_detected \r\n"); break;

               case ec_stop       : fsm_trace("\t\tnotify: stop        \r\n"); break;

               case ec_pause      : fsm_trace("\t\tnotify: pause       \r\n"); break;

               }

 

               fsm::event::notify (m, s);

        }

};

 

//===========================================================================

class cdplayer_state : public fsm::state

{

public:

        virtual const char* get_name (void) = 0;

 

        virtual void on_event (fsm::machine* m, fsm::event* e)

        {

               /*

               cdplayer*       cm = static_cast<cdplayer*      > ( m );

               cdplayer_event* ce = static_cast<cdplayer_event*> ( e );

 

               switch (ce->_code)

               {

               case ec_play       : break;

               case ec_open_close : break;

               case ec_cd_detected: break;

               case ec_stop       : break;

               case ec_pause      : break;

               }

               */

        }

 

        virtual void on_entry (fsm::machine*, fsm::state*)

        {

               fsm_trace("\ton_entry: %s \r\n", get_name());

        }

 

        virtual void on_exit (fsm::machine*, fsm::state*)

        {

               fsm_trace("\ton_exit : %s \r\n", get_name());

        }

};

#define DECLARE_NAME(n) virtual const char* get_name (void) { return (n); }

 

//===========================================================================

class stopped: public cdplayer_state

{

public:

        DECLARE_NAME ("stopped" )

        virtual void on_event (fsm::machine* m, fsm::event* e);

};

 

//===========================================================================

class open: public cdplayer_state

{

public:

        DECLARE_NAME ("open" )

        virtual void on_event (fsm::machine* m, fsm::event* e);

};

 

//===========================================================================

class empty: public cdplayer_state

{

public:

        DECLARE_NAME ("empty" )

        virtual void on_event (fsm::machine* m, fsm::event* e);

};

 

//===========================================================================

class playing: public cdplayer_state

{

public:

        DECLARE_NAME ("playing" )

        virtual void on_event (fsm::machine* m, fsm::event* e);

};

 

//===========================================================================

class paused: public cdplayer_state

{

public:

        DECLARE_NAME ("paused" )

        virtual void on_event (fsm::machine* m, fsm::event* e);

};

 

//===========================================================================

class cdplayer: public fsm::machine

{

private:

        stopped _stopped;

        open    _open   ;

        empty   _empty  ;

        playing _playing;

        paused  _paused ;

 

public:

        enum state_id_t

        {

               sid_stopped,

               sid_open   ,

               sid_empty  ,

               sid_playing,

               sid_paused ,

        };

 

public:

        virtual void transition (fsm::state* n)

        {

               cdplayer_state* previous = static_cast<cdplayer_state*> ( get_state() );

               cdplayer_state* current  = static_cast<cdplayer_state*> ( n );

 

               fsm_trace ("transition:");

               if (previous) fsm_trace (" %s -> ", previous->get_name());

               if (current ) fsm_trace (" %s ", current ->get_name());

               fsm_trace ("\r\n");

 

               fsm::machine::transition(n);

        }

 

        cdplayer_state* get_state_instance (state_id_t id)

        {

               switch (id)

               {

               case sid_stopped : return &_stopped ;

               case sid_open    : return &_open    ;

               case sid_empty   : return &_empty   ;

               case sid_playing : return &_playing ;

               case sid_paused  : return &_paused  ;

               }

 

               fsm_assert (0);

 

               return 0;

        }

 

public:

        void start_playback         (void){ printf("\t\t\taction: start_playback         \r\n"); }

        void open_drawer            (void){ printf("\t\t\taction: open_drawer            \r\n"); }

        void close_drawer           (void){ printf("\t\t\taction: close_drawer           \r\n"); }

        void collect_cd_information (void){ printf("\t\t\taction: collect_cd_information \r\n"); }

        void store_cd_information   (void){ printf("\t\t\taction: store_cd_information   \r\n"); }

        void stop_playback          (void){ printf("\t\t\taction: stop_playback          \r\n"); }

        void pause_playback         (void){ printf("\t\t\taction: pause_playback         \r\n"); }

        void resume_playback        (void){ printf("\t\t\taction: resume_playback        \r\n"); }

};

 

/*********************************************************************************

 CD PLAYER STATE TRANSITION TABLE

---------------+-------------+------------+--------------------------------------

 CURRENT STATE | EVENT       | NEXT STATE | TRANSITION ACTION

---------------+-------------+------------+--------------------------------------

 stopped       | play        | playing    | start playback

 stopped       | open/close  | open       | open drawer

 open          | open/close  | empty      | close drawer; collect cd information

 empty         | open/close  | open       | open drawer

 empty         | cd-detected | stopped    | store cd information

 playing       | stop        | stopped    | stop playback

 playing       | pause       | paused     | pause playback

 playing       | open/close  | open       | stop playback; open drawer

 paused        | play        | playing    | resume playback

 paused        | stop        | stopped    | stop playback

 paused        | open/close  | open       | stop playback; open drawer

---------------+-------------+------------+--------------------------------------

*********************************************************************************/

 

//===========================================================================

void stopped::on_event (fsm::machine* m, fsm::event* e)

{

        cdplayer*       cm = static_cast<cdplayer*      > ( m );

        cdplayer_event* ce = static_cast<cdplayer_event*> ( e );

 

        switch (ce->_code)

        {

        case ec_play :

               cm->start_playback();

               transition(cm, cm->get_state_instance (cdplayer::sid_playing));

               break;

 

        case ec_open_close :

               cm->open_drawer ();

               transition(cm, cm->get_state_instance (cdplayer::sid_open));

               break;

        }

}

 

//===========================================================================

void open::on_event (fsm::machine* m, fsm::event* e)

{

        cdplayer*       cm = static_cast<cdplayer*      > ( m );

        cdplayer_event* ce = static_cast<cdplayer_event*> ( e );

 

        switch (ce->_code)

        {

        case ec_open_close :

               cm->close_drawer();

               cm->collect_cd_information();

               transition(cm, cm->get_state_instance (cdplayer::sid_empty));

               break;

        }

}

 

//===========================================================================

void empty::on_event (fsm::machine* m, fsm::event* e)

{

        cdplayer*       cm = static_cast<cdplayer*      > ( m );

        cdplayer_event* ce = static_cast<cdplayer_event*> ( e );

 

        switch (ce->_code)

        {

        case ec_open_close :

               cm->open_drawer();

               transition(cm, cm->get_state_instance (cdplayer::sid_open));

               break;

 

        case ec_cd_detected:

               cm->store_cd_information();

               transition(cm, cm->get_state_instance (cdplayer::sid_stopped));

               break;

        }

}

 

//===========================================================================

void playing::on_event (fsm::machine* m, fsm::event* e)

{

        cdplayer*       cm = static_cast<cdplayer*      > ( m );

        cdplayer_event* ce = static_cast<cdplayer_event*> ( e );

 

        switch (ce->_code)

        {

        case ec_stop :

               cm->stop_playback();

               transition(cm, cm->get_state_instance (cdplayer::sid_stopped));

               break;

 

        case ec_pause :

               cm->pause_playback();

               transition(cm, cm->get_state_instance (cdplayer::sid_paused));

               break;

 

        case ec_open_close :

               cm->stop_playback();

               cm->open_drawer();

               transition(cm, cm->get_state_instance (cdplayer::sid_open));

               break;

        }

}

//===========================================================================

void paused::on_event (fsm::machine* m, fsm::event* e)

{

        cdplayer*       cm = static_cast<cdplayer*      > ( m );

        cdplayer_event* ce = static_cast<cdplayer_event*> ( e );

 

        switch (ce->_code)

        {

        case ec_play :

               cm->resume_playback();

               transition(cm, cm->get_state_instance (cdplayer::sid_playing));

               break;

 

        case ec_stop :

               cm->stop_playback();

               transition(cm, cm->get_state_instance (cdplayer::sid_stopped));

               break;

 

        case ec_open_close :

               cm->stop_playback();

               cm->open_drawer();

               transition(cm, cm->get_state_instance (cdplayer::sid_open));

               break;

        }

}

//===========================================================================

int _tmain(int argc, _TCHAR* argv[])

{

        cdplayer       player;

        cdplayer_event play        (ec_play       );

        cdplayer_event open_close  (ec_open_close );

        cdplayer_event cd_detected (ec_cd_detected);

        cdplayer_event stop        (ec_stop       );

        cdplayer_event pause       (ec_pause      );

 

        player.transition ( player.get_state_instance(cdplayer::sid_empty) );

 

        player.notify ( &cd_detected );

        player.notify ( &play        );

        player.notify ( &pause       );

        player.notify ( &stop        );

        player.notify ( &open_close  );

        player.notify ( &stop        );

        player.notify ( &play        );

        player.notify ( &open_close  );

 

        player.notify ( &open_close  );

        player.notify ( &cd_detected );

        player.notify ( &play        );

 

        return 0;

}

 

/////////////////////////////////////////////////////////////////////////////

//

// result

//

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

/*

transition: empty

        on_entry: empty

                notify: cd_detected

                        action: store_cd_information

transition: empty ->  stopped

        on_exit : empty

        on_entry: stopped

                notify: play

                        action: start_playback

transition: stopped ->  playing

        on_exit : stopped

        on_entry: playing

                notify: pause

                        action: pause_playback

transition: playing ->  paused

        on_exit : playing

        on_entry: paused

                notify: stop

                        action: stop_playback

transition: paused ->  stopped

        on_exit : paused

        on_entry: stopped

                notify: open_close

                        action: open_drawer

transition: stopped ->  open

        on_exit : stopped

        on_entry: open

                notify: stop

                notify: play

                notify: open_close

                        action: close_drawer

                        action: collect_cd_information

transition: open ->  empty

        on_exit : open

        on_entry: empty

                notify: open_close

                        action: open_drawer

transition: empty ->  open

        on_exit : empty

        on_entry: open

                notify: cd_detected

                notify: play

계속하려면아무키나누르십시오. . .

*/

 

 

Posted by 셈말짓기 :