| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673 |
- /*
- ** Command & Conquer Generals Zero Hour(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- //
- // XLStuff.cpp
- //
- //
- #include "stdAfx.h"
- #include "Babylon.h"
- #include "resource.h"
- #include <stdio.h>
- #include "xlstuff.h"
- #include <assert.h>
- #include <comdef.h>
- static const int xlWorkbookNormal = -4143;
- static const int xlNoChange = 1;
- static const int xlLocalSessionChanges = 2;
- static const int xlWBATWorksheet = -4167;
- static _Workbook *workbook = NULL;
- static _Application *xl = NULL;
- static Workbooks *wbs = NULL;
- static Range *range = NULL;
- static _Worksheet *ws = NULL;
- static OLECHAR buffer[100*1024];
- static VARIANT no, yes, dummy, dummy0, nullstring, empty;
- static VARIANT continuous, automatic, medium, thin, none;
- static VARIANT yellow, solid;
- static VARIANT GetCell ( int row, int column )
- {
- VARIANT cell;
- VARIANT result;
- LPDISPATCH dispatch;
- OLECHAR cellname[20];
- V_VT ( &cell ) = VT_EMPTY;
- assert ( column > 0 );
- swprintf ( cellname, L"%c%d", 'A'+column -1 , row );
- V_VT ( &cell ) = VT_BSTR;
- V_BSTR ( &cell ) = SysAllocString (cellname);
- V_VT ( &result ) = VT_BOOL;
- V_BOOL ( &result ) = FALSE;
- if ( !ws )
- {
- goto error;
- }
- if ( ! (dispatch = ws->GetRange (cell, cell )))
- {
- goto error;
- }
-
- range->AttachDispatch ( dispatch );
- result = range->GetValue ();
- range->ReleaseDispatch ( );
- error:
- VariantClear ( &cell );
- return result;
- }
- int PutCell ( int row, int column, OLECHAR *string, int val )
- {
- VARIANT cell;
- VARIANT newValue;
- int ok = FALSE;
- LPDISPATCH dispatch;
- OLECHAR cellname[20];
- V_VT ( &newValue ) = VT_EMPTY;
- V_VT ( &cell ) = VT_EMPTY;
- assert ( column > 0 );
- swprintf ( cellname, L"%c%d", 'A'+column-1, row );
- V_VT ( &cell ) = VT_BSTR;
- V_BSTR ( &cell ) = SysAllocString (cellname);
- if ( !ws )
- {
- goto error;
- }
- if ( ! (dispatch = ws->GetRange (cell, cell )))
- {
- goto error;
- }
- range->AttachDispatch ( dispatch );
-
- if ( string )
- {
- V_VT ( &newValue ) = VT_BSTR;
- if ( string[0] == '\'')
- {
- buffer[0] = '\\';
- wcscpy ( &buffer[1], string );
- V_BSTR ( &newValue ) = SysAllocString ( buffer );
- }
- else
- {
- V_BSTR ( &newValue ) = SysAllocString ( string );
- }
- }
- else
- {
- V_VT ( &newValue ) = VT_I4;
- V_I4 ( &newValue ) = val;
- }
- range->SetValue ( newValue );
- range->ReleaseDispatch ( );
- ok = TRUE;
- error:
- VariantClear ( &cell );
- VariantClear ( &newValue );
- return ok;
- }
- int PutSeparator ( int row )
- {
- // Rows(row).Select
- // Selection.Borders(xlDiagonalDown).LineStyle = xlNone
- // Selection.Borders(xlDiagonalUp).LineStyle = xlNone
- // Selection.Borders(xlEdgeLeft).LineStyle = xlNone
- // Selection.Borders(xlEdgeTop).LineStyle = xlNone
- // With Selection.Borders(xlEdgeBottom)
- // .LineStyle = xlContinuous
- // .Weight = xlMedium
- // .ColorIndex = xlAutomatic
- // End With
- // With Selection.Borders(xlEdgeRight)
- // .LineStyle = xlContinuous
- // .Weight = xlThin
- // .ColorIndex = xlAutomatic
- // End With
- int ok = FALSE;
- Border *border = NULL;
- Borders *borders = NULL;
- LPDISPATCH dispatch;
- OLECHAR cellname1[20];
- OLECHAR cellname2[20];
- VARIANT cell1,cell2;
- if ( !ws )
- {
- goto error;
- }
- assert ( row > 0 );
- swprintf ( cellname1, L"A%d", row );
- swprintf ( cellname2, L"%c%d", 'A'+CELL_LAST-1-1, row );
- V_VT ( &cell1 ) = VT_BSTR;
- V_BSTR ( &cell1 ) = SysAllocString (cellname1);
- V_VT ( &cell2 ) = VT_BSTR;
- V_BSTR ( &cell2 ) = SysAllocString (cellname2);
- if ( ! (dispatch = ws->GetRange (cell1, cell2 )))
- {
- goto error;
- }
- range->AttachDispatch ( dispatch );
- dispatch = range->GetBorders ();
- borders = new Borders ( dispatch );
- if ( !borders )
- {
- goto error;
- }
- dispatch = borders->GetItem ( xlEdgeBottom );
- border = new Border ( dispatch );
- if ( !border )
- {
- goto error;
- }
- border->SetLineStyle ( continuous );
- border->SetColorIndex ( automatic );
- border->SetWeight ( thin );
- ok = TRUE;
- error:
- range->ReleaseDispatch ( );
- if ( borders )
- {
- delete borders ;
- }
- if ( border )
- {
- delete border ;
- }
- VariantClear ( &cell1 );
- VariantClear ( &cell2 );
- return ok;
- }
- int PutSection ( int row, OLECHAR *title )
- {
- int ok = FALSE;
- Range *range = NULL;
- Border *border = NULL;
- Borders *borders = NULL;
- Interior *interior = NULL;
- LPDISPATCH dispatch;
- OLECHAR cellname1[20];
- OLECHAR cellname2[20];
- VARIANT cell1,cell2;
- _Worksheet *ws = NULL;
- if ( !ws )
- {
- goto error;
- }
- assert ( row > 0 );
- swprintf ( cellname1, L"A%d", row );
- swprintf ( cellname2, L"%c%d", 'A'+CELL_LAST-1-1, row );
- V_VT ( &cell1 ) = VT_BSTR;
- V_BSTR ( &cell1 ) = SysAllocString (cellname1);
- V_VT ( &cell2 ) = VT_BSTR;
- V_BSTR ( &cell2 ) = SysAllocString (cellname2);
- if ( ! (dispatch = ws->GetRange (cell1, cell2 )))
- {
- goto error;
- }
- range->AttachDispatch ( dispatch );
- dispatch = range->GetBorders ();
- borders = new Borders ( dispatch );
- if ( !borders )
- {
- goto error;
- }
- dispatch = borders->GetItem ( xlEdgeBottom );
- border = new Border ( dispatch );
- if ( !border )
- {
- goto error;
- }
- border->SetLineStyle ( continuous );
- border->SetColorIndex ( automatic );
- border->SetWeight ( thin );
- delete border;
- border = NULL;
- dispatch = borders->GetItem ( xlEdgeTop );
- border = new Border ( dispatch );
- if ( !border )
- {
- goto error;
- }
- border->SetLineStyle ( continuous );
- border->SetColorIndex ( automatic );
- border->SetWeight ( medium );
- delete border;
- border = NULL;
- dispatch = borders->GetItem ( xlEdgeRight );
- border = new Border ( dispatch );
- if ( !border )
- {
- goto error;
- }
- border->SetLineStyle ( none );
- delete border;
- border = NULL;
- dispatch = borders->GetItem ( xlEdgeLeft );
- border = new Border ( dispatch );
- if ( !border )
- {
- goto error;
- }
- border->SetLineStyle ( none );
- dispatch = range->GetInterior ( );
- interior = new Interior ( dispatch );
- if ( !interior )
- {
- goto error;
- }
- interior->SetColorIndex ( yellow );
- interior->SetPattern ( solid );
- PutCell ( row, 1, L"Section", 0 );
- PutCell ( row, 2, title, 0 );
- ok = TRUE;
- error:
- range->ReleaseDispatch ( );
- if ( borders )
- {
- delete borders ;
- }
- if ( border )
- {
- delete border ;
- }
- VariantClear ( &cell1 );
- VariantClear ( &cell2 );
- return ok;
- }
- int OpenExcel ( void )
- {
- LPDISPATCH dispatch;
- if ( xl )
- {
- return TRUE;
- }
- #if 0
- while ( ExcelRunning ())
- {
- if ( AfxMessageBox ( "Excel is running!\n\nClose or kill all instances of Excel and retry\n\nNOTE: Check task tray (CTRL-ALT-DELETE) for instances of Excel", MB_OKCANCEL ) == IDCANCEL )
- {
- return FALSE;
- }
- }
- #endif
- xl = new _Application();
- if ( !xl )
- {
- return FALSE;
- }
- if ( !xl->CreateDispatch ("Excel.Application"))
- {
- goto error_access;
- }
- dispatch = xl->GetWorkbooks ( );
- if ( dispatch )
- {
- wbs = new Workbooks( dispatch );
- }
- if ( !wbs )
- {
- return FALSE;
- }
- if ( ! (ws = new _Worksheet ()))
- {
- return FALSE;
- }
- if ( ! (range = new Range ()))
- {
- return FALSE;
- }
- V_VT ( &no ) = VT_BOOL;
- V_VT ( &yes ) = VT_BOOL;
- V_VT ( &dummy ) = VT_I4;
- V_VT ( &dummy0 ) = VT_I4;
- V_VT ( &nullstring ) = VT_BSTR ;
- V_VT ( &empty ) = VT_EMPTY;
- V_VT ( &continuous ) = VT_I4;
- V_VT ( &automatic ) = VT_I4;
- V_VT ( &medium ) = VT_I4;
- V_VT ( &thin ) = VT_I4;
- V_VT ( &none ) = VT_I4;
- V_VT ( &solid ) = VT_I4;
- V_VT ( &yellow ) = VT_I4;
- V_BOOL ( &no ) = FALSE;
- V_BOOL ( &yes ) = TRUE;
- V_I4 ( &dummy ) = 1;
- V_I4 ( &dummy0 ) = 0;
- V_BSTR ( &nullstring ) = SysAllocString ( OLESTR ("") );
- V_I4 ( &continuous ) = xlContinuous;
- V_I4 ( &automatic ) = xlAutomatic;
- V_I4 ( &medium ) = xlMedium;
- V_I4 ( &thin ) = xlThin;
- V_I4 ( &none ) = xlThin;
- V_I4 ( &solid ) = xlSolid;
- V_I4 ( &yellow ) = 6;
- return TRUE;
- error_access:
- AfxMessageBox ("Could not access Excel!\n\nMake sure Excel is installed on this system.");
- return FALSE;
- }
- void CloseExcel ( void )
- {
- CloseWorkBook ();
- if ( range )
- {
- delete range;
- range = NULL;
- }
- if ( ws )
- {
- delete ws;
- ws = NULL;
- }
- if ( wbs )
- {
- wbs->Close();
- delete wbs;
- wbs = NULL;
- }
- if ( xl )
- {
- xl->Quit();
- xl->ReleaseDispatch ();
- delete xl;
- xl = NULL;
- }
- VariantClear ( &nullstring );
- }
- int OpenWorkBook ( const char *filename )
- {
- LPDISPATCH dispatch;
-
- dispatch = wbs->Open ((LPCTSTR) filename, dummy0, yes, dummy, nullstring, nullstring, yes, dummy, dummy, no, no, dummy, no );
- if ( dispatch )
- {
- workbook = new _Workbook ( dispatch );
- }
- if ( !workbook )
- {
- return FALSE;
- }
- SelectActiveSheet ( );
- return TRUE;
- }
- int NewWorkBook ( const char *path )
- {
- LPDISPATCH dispatch;
- VARIANT temp;
- char tfile[200];
- char *p;
- WIN32_FIND_DATA finfo;
- HANDLE handle;
- V_VT ( &temp ) = VT_I4;
- V_I4 ( &temp ) = xlWBATWorksheet;
- if ( path )
- {
- strcpy ( tfile, path );
- if ( (p = strchr ( tfile, '.' )))
- {
- *p = 0;
- }
-
- strcpy ( p, ".xlt" );
-
- if ( (handle = FindFirstFile ( tfile, &finfo)) != INVALID_HANDLE_VALUE )
- {
- if ( !(finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
- {
- swprintf ( buffer, L"%S", tfile );
- V_VT ( &temp ) = VT_BSTR;
- V_BSTR ( &temp ) = SysAllocString ( buffer );
- }
-
- FindClose ( handle );
- }
- }
- dispatch = wbs->Add ( temp );
- VariantClear ( &temp );
- if ( dispatch )
- {
- workbook = new _Workbook ( dispatch );
- }
- if ( !workbook )
- {
- return FALSE;
- }
- SelectActiveSheet ( );
- return TRUE;
- }
- int SaveWorkBook ( const char *filename, int protect )
- {
- VARIANT name, fileformat, rc;
- V_VT ( &name ) = VT_BSTR;
- swprintf ( buffer, L"%S", filename );
- V_BSTR ( &name ) = SysAllocString ( buffer );
- V_VT ( &fileformat ) = VT_I4;
- V_I4 ( &fileformat ) = xlWorkbookNormal;
- V_VT ( &rc ) = VT_I4;
- V_I4 ( &rc ) = xlLocalSessionChanges;
- if ( protect )
- {
- VARIANT password;
- V_VT ( &password ) = VT_BSTR;
- V_BSTR ( &password ) = SysAllocString ( L"" );
- ws->Protect ( password, yes, yes, yes, no );
- VariantClear ( &password );
- }
- workbook->SaveAs ( name, fileformat, nullstring, nullstring, no, no,
- xlNoChange, rc, no, empty, empty );
- VariantClear ( &name );
- return TRUE;
- }
- void CloseWorkBook ( void )
- {
- if ( workbook )
- {
- workbook->SetSaved ( TRUE );
- workbook->Close ( no, nullstring, no );
- delete workbook;
- workbook = NULL;
- }
- }
- void SelectActiveSheet ( void )
- {
- LPDISPATCH dispatch;
- if ( ! (dispatch = xl->GetActiveSheet ()))
- {
- return;
- }
- ws->ReleaseDispatch ( );
- ws->AttachDispatch ( dispatch );
- }
- int GetInt ( int row, int cell )
- {
- long value;
- VARIANT var;
- _variant_t v;
- var = GetCell ( row, cell );
- v.Attach ( var );
- value = v;
- return (int) value;
- }
- int GetString ( int row, int cell, OLECHAR *string )
- {
- VARIANT var;
- string[0] =0;
- var = GetCell ( row, cell );
- if ( V_VT ( &var ) == VT_BSTR )
- {
- wcscpy ( string, V_BSTR ( &var ));
- }
- VariantClear ( &var );
- return 1;
- }
|