-
Notifications
You must be signed in to change notification settings - Fork 131
/
excel.c
6464 lines (5447 loc) · 173 KB
/
excel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2014 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Ilia Alshanetsky <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "libxl.h"
#include <stdlib.h>
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/date/php_date.h"
#if defined(HAVE_XML) && defined(EXCEL_WITH_LIBXML)
#include "ext/xml/php_xml.h"
#endif
#include "php_excel.h"
#include "zend_exceptions.h"
static long xlFormatBorder(FormatHandle f)
{
return 1;
}
static long xlFormatBorderColor(FormatHandle f)
{
return 1;
}
/* work-around for missing headers in LibXL */
#ifndef LIBXL_VERSION
#define xlSheetSetProtect xlSheetSetProtectA
#ifndef HAVE_LIBXL_243_PLUS
#define xlSheetProtect xlSheetProtectA
#endif
#endif
#if LIBXL_VERSION >= 0x03020000
#define xlBookSetRefR1C1 xlBookSetRefR1C1A
#define xlBookRefR1C1 xlBookRefR1C1A
#endif
#if LIBXL_VERSION >= 0x03020000 && LIBXL_VERSION < 0x03050401
enum libXLPictureType {PICTURETYPE_PNG, PICTURETYPE_JPEG, PICTURETYPE_WMF, PICTURETYPE_DIB, PICTURETYPE_EMF, PICTURETYPE_PICT, PICTURETYPE_TIFF, PICTURETYPE_ERROR = 0xFF};
#endif
#define PHP_EXCEL_DATE 1
#define PHP_EXCEL_FORMULA 2
#define PHP_EXCEL_NUMERIC_STRING 3
#define PHP_EXCEL_VERSION "1.0.3dev"
#ifdef COMPILE_DL_EXCEL
ZEND_GET_MODULE(excel)
#endif
ZEND_DECLARE_MODULE_GLOBALS(excel)
static PHP_GINIT_FUNCTION(excel);
PHP_INI_BEGIN()
#if defined(HAVE_LIBXL_SETKEY)
STD_PHP_INI_ENTRY("excel.license_name", NULL, PHP_INI_ALL, OnUpdateString, ini_license_name, zend_excel_globals, excel_globals)
STD_PHP_INI_ENTRY("excel.license_key", NULL, PHP_INI_ALL, OnUpdateString, ini_license_key, zend_excel_globals, excel_globals)
#endif
STD_PHP_INI_ENTRY("excel.skip_empty", "0", PHP_INI_ALL, OnUpdateLong, ini_skip_empty, zend_excel_globals, excel_globals)
PHP_INI_END()
/* {{{ OO init/structure stuff */
#define REGISTER_EXCEL_CLASS(name, c_name, clone) \
{ \
zend_class_entry ce; \
INIT_CLASS_ENTRY(ce, "Excel" # name, excel_funcs_ ## c_name); \
ce.create_object = excel_object_new_ ## c_name; \
excel_ce_ ## c_name = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC); \
memcpy(&excel_object_handlers_ ## c_name, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
excel_object_handlers_ ## c_name.clone_obj = clone; \
}
zend_class_entry *excel_ce_book, *excel_ce_sheet, *excel_ce_format, *excel_ce_font;
static zend_object_handlers excel_object_handlers_book;
static zend_object_handlers excel_object_handlers_sheet;
static zend_object_handlers excel_object_handlers_format;
static zend_object_handlers excel_object_handlers_font;
typedef struct _excel_book_object {
zend_object std;
BookHandle book;
} excel_book_object;
#define BOOK_FROM_OBJECT(book, object) \
{ \
excel_book_object *obj = (excel_book_object*) zend_object_store_get_object(object TSRMLS_CC); \
book = obj->book; \
if (!book) { \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The book wasn't initialized"); \
RETURN_FALSE; \
} \
}
typedef struct _excel_sheet_object {
zend_object std;
SheetHandle sheet;
BookHandle book;
} excel_sheet_object;
#define SHEET_FROM_OBJECT(sheet, object) \
{ \
excel_sheet_object *obj = (excel_sheet_object*) zend_object_store_get_object(object TSRMLS_CC); \
sheet = obj->sheet; \
if (!sheet) { \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The sheet wasn't initialized"); \
RETURN_FALSE; \
} \
}
#define SHEET_AND_BOOK_FROM_OBJECT(sheet, book, object) \
{ \
excel_sheet_object *obj = (excel_sheet_object*) zend_object_store_get_object(object TSRMLS_CC); \
sheet = obj->sheet; \
book = obj->book; \
if (!sheet) { \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The sheet wasn't initialized"); \
RETURN_FALSE; \
} \
}
#define FONT_FROM_OBJECT(font, object) \
{ \
excel_font_object *obj = (excel_font_object*) zend_object_store_get_object(object TSRMLS_CC); \
font = obj->font; \
if (!font) { \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The font wasn't initialized"); \
RETURN_FALSE; \
} \
}
typedef struct _excel_font_object {
zend_object std;
FontHandle font;
BookHandle book;
} excel_font_object;
#define FORMAT_FROM_OBJECT(format, object) \
{ \
excel_format_object *obj = (excel_format_object*) zend_object_store_get_object(object TSRMLS_CC); \
format = obj->format; \
if (!format) { \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The format wasn't initialized"); \
RETURN_FALSE; \
} \
}
typedef struct _excel_format_object {
zend_object std;
FormatHandle format;
BookHandle book;
} excel_format_object;
static void excel_book_object_free_storage(void *object TSRMLS_DC)
{
excel_book_object *intern = (excel_book_object *)object;
zend_object_std_dtor(&intern->std TSRMLS_CC);
if (intern->book) {
xlBookRelease(intern->book);
intern->book = NULL;
}
efree(object);
}
static zend_object_value excel_object_new_book(zend_class_entry *class_type TSRMLS_DC)
{
excel_book_object *intern;
zend_object_value retval;
intern = emalloc(sizeof(excel_book_object));
memset(intern, 0, sizeof(excel_book_object));
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
#ifdef ZEND_ENGINE_2_4
object_properties_init(&intern->std, class_type);
#else
{
zval *tmp;
zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
}
#endif
intern->book = xlCreateBook();
(&retval)->handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t) excel_book_object_free_storage, NULL TSRMLS_CC);
(&retval)->handlers = &excel_object_handlers_book;
return retval;
}
static void excel_sheet_object_free_storage(void *object TSRMLS_DC)
{
excel_sheet_object *intern = (excel_sheet_object *)object;
zend_object_std_dtor(&intern->std TSRMLS_CC);
efree(object);
}
static zend_object_value excel_object_new_sheet(zend_class_entry *class_type TSRMLS_DC)
{
excel_sheet_object *intern;
zend_object_value retval;
intern = emalloc(sizeof(excel_sheet_object));
memset(intern, 0, sizeof(excel_sheet_object));
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
#ifdef ZEND_ENGINE_2_4
object_properties_init(&intern->std, class_type);
#else
{
zval *tmp;
zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
}
#endif
(&retval)->handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t) excel_sheet_object_free_storage, NULL TSRMLS_CC);
(&retval)->handlers = &excel_object_handlers_sheet;
return retval;
}
static void excel_font_object_free_storage(void *object TSRMLS_DC)
{
excel_font_object *intern = (excel_font_object *)object;
zend_object_std_dtor(&intern->std TSRMLS_CC);
efree(object);
}
#define REGISTER_EXCEL_CLASS_CONST_LONG(class_name, const_name, value) \
zend_declare_class_constant_long(excel_ce_ ## class_name, const_name, sizeof(const_name)-1, (long)value TSRMLS_CC);
static zend_object_value excel_object_new_font_ex(zend_class_entry *class_type, excel_font_object **ptr TSRMLS_DC)
{
excel_font_object *intern;
zend_object_value retval;
intern = emalloc(sizeof(excel_font_object));
memset(intern, 0, sizeof(excel_font_object));
if (ptr) {
*ptr = intern;
}
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
#ifdef ZEND_ENGINE_2_4
object_properties_init(&intern->std, class_type);
#else
{
zval *tmp;
zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
}
#endif
(&retval)->handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t) excel_font_object_free_storage, NULL TSRMLS_CC);
(&retval)->handlers = &excel_object_handlers_font;
return retval;
}
static zend_object_value excel_object_new_font(zend_class_entry *class_type TSRMLS_DC)
{
return excel_object_new_font_ex(class_type, NULL TSRMLS_CC);
}
static zend_object_value excel_font_object_clone(zval *this_ptr TSRMLS_DC)
{
excel_font_object *new_obj = NULL;
excel_font_object *old_obj = (excel_font_object *) zend_object_store_get_object(this_ptr TSRMLS_CC);
zend_object_value new_ov = excel_object_new_font_ex(old_obj->std.ce, &new_obj TSRMLS_CC);
FontHandle font;
font = xlBookAddFont(old_obj->book, old_obj->font);
if (!font) {
zend_throw_exception(NULL, "Failed to copy font", 0 TSRMLS_CC);
} else {
new_obj->book = old_obj->book;
new_obj->font = font;
}
zend_objects_clone_members(&new_obj->std, new_ov, &old_obj->std, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
return new_ov;
}
static void excel_format_object_free_storage(void *object TSRMLS_DC)
{
excel_format_object *intern = (excel_format_object *)object;
zend_object_std_dtor(&intern->std TSRMLS_CC);
efree(object);
}
static zend_object_value excel_object_new_format_ex(zend_class_entry *class_type, excel_format_object **ptr TSRMLS_DC)
{
excel_format_object *intern;
zend_object_value retval;
intern = emalloc(sizeof(excel_format_object));
memset(intern, 0, sizeof(excel_format_object));
if (ptr) {
*ptr = intern;
}
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
#ifdef ZEND_ENGINE_2_4
object_properties_init(&intern->std, class_type);
#else
{
zval *tmp;
zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
}
#endif
(&retval)->handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t) excel_format_object_free_storage, NULL TSRMLS_CC);
(&retval)->handlers = &excel_object_handlers_format;
return retval;
}
static zend_object_value excel_object_new_format(zend_class_entry *class_type TSRMLS_DC)
{
return excel_object_new_format_ex(class_type, NULL TSRMLS_CC);
}
static zend_object_value excel_format_object_clone(zval *this_ptr TSRMLS_DC)
{
excel_format_object *new_obj = NULL;
excel_format_object *old_obj = (excel_format_object *) zend_object_store_get_object(this_ptr TSRMLS_CC);
zend_object_value new_ov = excel_object_new_format_ex(old_obj->std.ce, &new_obj TSRMLS_CC);
FormatHandle format;
format = xlBookAddFormat(old_obj->book, old_obj->format);
if (!format) {
zend_throw_exception(NULL, "Failed to copy format", 0 TSRMLS_CC);
} else {
new_obj->book = old_obj->book;
new_obj->format = format;
}
zend_objects_clone_members(&new_obj->std, new_ov, &old_obj->std, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
return new_ov;
}
#if LIBXL_VERSION <= 0x03010000
static wchar_t * _php_excel_to_wide(const char *string, size_t len, size_t *out_len)
{
wchar_t *buf = safe_emalloc(len, sizeof(wchar_t), 0);
*out_len = mbstowcs(buf, string, len);
if (*out_len == (size_t) -1) {
efree(buf);
return NULL;
}
return erealloc(buf, (*out_len + 1) * sizeof(wchar_t));
}
#endif
#define EXCEL_METHOD(class_name, function_name) \
PHP_METHOD(Excel ## class_name, function_name)
/* {{{ proto bool ExcelBook::requiresKey()
true if license key is required. */
EXCEL_METHOD(Book, requiresKey)
{
#if defined(HAVE_LIBXL_SETKEY)
RETURN_BOOL(1);
#else
RETURN_BOOL(0);
#endif
}
/* }}} */
/* {{{ proto bool ExcelBook::load(string data)
Load Excel data string. */
EXCEL_METHOD(Book, load)
{
BookHandle book;
zval *object = getThis();
char *data;
int data_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len) == FAILURE) {
RETURN_FALSE;
}
if (!data_len) {
RETURN_FALSE;
}
BOOK_FROM_OBJECT(book, object);
RETURN_BOOL(xlBookLoadRaw(book, data, data_len));
}
/* }}} */
/* {{{ proto bool ExcelBook::loadFile(string filename)
Load Excel from file. */
EXCEL_METHOD(Book, loadFile)
{
BookHandle book;
zval *object = getThis();
char *filename;
int filename_len;
php_stream *stream;
int len;
char *contents;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
RETURN_FALSE;
}
if (!filename_len) {
RETURN_FALSE;
}
BOOK_FROM_OBJECT(book, object);
stream = php_stream_open_wrapper(filename, "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL);
if (!stream) {
RETURN_FALSE;
}
len = php_stream_copy_to_mem(stream, &contents, PHP_STREAM_COPY_ALL, 0);
php_stream_close(stream);
if (len < 1) {
RETURN_FALSE;
}
RETVAL_BOOL(xlBookLoadRaw(book, contents, len));
efree(contents);
}
/* }}} */
/* {{{ proto mixed ExcelBook::save([string filename])
Save Excel file. */
EXCEL_METHOD(Book, save)
{
BookHandle book;
zval *object = getThis();
char *filename = NULL;
int filename_len;
unsigned int len = 0;
char *contents = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &filename, &filename_len) == FAILURE) {
RETURN_FALSE;
}
BOOK_FROM_OBJECT(book, object);
if (!xlBookSaveRaw(book, (const char **)&contents, &len)) {
RETURN_FALSE;
}
if (filename) {
int numbytes;
php_stream *stream = php_stream_open_wrapper(filename, "wb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL);
if (!stream) {
RETURN_FALSE;
}
if ((numbytes = php_stream_write(stream, contents, len)) != len) {
php_stream_close(stream);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only %d of %d bytes written, possibly out of free disk space", numbytes, len);
RETURN_FALSE;
}
php_stream_close(stream);
RETURN_TRUE;
} else {
RETURN_STRINGL(contents, len, 1);
}
}
/* }}} */
/* {{{ proto ExcelSheet ExcelBook::getSheet([int sheet])
Get an excel sheet. */
EXCEL_METHOD(Book, getSheet)
{
BookHandle book;
zval *object = getThis();
long sheet = 0;
SheetHandle sh;
excel_sheet_object *fo;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &sheet) == FAILURE) {
RETURN_FALSE;
}
if (sheet < 0) {
RETURN_FALSE;
}
BOOK_FROM_OBJECT(book, object);
if (!(sh = xlBookGetSheet(book, sheet))) {
RETURN_FALSE;
}
Z_TYPE_P(return_value) = IS_OBJECT;
object_init_ex(return_value, excel_ce_sheet);
Z_SET_REFCOUNT_P(return_value, 1);
Z_SET_ISREF_P(return_value);
fo = (excel_sheet_object *) zend_object_store_get_object(return_value TSRMLS_CC);
fo->sheet = sh;
fo->book = book;
}
/* }}} */
/* {{{ proto ExcelSheet ExcelBook::getSheetByName(string name [, bool case_insensitive])
Get an excel sheet by name. */
EXCEL_METHOD(Book, getSheetByName)
{
BookHandle book;
zval *object = getThis();
char *sheet_name;
int sheet_name_len;
long sheet;
excel_sheet_object *fo;
long sheet_count;
zend_bool case_s = 0;
const char *s;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &sheet_name, &sheet_name_len, &case_s) == FAILURE) {
RETURN_FALSE;
}
if (sheet_name_len == 0) {
RETURN_FALSE;
}
BOOK_FROM_OBJECT(book, object);
sheet_count = xlBookSheetCount(book);
for(sheet = 0; sheet < sheet_count; sheet++) {
SheetHandle sh = xlBookGetSheet(book, sheet);
if (sh) {
s = xlSheetName(sh);
if (s) {
if ((case_s && !strcasecmp(s, sheet_name)) || (!case_s && !strcmp(s, sheet_name))) {
Z_TYPE_P(return_value) = IS_OBJECT;
object_init_ex(return_value, excel_ce_sheet);
Z_SET_REFCOUNT_P(return_value, 1);
Z_SET_ISREF_P(return_value);
fo = (excel_sheet_object *) zend_object_store_get_object(return_value TSRMLS_CC);
fo->sheet = sh;
fo->book = book;
return;
}
}
}
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool ExcelBook::deleteSheet(int sheet)
Delete an excel sheet. */
EXCEL_METHOD(Book, deleteSheet)
{
BookHandle book;
zval *object = getThis();
long sheet;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &sheet) == FAILURE) {
RETURN_FALSE;
}
if (sheet < 0) {
RETURN_FALSE;
}
BOOK_FROM_OBJECT(book, object);
RETURN_BOOL(xlBookDelSheet(book, sheet));
}
/* }}} */
/* {{{ proto int ExcelBook::activeSheet([int sheet])
Get or set an active excel sheet. */
EXCEL_METHOD(Book, activeSheet)
{
BookHandle book;
zval *object = getThis();
long sheet = -1;
long res;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &sheet) == FAILURE) {
RETURN_FALSE;
}
BOOK_FROM_OBJECT(book, object);
if (sheet > -1) {
xlBookSetActiveSheet(book, sheet);
}
res = xlBookActiveSheet(book);
if (sheet == -1 || res == sheet) {
RETURN_LONG(res);
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto ExcelSheet ExcelBook::addSheet(string name)
Add an excel sheet. */
EXCEL_METHOD(Book, addSheet)
{
BookHandle book;
zval *object = getThis();
SheetHandle sh;
excel_sheet_object *fo;
char *name;
int name_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) {
RETURN_FALSE;
}
BOOK_FROM_OBJECT(book, object);
#ifdef LIBXL_VERSION
sh = xlBookAddSheet(book, name, 0);
#else
sh = xlBookAddSheet(book, name);
#endif
if (!sh) {
RETURN_FALSE;
}
Z_TYPE_P(return_value) = IS_OBJECT;
object_init_ex(return_value, excel_ce_sheet);
Z_SET_REFCOUNT_P(return_value, 1);
Z_SET_ISREF_P(return_value);
fo = (excel_sheet_object *) zend_object_store_get_object(return_value TSRMLS_CC);
fo->sheet = sh;
fo->book = book;
}
/* }}} */
/* {{{ proto ExcelSheet ExcelBook::copySheet(string name, int sheet_number)
Copy an excel sheet. */
EXCEL_METHOD(Book, copySheet)
{
BookHandle book;
zval *object = getThis();
SheetHandle sh;
excel_sheet_object *fo;
char *name;
int name_len;
long num;
#ifdef LIBXL_VERSION
SheetHandle osh;
#endif
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &name, &name_len, &num) == FAILURE) {
RETURN_FALSE;
}
if (num < 0) {
RETURN_FALSE;
}
BOOK_FROM_OBJECT(book, object);
#ifdef LIBXL_VERSION
if (!(osh = xlBookGetSheet(book, num))) {
RETURN_FALSE;
}
sh = xlBookAddSheet(book, name, osh);
#else
sh = xlBookCopySheet(book, name, num);
#endif
if (!sh) {
RETURN_FALSE;
}
Z_TYPE_P(return_value) = IS_OBJECT;
object_init_ex(return_value, excel_ce_sheet);
Z_SET_REFCOUNT_P(return_value, 1);
Z_SET_ISREF_P(return_value);
fo = (excel_sheet_object *) zend_object_store_get_object(return_value TSRMLS_CC);
fo->sheet = sh;
fo->book = book;
}
/* }}} */
/* {{{ proto int ExcelBook::sheetCount()
Get the number of sheets inside a file. */
EXCEL_METHOD(Book, sheetCount)
{
BookHandle book;
zval *object = getThis();
if (ZEND_NUM_ARGS()) {
RETURN_FALSE;
}
BOOK_FROM_OBJECT(book, object);
RETURN_LONG(xlBookSheetCount(book));
}
/* }}} */
/* {{{ proto string ExcelBook::getError()
Get Excel error string. */
EXCEL_METHOD(Book, getError)
{
BookHandle book;
zval *object = getThis();
char *err;
if (ZEND_NUM_ARGS()) {
RETURN_FALSE;
}
BOOK_FROM_OBJECT(book, object);
err = (char *)xlBookErrorMessage(book);
if (err) {
if (!strcmp(err, "ok")) {
RETURN_FALSE;
} else {
RETURN_STRING(err, 1);
}
} else {
RETURN_STRING("Unknown Error", 1);
}
}
/* }}} */
/* {{{ proto ExcelFont ExcelBook::addFont([ExcelFont font])
Add or Copy ExcelFont object. */
EXCEL_METHOD(Book, addFont)
{
BookHandle book;
zval *object = getThis();
FontHandle nfont;
FontHandle font = NULL;
excel_font_object *fo;
zval *fob = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &fob, excel_ce_font) == FAILURE) {
RETURN_FALSE;
}
BOOK_FROM_OBJECT(book, object);
if (fob) {
FONT_FROM_OBJECT(font, fob);
}
nfont = xlBookAddFont(book, font);
if (!nfont) {
RETURN_FALSE;
}
Z_TYPE_P(return_value) = IS_OBJECT;
object_init_ex(return_value, excel_ce_font);
Z_SET_REFCOUNT_P(return_value, 1);
Z_SET_ISREF_P(return_value);
fo = (excel_font_object *) zend_object_store_get_object(return_value TSRMLS_CC);
fo->font = nfont;
fo->book = book;
}
/* }}} */
/* {{{ proto ExcelFormat ExcelBook::addFormat([ExcelFormat format])
Add or Copy ExcelFormat object. */
EXCEL_METHOD(Book, addFormat)
{
BookHandle book;
zval *object = getThis();
FormatHandle nformat;
FormatHandle format = NULL;
excel_format_object *fo;
zval *fob = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &fob, excel_ce_format) == FAILURE) {
RETURN_FALSE;
}
BOOK_FROM_OBJECT(book, object);
if (fob) {
FORMAT_FROM_OBJECT(format, fob);
}
nformat = xlBookAddFormat(book, format);
if (!nformat) {
RETURN_FALSE;
}
Z_TYPE_P(return_value) = IS_OBJECT;
object_init_ex(return_value, excel_ce_format);
Z_SET_REFCOUNT_P(return_value, 1);
Z_SET_ISREF_P(return_value);
fo = (excel_format_object *) zend_object_store_get_object(return_value TSRMLS_CC);
fo->format = nformat;
fo->book = book;
}
/* }}} */
#ifdef HAVE_LIBXL_243_PLUS
/* {{{ proto array ExcelBook::getAllFormats()
Get an array of all ExcelFormat objects used inside a document. */
EXCEL_METHOD(Book, getAllFormats)
{
BookHandle book;
zval *object = getThis();
unsigned short fc;
unsigned short c;
if (ZEND_NUM_ARGS()) {
RETURN_FALSE;
}
BOOK_FROM_OBJECT(book, object);
array_init(return_value);
fc = xlBookFormatSize(book);
if (!fc) {
return;
}
for (c = 0; c < fc; c++) {
FormatHandle format;
if ((format = xlBookFormat(book, c))) {
excel_format_object *fo;
zval *value;
MAKE_STD_ZVAL(value);
Z_TYPE_P(value) = IS_OBJECT;
object_init_ex(value, excel_ce_format);
Z_SET_REFCOUNT_P(value, 1);
Z_SET_ISREF_P(value);
fo = (excel_format_object *) zend_object_store_get_object(value TSRMLS_CC);
fo->format = format;
fo->book = book;
add_next_index_zval(return_value, value);
}
}
}
/* }}} */
#endif
/* {{{ proto int ExcelBook::addCustomFormat(string format)
Create a custom cell format */
EXCEL_METHOD(Book, addCustomFormat)
{
BookHandle book;
zval *object = getThis();
char *format;
int format_len;
int id;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &format, &format_len) == FAILURE) {
RETURN_FALSE;
}
if (!format_len) {
RETURN_FALSE;
}
BOOK_FROM_OBJECT(book, object);
if (!(id = xlBookAddCustomNumFormat(book, format))) {
RETURN_FALSE;
}
RETURN_LONG(id);
}
/* }}} */
/* {{{ proto string ExcelBook::getCustomFormat(int id)
Get a custom cell format */
EXCEL_METHOD(Book, getCustomFormat)
{
BookHandle book;
zval *object = getThis();
long id;
char *data;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &id) == FAILURE) {
RETURN_FALSE;
}
if (id < 1) {
RETURN_FALSE;
}
BOOK_FROM_OBJECT(book, object);
if (!(data = (char *)xlBookCustomNumFormat(book, id))) {
RETURN_FALSE;
}
RETURN_STRING(data, 1);
}
/* }}} */
static double _php_excel_date_pack(BookHandle book, long ts)
{
struct tm tm;
if (!php_localtime_r(&ts, &tm)) {
return -1;
}
tm.tm_year += 1900;
tm.tm_mon += 1;
return xlBookDatePack(book, tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec
#ifdef HAVE_LIBXL_243_PLUS
, 0
#endif
);
}
/* {{{ proto float ExcelBook::packDate(int timestamp)
Pack a unix timestamp into an Excel Double */
EXCEL_METHOD(Book, packDate)
{
BookHandle book;
zval *object = getThis();
long ts;
double dt;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ts) == FAILURE) {
RETURN_FALSE;
}
if (ts < 1) {
RETURN_FALSE;
}
BOOK_FROM_OBJECT(book, object);
if ((dt = _php_excel_date_pack(book, ts)) == -1) {
RETURN_FALSE;
}
RETURN_DOUBLE(dt);
}
/* }}} */
static double _php_excel_date_pack_values(BookHandle book, int year, int month, int day, int hour, int min, int sec)
{
return xlBookDatePack(book, year, month, day, hour, min, sec
#ifdef HAVE_LIBXL_243_PLUS
, 0
#endif
);
}
/* {{{ proto float ExcelBook::packDateValues(int year, int month, int day, int hour, int minute, int second)
Pack a date by single values into an Excel Double */
EXCEL_METHOD(Book, packDateValues)
{
BookHandle book;
zval *object = getThis();