001:
002:
003:
004:
005:
006:
007:
008:
009:
010:
011:
012:
013:
014:
015:
016:
017:
018:
019:
020:
021:
022:
023:
024:
025:
026:
027:
028:
029:
030:
031:
032:
033:
034:
035:
036:
037:
038:
039:
040:
041:
042:
043:
044:
045:
046:
047:
048:
049:
050:
051:
052:
053:
054:
055:
056:
057:
058:
059:
060:
061:
062:
063:
064:
065:
066:
067:
068:
069:
070:
071:
072:
073:
074:
075:
076:
077:
078:
079:
080:
081:
082:
083:
084:
085:
086:
087:
088:
089:
090:
091:
092:
093:
094:
095:
096:
097:
098:
099:
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:
Min/Max


















Min/Max



























































































































































































































































































































































































































































































































































































































































































































































































































/*
* Copyright (c), Zeriph Enterprises
* All rights reserved.
*
* Contributor(s):
* Zechariah Perez, omni (at) zeriph (dot) com
*
* THIS SOFTWARE IS PROVIDED BY ZERIPH AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ZERIPH AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* DEV_NOTE: this file is not intended to be used directly by any user code!

i.e. do not #include <omni/xxx_impl.hxx> and do not compile this source directly.
this file is included directly in other source.
*/

// so as not to accidentally build this file with the source
// these macros are defined in io.hpp
#if !defined(OMNI_IO_FILE_RW_FW)
    #error "invalid preprocessor directive detected"
#endif

#define OMNI_IO_FILE_EX_FW 1
#include <omni/xx/io/file_ex.hxx>

namespace omni { namespace io { namespace file_internal {
    template < typename STR, typename IFSTREAM, template < class, class > class std_seq_t, class T, class std_allocator_t >
    uint64_t rd(const STR& file, std_seq_t<T, std_allocator_t >& buffer, uint64_t length, std::streamoff file_offset, std::ios_base::seekdir offset_direction)
    {
        // TODO: add buffer_offset ???

        // DEV_NOTE: wifstream not take wchar_t[] filename since it is basic_ifstream<wchar_t>, which the constructors
        // for basic_ifstream are only basic_ifstream(const char* file..) and basic_ifstream(const std::string& file...)
        IFSTREAM ifile(omni::string::to_string(file).c_str(), std::ios::binary);
        if (!ifile.is_open()) { return 0; }
        if (length == 0) {
            ifile.seekg(0, ifile.end);
            length = static_cast<uint64_t>(ifile.tellg());
        }
        ifile.seekg(file_offset, offset_direction);
        // we cannot assume the SEQ type will allow contiguous memory, so we have to loop :/
        typename IFSTREAM::char_type* bf = new typename IFSTREAM::char_type[length];
        typename IFSTREAM::char_type* bfbeg = bf;
        ifile.read(bf, static_cast<std::streamsize>(length));
        length = static_cast<uint64_t>(ifile.gcount());
        ifile.close();

        // TODO: buffer.clear() ?? or just append?
        buffer.clear();
        if (length > 0) {
            typename IFSTREAM::char_type* bfend = bf + length;
            uint64_t i = 0;
            if (sizeof(T) == sizeof(typename IFSTREAM::char_type)) {
                for (i = 0; i < length; ++i) {
                    //buffer[i] = *(reinterpret_cast<T*>(bf));
                    buffer.push_back(*(reinterpret_cast<T*>(bf)));
                    ++bf;
                }
            } else {
                T* nbf = OMNI_NULL_PTR;
                uint64_t nlen = length / sizeof(T);
                uint64_t diff = length % sizeof(T);
                if (diff == 0) { // clean boundaries
                    for (i = 0; i < nlen; ++i) {
                        nbf = reinterpret_cast<T*>(bf);
                        //buffer[i] = *nbf;
                        buffer.push_back(*nbf);
                        bf += sizeof(T);
                        if (bf >= bfend) { break; }
                    }
                } else {
                    if (length < sizeof(T)) {
                        diff = length;
                        nlen = 0;
                    }
                    for (i = 0; i < nlen; ++i) {
                        nbf = reinterpret_cast<T*>(bf);
                        //buffer[i] = *nbf;
                        buffer.push_back(*nbf);
                        bf += sizeof(T);
                        if (bf >= bfend) { break; }
                    }
                    if (bf < bfend) {
                        T tval;
                        std::memset(&tval, 0, sizeof(T));
                        typename IFSTREAM::char_type* xbf = reinterpret_cast<typename IFSTREAM::char_type*>(&tval);
                        for (i = 0; i < diff; ++i) {
                            xbf[i] = *bf;
                            ++bf; //++xbf;
                            if (bf >= bfend) { break; }
                        }
                        buffer.push_back(tval);
                    }
                }
            }
            bf = bfbeg;
        }
        OMNI_FREE_ARR(bf);
        return length;
    }

