home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / biff / bigg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-16  |  11.8 KB  |  400 lines

  1. /*
  2.    This module was designed to work as a standalone
  3.    program.  What it does is promts you for BIFF
  4.    record information and then builds a biff buffer
  5.    that is copied to the Clipboard and also creates
  6.    a BIFF.XLS file.
  7. */
  8.  
  9. #include "windows.h"
  10. #include "winexp.h"  /* needed for file i/o functions */
  11. #include "stdlib.h"  /* needed for atof function */
  12. #include "bigg.h"
  13.  
  14. #define BUFLENGTH  100
  15.  
  16. extern  HWND  hWnd;
  17.  
  18. int     iRow;               /*         INT value from edit control */
  19. int     iColumn;            /*         INT value from edit control */
  20. WORD    wInt;               /* BOOL or INT value from edit control */
  21. int     iTextLength;        /*    length of text from edit control */
  22. char    szText[40];         /*              text from edit control */
  23. char    cBuffer[BUFLENGTH];
  24. int     iLastButton;
  25. int     iRadioButton;
  26. int     PlaceBiffOnClipboard( HANDLE );
  27. int     iLength;
  28. LPSTR   lpData, lpDataInit;
  29. FARPROC lpprocBiffDlgProc;
  30.  
  31. struct BIFFINTEGER FAR *lpBiffInt;
  32. struct BIFFNUMBER  FAR *lpBiffNumber;
  33. struct BIFFSTRING  FAR *lpBiffString;
  34. struct BIFFFORMULA FAR *lpBiffFormula;
  35.  
  36. /* ------ formal declarations -------- */
  37.  
  38. BOOL FAR PASCAL BiffDlgProc( HWND, WORD, WORD, LONG );
  39. HANDLE FillBiffBuffer( void );
  40. void   CreateBiffBOF(void);
  41. void   CreateBiffDIM( int, int, int, int );
  42. void   SaveBiffData(  int, int, int);
  43. void   CloseBiff(void);
  44. int    DumpStuff(void);
  45. void   lstrncpy(LPSTR, LPSTR, int);
  46.  
  47. /* -----------------------------------------
  48.      Entry point of this module.
  49.  ------------------------------------------- */
  50. void FAR GetBiff(HWND hWnd, HANDLE hInst)
  51. {
  52.    lpprocBiffDlgProc = MakeProcInstance( (FARPROC)BiffDlgProc, hInst );
  53.    DialogBox( hInst, MAKEINTRESOURCE(BIFFDLG), hWnd, lpprocBiffDlgProc );
  54.    FreeProcInstance(lpprocBiffDlgProc);
  55. }
  56.  
  57. /* -----------------------------------------
  58.      Process Biff dialog box.
  59.  ------------------------------------------- */
  60. BOOL FAR PASCAL BiffDlgProc( HWND hDlg, WORD wMessage, WORD wParam, LONG lParam)
  61. {
  62.    BOOL bResult;  /* Translated flag */ 
  63.  
  64.    switch (wMessage) {
  65.  
  66.    case WM_INITDIALOG:                  
  67.  
  68.        SetDlgItemText(hDlg, IDTEXT, (LPSTR)szText);
  69.  
  70.        CheckRadioButton(hDlg, IDBOOL, IDFORMULA, iLastButton);
  71.            iRadioButton = iLastButton;
  72.  
  73.        /* formula is not implemented so it is disabled */
  74.        EnableWindow( GetDlgItem(hDlg,IDFORMULA), FALSE );
  75.  
  76.        break;
  77.  
  78.    case WM_COMMAND:
  79.  
  80.        switch (wParam) {
  81.  
  82.        case IDOK:
  83.  
  84.            iRow = GetDlgItemInt(hDlg, IDROW , (BOOL FAR *)&bResult, FALSE);
  85.            if (bResult == FALSE) /* display an error. */
  86.                MessageBox( hDlg,
  87.                            (LPSTR)"Non-numeric character or exceeded max!",
  88.                            (LPSTR)"Row Error!", MB_OK );
  89.            if(iRow) iRow--;  // make ZERO based
  90.  
  91.            iColumn = GetDlgItemInt(hDlg, IDCOLUMN, (BOOL FAR *)&bResult, FALSE);
  92.            if (bResult == FALSE) /* display an error. */
  93.                MessageBox( hDlg,
  94.                            (LPSTR)"Non-numeric character or exceeded max!",
  95.                            (LPSTR)"Column Error!", MB_OK );
  96.  
  97.            if(iColumn)  iColumn--; // make ZERO based
  98.  
  99.            iLastButton = iRadioButton;
  100.            switch (iRadioButton) {
  101.               case IDBOOL:
  102.                    wInt = GetDlgItemInt(hDlg, IDTEXT, (BOOL FAR *)&bResult, FALSE);
  103.                    if (bResult == FALSE) /* display an error. */
  104.                        MessageBox( hDlg,
  105.                               (LPSTR)"Something is wrong with BOOL value!",
  106.                               (LPSTR)"BOOL Error!", MB_OK );
  107.                    break;
  108.  
  109.               case IDINT:
  110.                    wInt = GetDlgItemInt(hDlg, IDTEXT, (BOOL FAR *)&bResult, FALSE);
  111.                    if (bResult == FALSE) /* display an error. */
  112.                    MessageBox( hDlg,
  113.                               (LPSTR)"Must be 0 - 65535!",
  114.                               (LPSTR)"Integer Error!", MB_OK );
  115.                    break;
  116.  
  117.               case IDBLANK:
  118.               case IDLABEL:
  119.               case IDNUMBER:
  120.               case IDFORMULA:
  121.  
  122.                    break;
  123.  
  124.               default:
  125.                      MessageBox( hDlg,"Wrong data type", NULL, MB_OK);
  126.            }
  127.            iTextLength = GetDlgItemText(hDlg, IDTEXT, (LPSTR)szText, 40);
  128.            szText[iTextLength+1] = '\0';
  129.  
  130.            PlaceBiffOnClipboard( FillBiffBuffer() );
  131.  
  132.            EndDialog(hDlg, TRUE);
  133.            return(TRUE);
  134.  
  135.        case IDCANCEL:
  136.                EndDialog(hDlg, FALSE);
  137.                return(TRUE);
  138.  
  139.        case IDBLANK:
  140.        case IDLABEL:
  141.        case IDBOOL:
  142.        case IDINT:
  143.        case IDNUMBER:
  144.        case IDFORMULA:
  145.                CheckRadioButton(hDlg, IDBLANK, IDFORMULA,
  146.                   (iRadioButton = wParam));
  147.                return(TRUE);
  148.                break;
  149.        }
  150.    default:
  151.            return(FALSE);
  152.    }
  153.    return(TRUE);
  154. }
  155.  
  156.  
  157. /* ----------------------------------------------------------
  158.    Place the biff buffer to the clipboard in BIFF format
  159.  ------------------------------------------------------------ */
  160. int  PlaceBiffOnClipboard(HANDLE hBiffBuffer)
  161. {
  162.   WORD     wCBFormat;
  163.   int      iResult;
  164.   unsigned long  ulLength;
  165.  
  166.   if (hBiffBuffer == 0)
  167.     return FALSE;
  168.  
  169.   ulLength = (unsigned long)iLength;
  170.   hBiffBuffer = GlobalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, ulLength );
  171.   if (!hBiffBuffer) {
  172.      MessageBox(GetFocus(), "Not enough memory.", NULL, MB_OK);
  173.      return FALSE;
  174.   }
  175.   if ( lpData = GlobalLock(hBiffBuffer) ) {
  176.     lstrncpy( lpData ,lpDataInit, iLength );
  177.   }
  178.   GlobalUnlock(hBiffBuffer);
  179.  
  180.   if ( OpenClipboard(hWnd) == 0) {
  181.     MessageBox( GetFocus(), (LPSTR)"Opening clipboard", NULL, MB_OK);
  182.     return FALSE;
  183.   }
  184.   EmptyClipboard();
  185.   wCBFormat = RegisterClipboardFormat ( (LPSTR)"Biff" );
  186.   iResult   = SetClipboardData(wCBFormat,hBiffBuffer);
  187.   CloseClipboard();
  188.   return TRUE;
  189.  
  190. }
  191.  
  192. /* ----------------------
  193.    Fill the Biff buffer
  194.  ------------------------ */
  195. HANDLE FillBiffBuffer()
  196. {
  197.  
  198.   lpDataInit = lpData = cBuffer;  /* set starting address to fill */
  199.   CreateBiffBOF();
  200.   CreateBiffDIM(iRow, iRow, iColumn, iColumn);
  201.   SaveBiffData(iRadioButton, iRow, iColumn);
  202.   CloseBiff();
  203.  
  204.   DumpStuff();
  205.   return TRUE;
  206.  
  207. }
  208.  
  209.  
  210. /* --------------------------------------------------
  211.    Creates a BOF record,
  212.  ---------------------------------------------------- */
  213. void CreateBiffBOF()
  214. {
  215.  
  216.    *lpData++ = 0x9 ; *lpData++ = 0x0 ; // rec type
  217.    *lpData++ = 0x4 ; *lpData++ = 0x0 ; // rec length
  218.  
  219.    *lpData++ = 0x2 ; *lpData++ = 0x0 ; // version #
  220.    *lpData++ = 0x10; *lpData++ = 0x0 ; // 10 = XLS, 20 = XLC, 40 = XLM
  221.  
  222. }
  223.  
  224. /* -----------------------------------------------------------
  225.    Creates a Dimension record,
  226.    Currently, this dialog does not:
  227.       1] ask for more than ONE cell.
  228.       2] limits range to two characters.
  229.  ------------------------------------------------------------- */
  230. void CreateBiffDIM(int iFirstRow, int iLastRow, int iFirstCol, int iLastCol)
  231. {
  232.         
  233.    *lpData++ = 0x0;  *lpData++ = 0x0; // rec type
  234.    *lpData++ = 0x8;  *lpData++ = 0x0; // rec length
  235.  
  236.    *lpData++ = LOBYTE(iFirstRow);  *lpData++ = 0x0; // HIBYTE(iFirstRow); 
  237.    *lpData++ = LOBYTE(iLastRow+1); *lpData++ = 0x0; // HIBYTE(iLastRow+1);
  238.    *lpData++ = LOBYTE(iFirstCol);  *lpData++ = 0x0; // HIBYTE(iFirstCol); 
  239.    *lpData++ = LOBYTE(iLastCol+1); *lpData++ = 0x0; // HIBYTE(iLastCol+1);
  240.  
  241. }
  242.         
  243. /* --------------------------------------------------
  244.    Creates a Data record,
  245.  ---------------------------------------------------- */
  246. void SaveBiffData(int iType, int iRow, int iCol)
  247. {
  248.  
  249.   switch (iType) {
  250.  
  251.    case IDBLANK:   /* make a blank record */
  252.  
  253.          *lpData++ = 0x1;  *lpData++ = 0x0;// rec type
  254.          *lpData++ = 0x7;  *lpData++ = 0x0;// rec length
  255.  
  256.          *lpData++ = LOBYTE(iRow); *lpData++ = 0x0; // HIBYTE(iRow); // row
  257.          *lpData++ = LOBYTE(iCol); *lpData++ = 0x0; // HIBYTE(iCol); // col
  258.  
  259.          *lpData++ = 0x0;  // rec attribute
  260.          *lpData++ = 0x0;
  261.          *lpData++ = 0x0;
  262.  
  263.          break;
  264.  
  265.  
  266.    case IDBOOL:    /* make a boolean record */
  267.  
  268.          *lpData++ = 0x5;  *lpData++ = 0x0;// rec type
  269.          *lpData++ = 0x9;  *lpData++ = 0x0;// rec length
  270.  
  271.          *lpData++ = LOBYTE(iRow); *lpData++ = 0x0; // HIBYTE(iRow); // row
  272.          *lpData++ = LOBYTE(iCol); *lpData++ = 0x0; // HIBYTE(iCol); // col
  273.  
  274.          *lpData++ = 0x0;  // rec attribute
  275.          *lpData++ = 0x0;
  276.          *lpData++ = 0x0;
  277.          *lpData++ = (BYTE)(wInt?0x1:0x0); // data
  278.          *lpData++ = 0x0;
  279.  
  280.          break;
  281.  
  282.    case IDINT:     /* make an integer record */
  283.          lpBiffInt = lpData;
  284.  
  285.          lpBiffInt->wRecType   = 2;
  286.          lpBiffInt->wRecLength = 9;
  287.          lpBiffInt->wRow       = iRow;
  288.          lpBiffInt->wColumn    = iCol;
  289.          lpBiffInt->cAttrib[0] = 0;
  290.          lpBiffInt->cAttrib[1] = 0;
  291.          lpBiffInt->cAttrib[2] = 0;
  292.          lpBiffInt->wInteger   = wInt;
  293.  
  294.          lpData = lpData + lpBiffInt->wRecLength + 4;
  295.  
  296.          break;
  297.  
  298.    case IDNUMBER:  /* make a number record */
  299.  
  300.          lpBiffNumber = lpData;
  301.  
  302.          lpBiffNumber->wRecType   =  3;
  303.          lpBiffNumber->wRecLength = 15;
  304.          lpBiffNumber->wRow       = iRow;
  305.          lpBiffNumber->wColumn    = iCol;
  306.          lpBiffNumber->cAttrib[0] = 0;
  307.          lpBiffNumber->cAttrib[1] = 0;
  308.          lpBiffNumber->cAttrib[2] = 0;
  309.          lpBiffNumber->dNumber    = atof((PSTR)szText);
  310.  
  311.          lpData = lpData + lpBiffNumber->wRecLength + 4;
  312.  
  313.          break;
  314.  
  315.    case IDLABEL:   /* make a label (text) record */
  316.  
  317.          lpBiffString = lpData;
  318.  
  319.          lpBiffString->wRecType    = 4;
  320.          lpBiffString->wRecLength  = iTextLength+1 + 8;
  321.          lpBiffString->wRow        = iRow;
  322.          lpBiffString->wColumn     = iCol;
  323.          lpBiffString->cAttrib[0]  = 0;
  324.          lpBiffString->cAttrib[1]  = 0;
  325.          lpBiffString->cAttrib[2]  = 0;
  326.          lpBiffString->bTextLength = (BYTE)LOWORD(iTextLength);
  327.          lstrncpy( lpBiffString->cLabel, szText, iTextLength+1 );
  328.  
  329.          lpData = lpData + lpBiffString->wRecLength + 4 ;
  330.  
  331.          break;
  332.  
  333.    case IDFORMULA:  /* make a formula record */ // put here for future use
  334.  
  335.          lpBiffFormula = lpData;
  336.  
  337.          lpBiffFormula->wRecType     = 32;
  338.          lpBiffFormula->wRecLength   = iTextLength+1 + 8;
  339.          lpBiffFormula->wRow         = iRow;
  340.          lpBiffFormula->wColumn      = iCol;
  341.          lpBiffFormula->cAttrib[0]   = 0x0;
  342.          lpBiffFormula->cCurValue[0] = 0x0;
  343.          lpBiffFormula->bRecalc      = 0;
  344.          lpBiffFormula->bTextLength  = (BYTE)LOWORD(iTextLength);
  345.          lstrncpy( lpBiffFormula->cLabel, szText, iTextLength+1 );
  346.  
  347.          lpData = lpData + lpBiffFormula->wRecLength + 4 ;
  348.  
  349.          break;
  350.  
  351.    } /* end switch */
  352.  
  353. }
  354.  
  355. /* ---------------------
  356.    Create an EOF record.
  357.  ----------------------- */
  358. void CloseBiff()
  359. {
  360.    *lpData++ = 0xa;  // rec type
  361.    *lpData++ = 0x0;
  362.    *lpData++ = 0x0;
  363.    *lpData++ = 0x0;
  364.  
  365. }
  366.  
  367.  
  368. /* --------------------------------------------------
  369.    Dump buffer to a file.
  370.  ---------------------------------------------------- */
  371. int DumpStuff()
  372. {
  373.    OFSTRUCT OFFile;
  374.    int      iFile;
  375.  
  376.    iFile = OpenFile((LPSTR)"dump.xls",
  377.                     (LPOFSTRUCT)&OFFile,OF_CREATE|OF_WRITE);
  378.    if (iFile == -1) {
  379.      MessageBox (GetFocus(),(LPSTR)"OpenFile failed.", NULL, MB_OK);
  380.      return(FALSE);
  381.    }
  382.    iLength = lpData-lpDataInit;
  383.    if (_lwrite(iFile, lpDataInit, iLength) != iLength ) {
  384.      MessageBox (GetFocus(),(LPSTR)"write",(LPSTR)"failed",MB_OK);
  385.    }
  386.    _lclose(iFile);
  387.  
  388. }
  389.  
  390. /* ---------------------
  391.    Copy number of bytes.
  392.  ----------------------- */
  393.  
  394. void lstrncpy(LPSTR lpDest, LPSTR lpSrc, int n)
  395. {
  396.     while (n--)
  397.       *lpDest++ = *lpSrc++;
  398. }
  399.  
  400.