1414#include < vector>
1515#include < typeinfo>
1616#include < fstream>
17- #include < iostream>
1817#include < cassert>
1918#include < map>
2019#include < memory>
@@ -61,7 +60,6 @@ namespace xt
6160 // if we've reached the global header, stop reading
6261 if (local_header[2 ] != 0x03 || local_header[3 ] != 0x04 )
6362 {
64- std::cout << " Reached global header" << std::endl;
6563 break ;
6664 }
6765
@@ -92,9 +90,7 @@ namespace xt
9290 uint16_t compr_method = *reinterpret_cast <uint16_t *>(&local_header[0 ] + 8 );
9391 uint32_t compr_bytes = *reinterpret_cast <uint32_t *>(&local_header[0 ] + 18 );
9492 uint32_t uncompr_bytes = *reinterpret_cast <uint32_t *>(&local_header[0 ] + 22 );
95- std::cout << compr_bytes << " , " << uncompr_bytes << std::endl;
9693
97- std::cout << " Compression Method: " << compr_method << std::endl;
9894 if (compr_method == 0 )
9995 {
10096 arrays.insert (result_type::value_type (varname,
@@ -202,7 +198,7 @@ namespace xt
202198 }
203199 else
204200 {
205- stream.seekg (compr_bytes, std::ios_base::cur);
201+ stream.seekg ((std::streamsize) compr_bytes, std::ios_base::cur);
206202 }
207203 }
208204 throw std::runtime_error (" Array " s + search_varname + " not found in file: " s + filename);
@@ -240,19 +236,19 @@ namespace xt
240236
241237 self_type& operator <<(const char * rhs) {
242238 // write in little endian
243- size_t len = strlen (rhs);
239+ std:: size_t len = strlen (rhs);
244240 this ->reserve (len);
245- for (size_t byte = 0 ; byte < len; byte++)
241+ for (std:: size_t byte = 0 ; byte < len; byte++)
246242 {
247243 this ->push_back (rhs[byte]);
248244 }
249245 return *this ;
250246 }
251247
252- bool write (const char * char_arr, std::streamsize size)
248+ bool write (const char * char_arr, std::size_t size)
253249 {
254250 this ->reserve (this ->size () + size);
255- for (size_t byte = 0 ; byte < size; byte++)
251+ for (std:: size_t byte = 0 ; byte < size; byte++)
256252 {
257253 this ->push_back (char_arr[byte]);
258254 }
@@ -268,8 +264,8 @@ namespace xt
268264
269265 void parse_zip_footer (std::istream& stream,
270266 uint16_t & nrecs,
271- size_t & global_header_size,
272- size_t & global_header_offset)
267+ std::streamsize & global_header_size,
268+ std::streamoff & global_header_offset)
273269 {
274270 std::vector<char > footer (22 );
275271 stream.seekg (-22 , std::ios_base::end);
@@ -300,28 +296,27 @@ namespace xt
300296 namespace detail
301297 {
302298 inline uint16_t msdos_time (uint16_t hour, uint16_t min, uint16_t sec) {
303- return hour << 11 | min << 5 | (sec / 2 );
299+ return ( uint16_t ) ( hour << 11 | min << 5 | (sec / 2 ) );
304300 }
305301
306302 inline uint16_t msdos_date (uint16_t year, uint16_t month, uint16_t day) {
307- return (year - 1980 ) << 9 | month << 5 | day;
303+ return (uint16_t ) (( year - 1980 ) << 9 | month << 5 | day) ;
308304 }
309305
310306 inline std::pair<uint16_t , uint16_t > time_pair ()
311307 {
312308 std::time_t t = std::time (nullptr );
313309 auto tm = std::localtime (&t);
314- return {msdos_time (tm->tm_hour , tm->tm_min , tm->tm_sec ),
315- msdos_date ((tm->tm_year + 1900 ), tm->tm_mon + 1 , tm->tm_mday )};
310+ return {msdos_time (( uint16_t ) tm->tm_hour , ( uint16_t ) tm->tm_min , ( uint16_t ) tm->tm_sec ),
311+ msdos_date ((uint16_t ) ( tm->tm_year + 1900 ), ( uint16_t ) ( tm->tm_mon + 1 ), ( uint16_t ) tm->tm_mday )};
316312 }
317313 }
318314
319315 template <class E >
320316 void dump_npz (std::string filename,
321317 std::string varname, const xt::xexpression<E>& e,
322318 bool compression = false ,
323- bool append_to_existing_file = true ,
324- std::string mode = " w" )
319+ bool append_to_existing_file = true )
325320 {
326321 // first, append a .npy to the fname
327322 varname += " .npy" ;
@@ -330,20 +325,20 @@ namespace xt
330325 std::ifstream in_stream (filename, std::ios_base::in | std::ios_base::binary);
331326
332327 uint16_t nrecs = 0 ;
333- size_t global_header_offset = 0 ;
328+ std::streamoff global_header_offset = 0 ;
334329 detail::binary_vector global_header;
335330
336- if (in_stream)
331+ if (in_stream && append_to_existing_file )
337332 {
338333 // zip file exists. we need to add a new npy file to it.
339334 // first read the footer. this gives us the offset and size
340335 // of the global header then read and store the global header.
341336 // below, we will write the the new data at the start of the
342337 // global header then append the global header and footer below it
343- size_t global_header_size;
338+ std::streamsize global_header_size;
344339 detail::parse_zip_footer (in_stream, nrecs, global_header_size, global_header_offset);
345340 in_stream.seekg (global_header_offset);
346- global_header.resize (global_header_size);
341+ global_header.resize ((std:: size_t ) global_header_size);
347342 in_stream.read (&global_header[0 ], global_header_size);
348343 if (!in_stream)
349344 {
@@ -366,9 +361,9 @@ namespace xt
366361 detail::dump_npy_stream (array_contents, e);
367362
368363 // get the CRC of the data to be added
369- uint32_t crc = crc32 (0L , (uint8_t *) &array_contents[0 ], array_contents.size ());
370- size_t uncompressed_size = array_contents.size ();
371- size_t compressed_size = uncompressed_size;
364+ uint32_t crc = ( uint32_t ) crc32 (0L , (uint8_t *) &array_contents[0 ], ( unsigned int ) array_contents.size ());
365+ std:: size_t uncompressed_size = array_contents.size ();
366+ std:: size_t compressed_size = uncompressed_size;
372367
373368 uint16_t compression_method = 0 ;
374369
@@ -377,13 +372,12 @@ namespace xt
377372 std::ostringstream ss;
378373 compression_method = 8 ;
379374 zstr::ostream compressed_array (ss, compression_method);
380- compressed_array.write (&array_contents[0 ], array_contents.size ());
375+ compressed_array.write (&array_contents[0 ], (std::streamsize) array_contents.size ());
381376 compressed_array.flush ();
382377 array_contents.clear ();
383378 array_contents << ss.str ();
384379 compressed_size = array_contents.size ();
385380 }
386-
387381 auto date_time_pair = detail::time_pair ();
388382
389383 // build the local header
@@ -392,9 +386,9 @@ namespace xt
392386 << (uint16_t ) 0x0403 // second part of signature
393387 << (uint16_t ) 20 // minimum version to extract
394388 << (uint16_t ) 0 // general purpose bit flag
395- << (uint16_t ) compression_method // compression method
396- << (uint16_t ) date_time_pair.first // file last mod time
397- << (uint16_t ) date_time_pair.second // file last mod date
389+ << (uint16_t ) compression_method // compression method
390+ << (uint16_t ) date_time_pair.first // file last mod time
391+ << (uint16_t ) date_time_pair.second // file last mod date
398392 << (uint32_t ) crc // checksum
399393 << (uint32_t ) compressed_size // compressed size
400394 << (uint32_t ) uncompressed_size // uncompressed size
@@ -403,9 +397,10 @@ namespace xt
403397 << varname; // file name
404398
405399 // build global header
406- global_header << " PK" // first part of sig
407- << (uint16_t ) 0x0201 // second part of sig
408- << (uint16_t ) 20 ; // version made by
400+ global_header << " PK" // first part of sig
401+ << (uint16_t ) 0x0201 // second part of sig
402+ << (uint16_t ) 0x0314 ; // version made by .. hardcoded to what NumPy uses here
403+ // (not sure if this depends on zlib version)
409404
410405 // Copy from local header
411406 global_header.insert (global_header.end (),
@@ -415,7 +410,7 @@ namespace xt
415410 global_header << (uint16_t ) 0 // file comment length
416411 << (uint16_t ) 0 // disk number where file starts
417412 << (uint16_t ) 0 // internal file attributes
418- << (uint32_t ) 0 // external file attributes
413+ << (uint32_t ) 0x81800000 // external file attributes (taken from numpy)
419414 << (uint32_t ) global_header_offset // relative offset of local file header,
420415 // since it begins where the global header used to begin
421416 << varname;
@@ -430,14 +425,14 @@ namespace xt
430425 << (uint16_t ) (nrecs + 1 ) // total number of records
431426 << (uint32_t ) global_header.size () // nbytes of global headers
432427 // offset of start of global headers, since global header now starts after newly written array
433- << (uint32_t ) (global_header_offset + compressed_size + local_header.size ())
428+ << (uint32_t ) ((std:: size_t ) global_header_offset + compressed_size + local_header.size ())
434429 << (uint16_t ) 0 ; // zip file comment length
435430
436431 // write everything
437- stream.write (&local_header[0 ], local_header.size ());
438- stream.write (&array_contents[0 ], array_contents.size ());
439- stream.write (&global_header[0 ], global_header.size ());
440- stream.write (&footer[0 ], footer.size ());
432+ stream.write (&local_header[0 ], (std::streamsize) local_header.size ());
433+ stream.write (&array_contents[0 ], (std::streamsize) array_contents.size ());
434+ stream.write (&global_header[0 ], (std::streamsize) global_header.size ());
435+ stream.write (&footer[0 ], (std::streamsize) footer.size ());
441436 }
442437
443438} // namespace xt
0 commit comments