    template < typename STR, typename IFSTREAM, typename T, std::size_t SZ >
    uint64_t rd(const STR& file, T (&buffer)[SZ], uint64_t length, std::size_t buffer_offset, std::streamoff file_offset, std::ios_base::seekdir offset_direction)
    {
        if (buffer_offset >= SZ) { return 0; }
        // DEV_NOTE: wifstream not take wchar_t[] filename since it is basic_ifstream<wchar_t>, which the constructors
        // for basic_ifstream are only basic_ifstream(const char* file..) and basic_ifstream(const std::string& file...)
        IFSTREAM ifile(omni::string::to_string(file).c_str(), std::ios::binary);
        if (!ifile.is_open()) { return 0; }
        if ((length == 0) || (length > SZ)) {
            length = SZ;
        }
        T* buf = &buffer[buffer_offset];
        typename IFSTREAM::char_type* bf = reinterpret_cast<typename IFSTREAM::char_type*>(buf);
        ifile.seekg(file_offset, offset_direction);
        ifile.read(bf, static_cast<std::streamsize>(length));
        length = static_cast<uint64_t>(ifile.gcount());
        ifile.close();
        return length;
    }

    template < typename STR, typename IFSTREAM, typename T, uint16_t SZ >
    uint64_t rd(const STR& file, omni::stack_buffer<T, SZ>& buffer, uint64_t length, std::size_t buffer_offset, std::streamoff file_offset, std::ios_base::seekdir offset_direction)
    {
        if (buffer_offset >= SZ) { return 0; }
        // DEV_NOTE: wifstream not take wchar_t[] filename since it is basic_ifstream<wchar_t>, which the constructors
        // for basic_ifstream are only basic_ifstream(const char* file..) and basic_ifstream(const std::string& file...)
        IFSTREAM ifile(omni::string::to_string(file).c_str(), std::ios::binary);
        if (!ifile.is_open()) { return 0; }
        if ((length == 0) || (length > SZ)) {
            length = SZ;
        }
        T* buf = buffer.data() + buffer_offset;
        typename IFSTREAM::char_type* bf = reinterpret_cast<typename IFSTREAM::char_type*>(buf);
        ifile.seekg(file_offset, offset_direction);
        // TODO: SZ or SZ*sizeof(T) ?????
        ifile.read(bf, static_cast<std::streamsize>(length));
        length = static_cast<uint64_t>(ifile.gcount());
        ifile.close();
        return length;
    }

    template < typename STR, typename IFSTREAM, template < class, class > class std_seq_t, class T, class std_allocator_t >
    uint64_t rd_el(const STR& file, std_seq_t<T, std_allocator_t >& buffer, uint64_t elements, std::streamoff file_offset, std::ios_base::seekdir offset_direction)
    {
        // TODO: add buffer_offset ???

        // DEV_NOTE: wifstream not take wchar_t[] filename since it is basic_ifstream<wchar_t>, which the constructors
        // for basic_ifstream are only basic_ifstream(const char* file..) and basic_ifstream(const std::string& file...)
        IFSTREAM ifile(omni::string::to_string(file).c_str(), std::ios::binary);
        if (!ifile.is_open()) { return 0; }
        if (elements == 0) {
            ifile.seekg(0, ifile.end);
            elements = static_cast<uint64_t>(ifile.tellg());
        }
        elements = elements * sizeof(T);
        ifile.seekg(file_offset, offset_direction);
        // we cannot assume the SEQ type will allow contiguous memory, so we have to loop :/
        typename IFSTREAM::char_type* bf = new typename IFSTREAM::char_type[elements];
        typename IFSTREAM::char_type* bfbeg = bf;
        ifile.read(bf, static_cast<std::streamsize>(elements));
        elements = static_cast<uint64_t>(ifile.gcount());
        ifile.close();

        // TODO: buffer.clear() ?? or just append?
        buffer.clear();
        if (elements > 0) {
            typename IFSTREAM::char_type* bfend = bf + elements;
            uint64_t i = 0;
            if (sizeof(T) == sizeof(typename IFSTREAM::char_type)) {
                for (i = 0; i < elements; ++i) {
                    //buffer[i] = *(reinterpret_cast<T*>(bf));
                    buffer.push_back(*(reinterpret_cast<T*>(bf)));
                    ++bf;
                }
            } else {
                T* nbf = OMNI_NULL_PTR;
                uint64_t nlen = elements / sizeof(T);
                uint64_t diff = elements % sizeof(T);
                if (diff == 0) { // clean boundaries
                    for (i = 0; i < nlen; ++i) {
                        nbf = reinterpret_cast<T*>(bf);
                        //buffer[i] = *nbf;
                        buffer.push_back(*nbf);
                        bf += sizeof(T);
                        if (bf >= bfend) { break; }
                    }
                } else {
                    if (elements < sizeof(T)) {
                        diff = elements;
                        nlen = 0;
                    }
                    for (i = 0; i < nlen; ++i) {
                        nbf = reinterpret_cast<T*>(bf);
                        //buffer[i] = *nbf;
                        buffer.push_back(*nbf);
                        bf += sizeof(T);
                        if (bf >= bfend) { break; }
                    }
                    if (bf < bfend) {
                        T tval;
                        std::memset(&tval, 0, sizeof(T));
                        typename IFSTREAM::char_type* xbf = reinterpret_cast<typename IFSTREAM::char_type*>(&tval);
                        for (i = 0; i < diff; ++i) {
                            xbf[i] = *bf;
                            ++bf; //++xbf;
                            if (bf >= bfend) { break; }
                        }
                        buffer.push_back(tval);
                    }
                }
            }
            bf = bfbeg;
        }
        OMNI_FREE_ARR(bf);
        if ((elements < sizeof(T)) && (elements > 0)) {
            return 1;
        } else if (elements % sizeof(T) != 0) {
            return (elements / sizeof(T)) + 1;
        }
        return elements / sizeof(T);
    }

