@@ -365,6 +365,7 @@ static zend_result spl_filesystem_file_open(spl_filesystem_object *intern, bool
365
365
intern -> u .file .delimiter = ',' ;
366
366
intern -> u .file .enclosure = '"' ;
367
367
intern -> u .file .escape = (unsigned char ) '\\' ;
368
+ intern -> u .file .is_escape_default = true;
368
369
369
370
intern -> u .file .func_getCurr = zend_hash_str_find_ptr (& intern -> std .ce -> function_table , "getcurrentline" , sizeof ("getcurrentline" ) - 1 );
370
371
@@ -2273,16 +2274,33 @@ PHP_METHOD(SplFileObject, getChildren)
2273
2274
/* return NULL */
2274
2275
} /* }}} */
2275
2276
2277
+ static int spl_csv_enclosure_param_handling (const zend_string * escape_str , const spl_filesystem_object * intern , uint32_t arg_num )
2278
+ {
2279
+ if (escape_str == NULL ) {
2280
+ if (intern -> u .file .is_escape_default ) {
2281
+ php_error_docref (NULL , E_DEPRECATED , "the $escape parameter must be provided,"
2282
+ " as its default value will change,"
2283
+ " either explicitly or via SplFileObject::setCsvControl()" );
2284
+ if (UNEXPECTED (EG (exception ))) {
2285
+ return PHP_CSV_ESCAPE_ERROR ;
2286
+ }
2287
+ }
2288
+ return intern -> u .file .escape ;
2289
+ } else {
2290
+ return php_csv_handle_escape_argument (escape_str , arg_num );
2291
+ }
2292
+ }
2293
+
2276
2294
/* {{{ Return current line as CSV */
2277
2295
PHP_METHOD (SplFileObject , fgetcsv )
2278
2296
{
2279
2297
spl_filesystem_object * intern = spl_filesystem_from_obj (Z_OBJ_P (ZEND_THIS ));
2280
2298
char delimiter = intern -> u .file .delimiter , enclosure = intern -> u .file .enclosure ;
2281
- int escape = intern -> u . file . escape ;
2282
- char * delim = NULL , * enclo = NULL , * esc = NULL ;
2283
- size_t d_len = 0 , e_len = 0 , esc_len = 0 ;
2299
+ char * delim = NULL , * enclo = NULL ;
2300
+ size_t d_len = 0 , e_len = 0 ;
2301
+ zend_string * escape_str = NULL ;
2284
2302
2285
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "|sss " , & delim , & d_len , & enclo , & e_len , & esc , & esc_len ) == FAILURE ) {
2303
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "|ssS " , & delim , & d_len , & enclo , & e_len , & escape_str ) == FAILURE ) {
2286
2304
RETURN_THROWS ();
2287
2305
}
2288
2306
@@ -2302,23 +2320,12 @@ PHP_METHOD(SplFileObject, fgetcsv)
2302
2320
}
2303
2321
enclosure = enclo [0 ];
2304
2322
}
2305
- if (esc ) {
2306
- if (esc_len > 1 ) {
2307
- zend_argument_value_error (3 , "must be empty or a single character" );
2308
- RETURN_THROWS ();
2309
- }
2310
- if (esc_len == 0 ) {
2311
- escape = PHP_CSV_NO_ESCAPE ;
2312
- } else {
2313
- php_error_docref (NULL , E_DEPRECATED , "Passing a non-empty string to the $escape parameter is deprecated since 8.4" );
2314
- if (UNEXPECTED (EG (exception ))) {
2315
- RETURN_THROWS ();
2316
- }
2317
- escape = (unsigned char ) esc [0 ];
2318
- }
2323
+ int escape_char = spl_csv_enclosure_param_handling (escape_str , intern , 3 );
2324
+ if (escape_char == PHP_CSV_ESCAPE_ERROR ) {
2325
+ RETURN_THROWS ();
2319
2326
}
2320
2327
2321
- if (spl_filesystem_file_read_csv (intern , delimiter , enclosure , escape , return_value , true) == FAILURE ) {
2328
+ if (spl_filesystem_file_read_csv (intern , delimiter , enclosure , escape_char , return_value , true) == FAILURE ) {
2322
2329
RETURN_FALSE ;
2323
2330
}
2324
2331
}
@@ -2329,14 +2336,14 @@ PHP_METHOD(SplFileObject, fputcsv)
2329
2336
{
2330
2337
spl_filesystem_object * intern = spl_filesystem_from_obj (Z_OBJ_P (ZEND_THIS ));
2331
2338
char delimiter = intern -> u .file .delimiter , enclosure = intern -> u .file .enclosure ;
2332
- int escape = intern -> u .file .escape ;
2333
- char * delim = NULL , * enclo = NULL , * esc = NULL ;
2334
- size_t d_len = 0 , e_len = 0 , esc_len = 0 ;
2339
+ char * delim = NULL , * enclo = NULL ;
2340
+ size_t d_len = 0 , e_len = 0 ;
2335
2341
zend_long ret ;
2336
2342
zval * fields = NULL ;
2343
+ zend_string * escape_str = NULL ;
2337
2344
zend_string * eol = NULL ;
2338
2345
2339
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "a|sssS " , & fields , & delim , & d_len , & enclo , & e_len , & esc , & esc_len , & eol ) == FAILURE ) {
2346
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "a|ssSS " , & fields , & delim , & d_len , & enclo , & e_len , & escape_str , & eol ) == FAILURE ) {
2340
2347
RETURN_THROWS ();
2341
2348
}
2342
2349
@@ -2354,23 +2361,12 @@ PHP_METHOD(SplFileObject, fputcsv)
2354
2361
}
2355
2362
enclosure = enclo [0 ];
2356
2363
}
2357
- if (esc ) {
2358
- if (esc_len > 1 ) {
2359
- zend_argument_value_error (4 , "must be empty or a single character" );
2360
- RETURN_THROWS ();
2361
- }
2362
- if (esc_len == 0 ) {
2363
- escape = PHP_CSV_NO_ESCAPE ;
2364
- } else {
2365
- php_error_docref (NULL , E_DEPRECATED , "Passing a non-empty string to the $escape parameter is deprecated since 8.4" );
2366
- if (UNEXPECTED (EG (exception ))) {
2367
- RETURN_THROWS ();
2368
- }
2369
- escape = (unsigned char ) esc [0 ];
2370
- }
2364
+ int escape_char = spl_csv_enclosure_param_handling (escape_str , intern , 4 );
2365
+ if (escape_char == PHP_CSV_ESCAPE_ERROR ) {
2366
+ RETURN_THROWS ();
2371
2367
}
2372
2368
2373
- ret = php_fputcsv (intern -> u .file .stream , fields , delimiter , enclosure , escape , eol );
2369
+ ret = php_fputcsv (intern -> u .file .stream , fields , delimiter , enclosure , escape_char , eol );
2374
2370
if (ret < 0 ) {
2375
2371
RETURN_FALSE ;
2376
2372
}
@@ -2383,11 +2379,11 @@ PHP_METHOD(SplFileObject, setCsvControl)
2383
2379
{
2384
2380
spl_filesystem_object * intern = spl_filesystem_from_obj (Z_OBJ_P (ZEND_THIS ));
2385
2381
char delimiter = ',' , enclosure = '"' ;
2386
- int escape = ( unsigned char ) '\\' ;
2387
- char * delim = NULL , * enclo = NULL , * esc = NULL ;
2388
- size_t d_len = 0 , e_len = 0 , esc_len = 0 ;
2382
+ char * delim = NULL , * enclo = NULL ;
2383
+ size_t d_len = 0 , e_len = 0 ;
2384
+ zend_string * escape_str = NULL ;
2389
2385
2390
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "|sss " , & delim , & d_len , & enclo , & e_len , & esc , & esc_len ) == FAILURE ) {
2386
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "|ssS " , & delim , & d_len , & enclo , & e_len , & escape_str ) == FAILURE ) {
2391
2387
RETURN_THROWS ();
2392
2388
}
2393
2389
@@ -2405,25 +2401,17 @@ PHP_METHOD(SplFileObject, setCsvControl)
2405
2401
}
2406
2402
enclosure = enclo [0 ];
2407
2403
}
2408
- if (esc ) {
2409
- if (esc_len > 1 ) {
2410
- zend_argument_value_error (3 , "must be empty or a single character" );
2411
- RETURN_THROWS ();
2412
- }
2413
- if (esc_len == 0 ) {
2414
- escape = PHP_CSV_NO_ESCAPE ;
2415
- } else {
2416
- php_error_docref (NULL , E_DEPRECATED , "Passing a non-empty string to the $escape parameter is deprecated since 8.4" );
2417
- if (UNEXPECTED (EG (exception ))) {
2418
- RETURN_THROWS ();
2419
- }
2420
- escape = (unsigned char ) esc [0 ];
2421
- }
2404
+ int escape_char = php_csv_handle_escape_argument (escape_str , 3 );
2405
+ if (escape_char == PHP_CSV_ESCAPE_ERROR ) {
2406
+ RETURN_THROWS ();
2407
+ }
2408
+ if (escape_str != NULL ) {
2409
+ intern -> u .file .is_escape_default = false;
2422
2410
}
2423
2411
2424
2412
intern -> u .file .delimiter = delimiter ;
2425
2413
intern -> u .file .enclosure = enclosure ;
2426
- intern -> u .file .escape = escape ;
2414
+ intern -> u .file .escape = escape_char ;
2427
2415
}
2428
2416
/* }}} */
2429
2417
0 commit comments