    template < typename STR, typename IFSTREAM, typename T, std::size_t SZ >
    uint64_t rd_el(const STR& file, T (&buffer)[SZ], uint64_t elements, std::size_t buffer_offset, std::streamoff file_offset, std::ios_base::seekdir offset_direction)
    {
        if (buffer_offset >= SZ) { return 0; }
        // DEV_NOTE: wifstream not take wchar_t[] filename since it is basic_ifstream<wchar_t>, which the constructors
        // for basic_ifstream are only basic_ifstream(const char* file..) and basic_ifstream(const std::string& file...)
        IFSTREAM ifile(omni::string::to_string(file).c_str(), std::ios::binary);
        if (!ifile.is_open()) { return 0; }
        if ((elements == 0) || (elements > SZ)) {
            elements = SZ;
        }
        T* buf = &buffer[buffer_offset];
        typename IFSTREAM::char_type* bf = reinterpret_cast<typename IFSTREAM::char_type*>(buf);
        ifile.seekg(file_offset, offset_direction);
        ifile.read(bf, static_cast<std::streamsize>(elements * sizeof(T)));
        elements = static_cast<uint64_t>(ifile.gcount());
        ifile.close();
        if ((elements < sizeof(T)) && (elements > 0)) {
            return 1;
        } else if (elements % sizeof(T) != 0) {
            return (elements / sizeof(T)) + 1;
        }
        return elements / sizeof(T);
    }

    template < typename STR, typename IFSTREAM, typename T, uint16_t SZ >
    uint64_t rd_el(const STR& file, omni::stack_buffer<T, SZ>& buffer, uint64_t elements, std::size_t buffer_offset, std::streamoff file_offset, std::ios_base::seekdir offset_direction)
    {
        if (buffer_offset >= SZ) { return 0; }
        // DEV_NOTE: wifstream not take wchar_t[] filename since it is basic_ifstream<wchar_t>, which the constructors
        // for basic_ifstream are only basic_ifstream(const char* file..) and basic_ifstream(const std::string& file...)
        IFSTREAM ifile(omni::string::to_string(file).c_str(), std::ios::binary);
        if (!ifile.is_open()) { return 0; }
        if ((elements == 0) || (elements > SZ)) {
            elements = SZ;
        }
        T* buf = buffer.data() + buffer_offset;
        typename IFSTREAM::char_type* bf = reinterpret_cast<typename IFSTREAM::char_type*>(buf);
        ifile.seekg(file_offset, offset_direction);
        ifile.read(bf, static_cast<std::streamsize>(elements * sizeof(T)));
        elements = static_cast<uint64_t>(ifile.gcount());
        ifile.close();
        if ((elements < sizeof(T)) && (elements > 0)) {
            return 1;
        } else if (elements % sizeof(T) != 0) {
            return (elements / sizeof(T)) + 1;
        }
        return elements / sizeof(T);
    }

    template < typename STR, typename IFSTREAM, typename SEQ >
    uint64_t unsafe_rd_el(const STR& file, SEQ* buffer, uint64_t elements)
    {
        if ((elements == 0) || (buffer == OMNI_NULL_PTR)) { return 0; }
        IFSTREAM ifile(omni::string::to_string(file).c_str(), std::ios::binary);
        if (!ifile.is_open()) { return 0; }
        ifile.seekg(0, ifile.end);
        uint64_t length = static_cast<uint64_t>(ifile.tellg());
        ifile.seekg(0, ifile.beg);
        if (length > (elements * sizeof(SEQ))) { length = elements * sizeof(SEQ); }
        ifile.read(reinterpret_cast<typename IFSTREAM::char_type*>(buffer), static_cast<std::streamsize>(length));
        length = static_cast<uint64_t>(ifile.gcount());
        ifile.close();
        if ((length < sizeof(SEQ)) && (length > 0)) {
            return 1;
        } else if (length % sizeof(SEQ) != 0) {
            return (length / sizeof(SEQ)) + 1;
        }
        return length / sizeof(SEQ);
    }

    template < typename STR, typename OFSTREAM, template < class, class > class std_seq_t, class T, class std_allocator_t >
    uint64_t write_buf_fw(const STR& file, const std_seq_t<T, std_allocator_t >& buffer, uint64_t length, std::size_t buffer_offset, bool append, bool new_line)
    {
        OFSTREAM ofile(omni::string::to_string(file).c_str(), (append ? (std::ios::binary | std::ios::app) : std::ios::binary));
        uint64_t start = static_cast<uint64_t>(ofile.tellp());
        uint64_t end = start;
        typename std_seq_t<T, std_allocator_t >::const_iterator b;
        if (buffer_offset >= buffer.size()) {
            b = buffer.end();
        } else if (buffer_offset == 0) {
            b = buffer.begin();
        } else {
            b = buffer.begin() + buffer_offset;
        }
        if (length == 0) {
            for (; b != buffer.end(); ++b) {
                ofile << *b;
            }
        } else {
            for (; b != buffer.end(); ++b) {
                ofile << *b;
                if (--length == 0) { break; }
            }
        }
        if (new_line) { ofile << std::endl; }
        ofile.flush();
        end = static_cast<uint64_t>(ofile.tellp());
        ofile.close();
        return (end - start);
    }

    template < typename STR, typename OFSTREAM, typename T, std::size_t SZ >
    uint64_t write_buf_fw(const STR& file, const T (&buffer)[SZ], uint64_t length, std::size_t buffer_offset, bool append, bool new_line)
    {
        if (buffer_offset >= SZ) { return 0; }
        OFSTREAM ofile(omni::string::to_string(file).c_str(), (append ? (std::ios::binary | std::ios::app) : std::ios::binary));
        uint64_t start = static_cast<uint64_t>(ofile.tellp());
        uint64_t end = start;
        const T* buf = &buffer[buffer_offset];
        const typename OFSTREAM::char_type* bf = reinterpret_cast<const typename OFSTREAM::char_type*>(buf);
        uint64_t ssz = static_cast<uint64_t>(SZ - buffer_offset);
        if (ssz < length) {
            length = ssz;
        }
        if (length == 0) {
            ofile.write(bf, static_cast<std::streamsize>(ssz));
        } else {
            ofile.write(bf, length);
        }
        if (new_line) { ofile << std::endl; }
        ofile.flush();
        if ((ofile.rdstate() & OFSTREAM::failbit) != 0) {
            end = start;
            OMNI_ERR_FW("Error on write: failbit set", omni::exception("Error on write: failbit set"))
        } else if ((ofile.rdstate() & OFSTREAM::badbit) != 0) {
            end = start;
            OMNI_ERR_FW("Error on write: badbit set", omni::exception("Error on write: badbit set"))
        } else {
            end = static_cast<uint64_t>(ofile.tellp());
        }
        ofile.close();
        return (end - start);
    }

    template < typename STR, typename OFSTREAM, typename T, uint16_t SZ >
    uint64_t write_buf_fw(const STR& file, const omni::stack_buffer<T, SZ>& buffer, uint64_t length, uint16_t buffer_offset, bool append, bool new_line)
    {
        if (buffer_offset >= SZ) { return 0; }
        OFSTREAM ofile(omni::string::to_string(file).c_str(), (append ? (std::ios::binary | std::ios::app) : std::ios::binary));
        uint64_t start = static_cast<uint64_t>(ofile.tellp());
        uint64_t end = start;
        const T* buf = buffer.data() + buffer_offset;
        const typename OFSTREAM::char_type* bf = reinterpret_cast<const typename OFSTREAM::char_type*>(buf);
        uint64_t ssz = static_cast<std::streamsize>(SZ - buffer_offset);
        if (ssz < length) {
            length = ssz;
        }
        if (length == 0) {
            ofile.write(bf, static_cast<std::streamsize>(ssz));
        } else {
            ofile.write(bf, length);
        }
        if (new_line) { ofile << std::endl; }
        ofile.flush();
        if ((ofile.rdstate() & OFSTREAM::failbit) != 0) {
            end = start;
            OMNI_ERR_FW("Error on write: failbit set", omni::exception("Error on write: failbit set"))
        } else if ((ofile.rdstate() & OFSTREAM::badbit) != 0) {
            end = start;
            OMNI_ERR_FW("Error on write: badbit set", omni::exception("Error on write: badbit set"))
        } else {
            end = static_cast<uint64_t>(ofile.tellp());
        }
        ofile.close();
        return (end - start);
    }

    template < typename STR, typename OFSTREAM, template < class, class > class std_seq_t, class T, class std_allocator_t >
    uint64_t write_el_fw(const STR& file, const std_seq_t<T, std_allocator_t >& buffer, uint64_t elements, std::size_t buffer_offset, bool append, bool new_line)
    {
        OFSTREAM ofile(omni::string::to_string(file).c_str(), (append ? (std::ios::binary | std::ios::app) : std::ios::binary));
        uint64_t start = static_cast<uint64_t>(ofile.tellp());
        uint64_t end = start;
        typename std_seq_t<T, std_allocator_t >::const_iterator b;
        if (buffer_offset >= buffer.size()) {
            b = buffer.end();
        } else if (buffer_offset == 0) {
            b = buffer.begin();
        } else {
            b = buffer.begin() + buffer_offset;
        }
        if (elements == 0) {
            for (; b != buffer.end(); ++b) {
                ofile << *b;
            }
        } else {
            for (; b != buffer.end(); ++b) {
                ofile << *b;
                if (--elements == 0) { break; }
            }
        }
        if (new_line) { ofile << std::endl; }
        ofile.flush();
        end = static_cast<uint64_t>(ofile.tellp());
        ofile.close();
        elements = (end - start);
        if ((elements < sizeof(T)) && (elements > 0)) {
            return 1;
        } else if (elements % sizeof(T) != 0) {
            return (elements / sizeof(T)) + 1;
        }
        return elements / sizeof(T);
    }

    template < typename STR, typename OFSTREAM, typename T, std::size_t SZ >
    uint64_t write_el_fw(const STR& file, const T (&buffer)[SZ], uint64_t elements, std::size_t buffer_offset, bool append, bool new_line)
    {
        if (buffer_offset >= SZ) { return 0; }
        OFSTREAM ofile(omni::string::to_string(file).c_str(), (append ? (std::ios::binary | std::ios::app) : std::ios::binary));
        uint64_t start = static_cast<uint64_t>(ofile.tellp());
        uint64_t end = start;
        const T* buf = &buffer[buffer_offset];
        const typename OFSTREAM::char_type* bf = reinterpret_cast<const typename OFSTREAM::char_type*>(buf);
        std::streamsize ssz = static_cast<std::streamsize>(SZ - buffer_offset);
        if (ssz < 0) {
            elements = 0;
        } else if (static_cast<uint64_t>(ssz) < elements) {
            elements = static_cast<uint64_t>(ssz);
        }
        if (elements == 0) {
            ofile.write(bf, (ssz * sizeof(T)));
        } else {
            ofile.write(bf, (elements * sizeof(T)));
        }
        if (new_line) { ofile << std::endl; }
        ofile.flush();
        if ((ofile.rdstate() & OFSTREAM::failbit) != 0) {
            end = start;
            OMNI_ERR_FW("Error on write: failbit set", omni::exception("Error on write: failbit set"))
        } else if ((ofile.rdstate() & OFSTREAM::badbit) != 0) {
            end = start;
            OMNI_ERR_FW("Error on write: badbit set", omni::exception("Error on write: badbit set"))
        } else {
            end = static_cast<uint64_t>(ofile.tellp());
        }
        ofile.close();
        elements = (end - start);
        if ((elements < sizeof(T)) && (elements > 0)) {
            return 1;
        } else if (elements % sizeof(T) != 0) {
            return (elements / sizeof(T)) + 1;
        }
        return elements / sizeof(T);
    }

    template < typename STR, typename OFSTREAM, typename T, uint16_t SZ >
    uint64_t write_el_fw(const STR& file, const omni::stack_buffer<T, SZ>& buffer, uint64_t elements, uint16_t buffer_offset, bool append, bool new_line)
    {
        if (buffer_offset >= SZ) { return 0; }
        OFSTREAM ofile(omni::string::to_string(file).c_str(), (append ? (std::ios::binary | std::ios::app) : std::ios::binary));
        uint64_t start = static_cast<uint64_t>(ofile.tellp());
        uint64_t end = start;
        const T* buf = buffer.data() + buffer_offset;
        const typename OFSTREAM::char_type* bf = reinterpret_cast<const typename OFSTREAM::char_type*>(buf);
        std::streamsize ssz = static_cast<std::streamsize>(SZ - buffer_offset);
        if (ssz < 0) {
            elements = 0;
        } else if (static_cast<uint64_t>(ssz) < elements) {
            elements = static_cast<uint64_t>(ssz);
        }
        if (elements == 0) {
            ofile.write(bf, (ssz * sizeof(T)));
        } else {
            ofile.write(bf, (elements * sizeof(T)));
        }
        if (new_line) { ofile << std::endl; }
        ofile.flush();
        if ((ofile.rdstate() & OFSTREAM::failbit) != 0) {
            end = start;
            OMNI_ERR_FW("Error on write: failbit set", omni::exception("Error on write: failbit set"))
        } else if ((ofile.rdstate() & OFSTREAM::badbit) != 0) {
            end = start;
            OMNI_ERR_FW("Error on write: badbit set", omni::exception("Error on write: badbit set"))
        } else {
            end = static_cast<uint64_t>(ofile.tellp());
        }
        ofile.close();
        elements = (end - start);
        if ((elements < sizeof(T)) && (elements > 0)) {
            return 1;
        } else if (elements % sizeof(T) != 0) {
            return (elements / sizeof(T)) + 1;
        }
        return elements / sizeof(T);
    }

    template < typename STR, typename OFSTREAM, typename SEQ >
    uint64_t unsafe_write_el_fw(const STR& file, const SEQ* buffer, uint64_t elements, bool append)
    {
        if ((elements == 0) || (buffer == OMNI_NULL_PTR)) { return 0; }
        OFSTREAM ofile(omni::string::to_string(file).c_str(), (append ? (std::ios::binary | std::ios::app) : std::ios::binary));
        if (!ofile.is_open()) { return 0; }
        uint64_t start = static_cast<uint64_t>(ofile.tellp());
        uint64_t end = start;
        const typename OFSTREAM::char_type* bf = reinterpret_cast<const typename OFSTREAM::char_type*>(buffer);
        ofile.write(bf, static_cast<std::streamsize>(elements * sizeof(SEQ)));
        ofile.flush();
        if ((ofile.rdstate() & OFSTREAM::failbit) != 0) {
            end = start;
            OMNI_ERR_FW("Error on write: failbit set", omni::exception("Error on write: failbit set"))
        } else if ((ofile.rdstate() & OFSTREAM::badbit) != 0) {
            end = start;
            OMNI_ERR_FW("Error on write: badbit set", omni::exception("Error on write: badbit set"))
        } else {
            end = static_cast<uint64_t>(ofile.tellp());
        }
        ofile.close();
        elements = (end - start);
        if ((elements < sizeof(SEQ)) && (elements > 0)) {
            return 1;
        } else if (elements % sizeof(SEQ) != 0) {
            return (elements / sizeof(SEQ)) + 1;
        }
        return elements / sizeof(SEQ);
    }
}}}

namespace omni { namespace io { namespace dir_internal {
    inline std::string str_combine_path(const std::string& path1, const std::string& path2)
    {
        std::size_t psep = path1.find_last_of(OMNI_PATH_SEPARATOR);
        if (psep == path1.length()-1) { return path1 + path2; }
        return path1 + OMNI_PATH_SEPARATOR + path2;
    }
    inline std::wstring wstr_combine_path(const std::wstring& path1, const std::wstring& path2)
    {
        std::size_t psep = path1.find_last_of(OMNI_WPATH_SEPARATOR);
        if (psep == path1.length()-1) { return path1 + path2; }
        return path1 + OMNI_WPATH_SEPARATOR + path2;
    }

    #if defined(OMNI_OS_WIN)
        
    #else
        
    #endif

    #if defined(OMNI_OS_WIN)
        inline bool can_access(const std::string& path)
        {
            OMNI_FILE_CHECKA_FW(path, false)
            HANDLE hnd = ::CreateFileA(path.c_str(), FILE_TRAVERSE | SYNCHRONIZE,
                                     FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING,
                                     FILE_FLAG_BACKUP_SEMANTICS, NULL);
            if (hnd == INVALID_HANDLE_VALUE) {
                return false;
            }
            ::CloseHandle(hnd);
            return true;
        }

        inline bool can_access(const std::wstring& path)
        {
            std::wstring tf = OMNI_FILE_CHECKW_FW(tf, path, false)
            HANDLE hnd = ::CreateFileW(tf.c_str(), FILE_TRAVERSE | SYNCHRONIZE,
                                     FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING,
                                     FILE_FLAG_BACKUP_SEMANTICS, NULL);
            if (hnd == INVALID_HANDLE_VALUE) {
                return false;
            }
            ::CloseHandle(hnd);
            return true;
        }

        template < template < class, class > class std_seq_t, class T, class std_allocator_t >
        uint64_t get_dir_cont_win_fw(const std::string& name, std_seq_t< T, std_allocator_t >& dirs, int ftype)
        {
            std::string full = omni::io::dir_internal::str_combine_path(name, "*");
            #if defined(OMNI_WIN_API)
                OMNI_FILE_CHECKA_FW(full, dirs)
                WIN32_FIND_DATAA c_file;
                HANDLE hFile = ::FindFirstFileA(full.c_str(), &c_file);
                if (hFile != INVALID_HANDLE_VALUE) {
                    do {
                        std::string fname = std::string(c_file.cFileName);
                        if (fname != "." && fname != ".." ) { // do not add root dir
                            std::string file = omni::io::dir_internal::str_combine_path(name, fname);
                            if (ftype == 0) {
                                if (omni::io::dir_internal::exists(file)) { dirs.push_back(file); }
                            } else {
                                if (omni::io::file_internal::exists(file)) { dirs.push_back(file); }
                            }
                        }
                    } while (::FindNextFileA(hFile, &c_file));
                }
                ::FindClose(hFile);
            #else
                struct _finddata_t c_file;
                long hFile;
                if ((hFile = ::_findfirst(full.c_str(), &c_file)) != -1L) {
                    do {
                        std::string fname = std::string(c_file.name);
                        if (fname != "." && fname != ".." ) { // do not add root dir
                            std::string file = omni::io::dir_internal::str_combine_path(name, fname);
                            if (ftype == 0) {
                                if (omni::io::dir_internal::exists(file)) { dirs.push_back(file); }
                            } else {
                                if (omni::io::file_internal::exists(file)) { dirs.push_back(file); }
                            }
                        }
                    } while (::_findnext(hFile, &c_file) == 0);
                    ::_findclose(hFile);
                }
            #endif
            return dirs.size();
        }

        template < template < class, class > class std_seq_t, class T, class std_allocator_t >
        uint64_t get_dir_cont_win_fw(const std::wstring& name, std_seq_t< T, std_allocator_t >& dirs, int ftype)
        {
            std::wstring full = omni::io::dir_internal::wstr_combine_path(name, L"*");
            #if defined(OMNI_WIN_API)
                std::wstring tf = OMNI_FILE_CHECKW_FW(tf, full, false)
                WIN32_FIND_DATAW c_file;
                HANDLE hFile = ::FindFirstFileW(tf.c_str(), &c_file);
                if (hFile != INVALID_HANDLE_VALUE) {
                    do {
                        std::wstring fname = omni::string::to_wstring(c_file.cFileName);
                        if (fname != L"." && fname != L".." ) { // do not add root dir
                            std::wstring file = omni::io::dir_internal::wstr_combine_path(name, fname);
                            if (ftype == 0) {
                                if (omni::io::dir_internal::exists(file)) { dirs.push_back(file); }
                            } else {
                                if (omni::io::file_internal::exists(file)) { dirs.push_back(file); }
                            }
                        }
                    } while (::FindNextFileW(hFile, &c_file));
                }
                ::FindClose(hFile);
            #else
                struct _wfinddata_t c_file;
                long hFile;
                if ((hFile = ::_wfindfirst(full.c_str(), &c_file)) != -1L) {
                    do {
                        std::wstring fname = std::wstring(c_file.name);
                        if (fname != L"." && fname != L".." ) { // do not add root dir
                            std::wstring file = omni::io::dir_internal::wstr_combine_path(name, fname);
                            if (ftype == 0) {
                                if (omni::io::dir_internal::exists(file)) { dirs.push_back(file); }
                            } else {
                                if (omni::io::file_internal::exists(file)) { dirs.push_back(file); }
                            }
                        }
                    } while (::_wfindnext(hFile, &c_file) == 0);
                    ::_wfindclose(hFile);
                }
            #endif
            return dirs.size();
        }
    #else
        template < typename STR >
        inline bool can_access(const STR& path)
        {
            DIR *dp;
            if ((dp = ::opendir(omni::string::to_string(path).c_str())) == NULL) {
                if (errno == EACCES) { return false; }
            }
            ::closedir(dp);
            return true;
        }

        template < template < class, class > class std_seq_t, class T, class std_allocator_t >
        uint64_t get_dir_cont_nix_fw(const std::string& name, std_seq_t< T, std_allocator_t >& dirs, int ftype)
        {
            DIR *dp;
            struct dirent *dirp;
            if ((dp = ::opendir(name.c_str())) == NULL) {
                std::string er = "Error (";
                er += omni::string::util::to_string(errno) + "): " + std::strerror(errno);
                OMNI_ERRV_RETV_FW("Error opening file: ", errno, omni::exceptions::path_exception(er, name), dirs.size())
            }
            std::string fname, file;
            while ((dirp = ::readdir(dp)) != NULL) {
                fname = omni::string::util::lexical_cast<std::string>(dirp->d_name);
                if ((fname == ".") || (fname == "..")) { continue; }
                file = omni::io::dir_internal::str_combine_path(name, fname);
                if (ftype == 0) {
                    if (omni::io::dir_internal::exists(file)) { dirs.push_back(file); }
                } else {
                    if (omni::io::file_internal::exists(file)) { dirs.push_back(file); }
                }
            }
            ::closedir(dp);
            return dirs.size();
        }
        
        template < template < class, class > class std_seq_t, class T, class std_allocator_t >
        uint64_t get_dir_cont_nix_fw(const std::wstring& name, std_seq_t< T, std_allocator_t >& dirs, int ftype)
        {
            DIR *dp;
            struct dirent *dirp;
            std::string nm = omni::string::to_string(name);
            if ((dp = ::opendir(nm.c_str())) == NULL) {
                std::string er = "Error (";
                er += omni::string::util::to_string(errno) + "): " + std::strerror(errno);
                OMNI_ERRV_RETV_FW("Error opening file: ", errno, omni::exceptions::path_exception(er, nm), dirs.size())
            }
            std::wstring fname, file;
            while ((dirp = ::readdir(dp)) != NULL) {
                fname = omni::string::util::lexical_cast<std::wstring>(dirp->d_name);
                if ((fname == L".") || (fname == L"..")) { continue; }
                file = omni::io::dir_internal::wstr_combine_path(name, fname);
                if (ftype == 0) {
                    if (omni::io::dir_internal::exists(file)) { dirs.push_back(file); }
                } else {
                    if (omni::io::file_internal::exists(file)) { dirs.push_back(file); }
                }
            }
            ::closedir(dp);
            return dirs.size();
        }
    #endif

    template < typename STR >
    STR trim_trailing_slash(const STR& path)
    {
        std::size_t found = path.find_last_not_of(omni::string::util::lexical_cast<STR>("\\/").c_str());
        if (found != STR::npos) {
            return path.substr(0, found+1);
        }
        return path;
    }

    template < typename STR, template < class, class > class std_seq_t, class T, class std_allocator_t >
    uint64_t get_elements(const STR& folder, std_seq_t< T, std_allocator_t >& out, int ftype)
    {
        if (!omni::io::dir_internal::can_access(folder)) { return 0; }
        STR name = omni::io::dir_internal::trim_trailing_slash(folder);
        if (!omni::io::dir_internal::exists(name)) {
            OMNI_ERRV_RETV_FW("directory does not exist: ", omni::string::to_string_t(name), omni::exceptions::path_exception("Directory does not exist", omni::string::to_string(name)), 0)
        }
        #if defined(OMNI_OS_WIN)
            return get_dir_cont_win_fw(name, out, ftype);
        #else
            return get_dir_cont_nix_fw(name, out, ftype);
        #endif
    }

    template < typename STR, template < class, class > class std_seq_t, class T, class std_allocator_t >
    uint64_t get_directories(const STR& folder, std_seq_t< T, std_allocator_t >& out)
    {
        return omni::io::dir_internal::get_elements< STR, std_seq_t, T, std_allocator_t >(folder, out, 0);
    }

    template < typename STR, template < class, class > class std_seq_t, class T, class std_allocator_t >
    uint64_t get_files(const STR& folder, std_seq_t< T, std_allocator_t >& out)
    {
        return omni::io::dir_internal::get_elements< STR, std_seq_t, T, std_allocator_t >(folder, out, 1);
    }

    template < typename STR, template < class, class > class std_seq_t, class T, class std_allocator_t >
    uint64_t get_all_directories(const STR& folder, std_seq_t< T, std_allocator_t >& out)
    {
        if (!omni::io::dir_internal::can_access(folder)) { return 0; }
        std_seq_t< T, std_allocator_t > subs;
        omni::io::dir_internal::get_directories(folder, out);
        for (typename std_seq_t< T, std_allocator_t >::iterator dir = out.begin(); dir != out.end(); ++dir) {
            std_seq_t< T, std_allocator_t > sub;
            omni::io::dir_internal::get_all_directories(*dir, sub);
            subs.insert(subs.end(), sub.begin(), sub.end());
        }
        out.insert(out.end(), subs.begin(), subs.end());
        return out.size();
    }

    template < typename STR, template < class, class > class std_seq_t, class T, class std_allocator_t >
    uint64_t get_all_files(const STR& folder, std_seq_t< T, std_allocator_t >& out)
    {
        if (!omni::io::dir_internal::can_access(folder)) { return 0; }
        std_seq_t< T, std_allocator_t > dirs;
        omni::io::dir_internal::get_files(folder, out);
        omni::io::dir_internal::get_directories(folder, dirs);
        for (typename std_seq_t< T, std_allocator_t >::iterator dir = dirs.begin(); dir != dirs.end(); ++dir) {
            std_seq_t< T, std_allocator_t > sub;
            omni::io::dir_internal::get_all_files(*dir, sub);
            out.insert(out.end(), sub.begin(), sub.end());
        }
        return out.size();
    }
}}}
#undef OMNI_IO_FILE_RW_FW