summaryrefslogtreecommitdiff
path: root/indra/llcommon/llfile.cpp
blob: c3a0f0bfe0492d9a8e931f3dd5097bd47bf58fcc (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
/**
 * @file llfile.cpp
 * @author Michael Schlachter
 * @date 2006-03-23
 * @brief Implementation of cross-platform POSIX file buffer and c++
 * stream classes.
 *
 * $LicenseInfo:firstyear=2006&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#if LL_WINDOWS
#include <windows.h>
#include <stdlib.h>                 // Windows errno
#else
#include <errno.h>
#endif

#include "linden_common.h"
#include "llfile.h"
#include "llstring.h"
#include "llerror.h"
#include "stringize.h"

using namespace std;

static std::string empty;

// Many of the methods below use OS-level functions that mess with errno. Wrap
// variants of strerror() to report errors.

#if LL_WINDOWS
// On Windows, use strerror_s().
std::string strerr(int errn)
{
	char buffer[256];
	strerror_s(buffer, errn);       // infers sizeof(buffer) -- love it!
	return buffer;
}

typedef std::basic_ios<char,std::char_traits < char > > _Myios;

#else
// On Posix we want to call strerror_r(), but alarmingly, there are two
// different variants. The one that returns int always populates the passed
// buffer (except in case of error), whereas the other one always returns a
// valid char* but might or might not populate the passed buffer. How do we
// know which one we're getting? Define adapters for each and let the compiler
// select the applicable adapter.

// strerror_r() returns char*
std::string message_from(int /*orig_errno*/, const char* /*buffer*/, size_t /*bufflen*/,
						 const char* strerror_ret)
{
	return strerror_ret;
}

// strerror_r() returns int
std::string message_from(int orig_errno, const char* buffer, size_t bufflen,
						 int strerror_ret)
{
	if (strerror_ret == 0)
	{
		return buffer;
	}
	// Here strerror_r() has set errno. Since strerror_r() has already failed,
	// seems like a poor bet to call it again to diagnose its own error...
	int stre_errno = errno;
	if (stre_errno == ERANGE)
	{
		return STRINGIZE("strerror_r() can't explain errno " << orig_errno
						 << " (" << bufflen << "-byte buffer too small)");
	}
	if (stre_errno == EINVAL)
	{
		return STRINGIZE("unknown errno " << orig_errno);
	}
	// Here we don't even understand the errno from strerror_r()!
	return STRINGIZE("strerror_r() can't explain errno " << orig_errno
					 << " (error " << stre_errno << ')');
}

std::string strerr(int errn)
{
	char buffer[256];
	// Select message_from() function matching the strerror_r() we have on hand.
	return message_from(errn, buffer, sizeof(buffer),
						strerror_r(errn, buffer, sizeof(buffer)));
}
#endif	// ! LL_WINDOWS

// On either system, shorthand call just infers global 'errno'.
std::string strerr()
{
	return strerr(errno);
}

int warnif(const std::string& desc, const std::string& filename, int rc, int accept=0)
{
	if (rc < 0)
	{
		// Capture errno before we start emitting output
		int errn = errno;
		// For certain operations, a particular errno value might be
		// acceptable -- e.g. stat() could permit ENOENT, mkdir() could permit
		// EEXIST. Don't warn if caller explicitly says this errno is okay.
		if (errn != accept)
		{
			LL_WARNS("LLFile") << "Couldn't " << desc << " '" << filename
							   << "' (errno " << errn << "): " << strerr(errn) << LL_ENDL;
		}
#if 0 && LL_WINDOWS                 // turn on to debug file-locking problems
		// If the problem is "Permission denied," maybe it's because another
		// process has the file open. Try to find out.
		if (errn == EACCES)         // *not* EPERM
		{
			// Only do any of this stuff (before LL_ENDL) if it will be logged.
			LL_DEBUGS("LLFile") << empty;
			const char* TEMP = getenv("TEMP");
			if (! TEMP)
			{
				LL_CONT << "No $TEMP, not running 'handle'";
			}
			else
			{
				std::string tf(TEMP);
				tf += "\\handle.tmp";
				// http://technet.microsoft.com/en-us/sysinternals/bb896655
				std::string cmd(STRINGIZE("handle \"" << filename
										  // "openfiles /query /v | fgrep -i \"" << filename
										  << "\" > \"" << tf << '"'));
				LL_CONT << cmd;
				if (system(cmd.c_str()) != 0)
				{
					LL_CONT << "\nDownload 'handle.exe' from http://technet.microsoft.com/en-us/sysinternals/bb896655";
				}
				else
				{
					std::ifstream inf(tf);
					std::string line;
					while (std::getline(inf, line))
					{
						LL_CONT << '\n' << line;
					}
				}
				LLFile::remove(tf);
			}
			LL_CONT << LL_ENDL;
		}
#endif  // LL_WINDOWS hack to identify processes holding file open
	}
	return rc;
}

// static
int	LLFile::mkdir(const std::string& dirname, int perms)
{
#if LL_WINDOWS
	// permissions are ignored on Windows
	std::string utf8dirname = dirname;
	llutf16string utf16dirname = utf8str_to_utf16str(utf8dirname);
	int rc = _wmkdir(utf16dirname.c_str());
#else
	int rc = ::mkdir(dirname.c_str(), (mode_t)perms);
#endif
	// We often use mkdir() to ensure the existence of a directory that might
	// already exist. Don't spam the log if it does.
	return warnif("mkdir", dirname, rc, EEXIST);
}

// static
int	LLFile::rmdir(const std::string& dirname)
{
#if LL_WINDOWS
	// permissions are ignored on Windows
	std::string utf8dirname = dirname;
	llutf16string utf16dirname = utf8str_to_utf16str(utf8dirname);
	int rc = _wrmdir(utf16dirname.c_str());
#else
	int rc = ::rmdir(dirname.c_str());
#endif
	return warnif("rmdir", dirname, rc);
}

// static
LLFILE*	LLFile::fopen(const std::string& filename, const char* mode)	/* Flawfinder: ignore */
{
#if	LL_WINDOWS
	std::string utf8filename = filename;
	std::string utf8mode = std::string(mode);
	llutf16string utf16filename = utf8str_to_utf16str(utf8filename);
	llutf16string utf16mode = utf8str_to_utf16str(utf8mode);
	return _wfopen(utf16filename.c_str(),utf16mode.c_str());
#else
	return ::fopen(filename.c_str(),mode);	/* Flawfinder: ignore */
#endif
}

LLFILE*	LLFile::_fsopen(const std::string& filename, const char* mode, int sharingFlag)
{
#if	LL_WINDOWS
	std::string utf8filename = filename;
	std::string utf8mode = std::string(mode);
	llutf16string utf16filename = utf8str_to_utf16str(utf8filename);
	llutf16string utf16mode = utf8str_to_utf16str(utf8mode);
	return _wfsopen(utf16filename.c_str(),utf16mode.c_str(),sharingFlag);
#else
	llassert(0);//No corresponding function on non-windows
	return NULL;
#endif
}

int	LLFile::close(LLFILE * file)
{
	int ret_value = 0;
	if (file)
	{
		ret_value = fclose(file);
	}
	return ret_value;
}


int	LLFile::remove(const std::string& filename)
{
#if	LL_WINDOWS
	std::string utf8filename = filename;
	llutf16string utf16filename = utf8str_to_utf16str(utf8filename);
	int rc = _wremove(utf16filename.c_str());
#else
	int rc = ::remove(filename.c_str());
#endif
	return warnif("remove", filename, rc);
}

int	LLFile::rename(const std::string& filename, const std::string& newname)
{
#if	LL_WINDOWS
	std::string utf8filename = filename;
	std::string utf8newname = newname;
	llutf16string utf16filename = utf8str_to_utf16str(utf8filename);
	llutf16string utf16newname = utf8str_to_utf16str(utf8newname);
	int rc = _wrename(utf16filename.c_str(),utf16newname.c_str());
#else
	int rc = ::rename(filename.c_str(),newname.c_str());
#endif
	return warnif(STRINGIZE("rename to '" << newname << "' from"), filename, rc);
}

int	LLFile::stat(const std::string& filename, llstat* filestatus)
{
#if LL_WINDOWS
	std::string utf8filename = filename;
	llutf16string utf16filename = utf8str_to_utf16str(utf8filename);
	int rc = _wstat(utf16filename.c_str(),filestatus);
#else
	int rc = ::stat(filename.c_str(),filestatus);
#endif
	// We use stat() to determine existence (see isfile(), isdir()).
	// Don't spam the log if the subject pathname doesn't exist.
	return warnif("stat", filename, rc, ENOENT);
}

bool LLFile::isdir(const std::string& filename)
{
	llstat st;

	return stat(filename, &st) == 0 && S_ISDIR(st.st_mode);
}

bool LLFile::isfile(const std::string& filename)
{
	llstat st;

	return stat(filename, &st) == 0 && S_ISREG(st.st_mode);
}

const char *LLFile::tmpdir()
{
	static std::string utf8path;

	if (utf8path.empty())
	{
		char sep;
#if LL_WINDOWS
		sep = '\\';

		DWORD len = GetTempPathW(0, L"");
		llutf16string utf16path;
		utf16path.resize(len + 1);
		len = GetTempPathW(static_cast<DWORD>(utf16path.size()), &utf16path[0]);
		utf8path = utf16str_to_utf8str(utf16path);
#else
		sep = '/';

		char *env = getenv("TMPDIR");

		utf8path = env ? env : "/tmp/";
#endif
		if (utf8path[utf8path.size() - 1] != sep)
		{
			utf8path += sep;
		}
	}
	return utf8path.c_str();
}


/***************** Modified file stream created to overcome the incorrect behaviour of posix fopen in windows *******************/

#if LL_WINDOWS

LLFILE *	LLFile::_Fiopen(const std::string& filename, 
		std::ios::openmode mode)
{	// open a file
	static const char *mods[] =
	{	// fopen mode strings corresponding to valid[i]
	"r", "w", "w", "a", "rb", "wb", "wb", "ab",
	"r+", "w+", "a+", "r+b", "w+b", "a+b",
	0};
	static const int valid[] =
	{	// valid combinations of open flags
		ios_base::in,
		ios_base::out,
		ios_base::out | ios_base::trunc,
		ios_base::out | ios_base::app,
		ios_base::in | ios_base::binary,
		ios_base::out | ios_base::binary,
		ios_base::out | ios_base::trunc | ios_base::binary,
		ios_base::out | ios_base::app | ios_base::binary,
		ios_base::in | ios_base::out,
		ios_base::in | ios_base::out | ios_base::trunc,
		ios_base::in | ios_base::out | ios_base::app,
		ios_base::in | ios_base::out | ios_base::binary,
		ios_base::in | ios_base::out | ios_base::trunc
			| ios_base::binary,
		ios_base::in | ios_base::out | ios_base::app
			| ios_base::binary,
	0};

	LLFILE *fp = 0;
	int n;
	ios_base::openmode atendflag = mode & ios_base::ate;
	ios_base::openmode norepflag = mode & ios_base::_Noreplace;

	if (mode & ios_base::_Nocreate)
		mode |= ios_base::in;	// file must exist
	mode &= ~(ios_base::ate | ios_base::_Nocreate | ios_base::_Noreplace);
	for (n = 0; valid[n] != 0 && valid[n] != mode; ++n)
		;	// look for a valid mode

	if (valid[n] == 0)
		return (0);	// no valid mode
	else if (norepflag && mode & (ios_base::out || ios_base::app)
		&& (fp = LLFile::fopen(filename, "r")) != 0)	/* Flawfinder: ignore */
		{	// file must not exist, close and fail
		fclose(fp);
		return (0);
		}
	else if (fp != 0 && fclose(fp) != 0)
		return (0);	// can't close after test open
// should open with protection here, if other than default
	else if ((fp = LLFile::fopen(filename, mods[n])) == 0)	/* Flawfinder: ignore */
		return (0);	// open failed

	if (!atendflag || fseek(fp, 0, SEEK_END) == 0)
		return (fp);	// no need to seek to end, or seek succeeded

	fclose(fp);	// can't position at end
	return (0);
}

#endif /* LL_WINDOWS */

/************** llstdio file buffer ********************************/


//llstdio_filebuf* llstdio_filebuf::open(const char *_Filename,
//	ios_base::openmode _Mode)
//{
//#if LL_WINDOWS
//	_Filet *_File;
//	if (is_open() || (_File = LLFILE::_Fiopen(_Filename, _Mode)) == 0)
//		return (0);	// open failed
//
//	_Init(_File, _Openfl);
//	_Initcvt(&_USE(_Mysb::getloc(), _Cvt));
//	return (this);	// open succeeded
//#else
//	std::filebuf* _file = std::filebuf::open(_Filename, _Mode);
//	if (NULL == _file) return NULL;
//	return this;
//#endif
//}


// *TODO: Seek the underlying c stream for better cross-platform compatibility?
#if !LL_WINDOWS
llstdio_filebuf::int_type llstdio_filebuf::overflow(llstdio_filebuf::int_type __c)
{
	int_type __ret = traits_type::eof();
	const bool __testeof = traits_type::eq_int_type(__c, __ret);
	const bool __testout = _M_mode & ios_base::out;
	if (__testout && !_M_reading)
	{
		if (this->pbase() < this->pptr())
		{
			// If appropriate, append the overflow char.
			if (!__testeof)
			{
				*this->pptr() = traits_type::to_char_type(__c);
				this->pbump(1);
			}

			// Convert pending sequence to external representation,
			// and output.
			if (_convert_to_external(this->pbase(),
					 this->pptr() - this->pbase()))
			{
				_M_set_buffer(0);
				__ret = traits_type::not_eof(__c);
			}
	}
		else if (_M_buf_size > 1)
		{
			// Overflow in 'uncommitted' mode: set _M_writing, set
			// the buffer to the initial 'write' mode, and put __c
			// into the buffer.
			_M_set_buffer(0);
			_M_writing = true;
			if (!__testeof)
			{
				*this->pptr() = traits_type::to_char_type(__c);
				this->pbump(1);
			}
			__ret = traits_type::not_eof(__c);
		}
		else
		{
			// Unbuffered.
			char_type __conv = traits_type::to_char_type(__c);
			if (__testeof || _convert_to_external(&__conv, 1))
			{
				_M_writing = true;
				__ret = traits_type::not_eof(__c);
			}
		}
	}
	return __ret;
}

bool llstdio_filebuf::_convert_to_external(char_type* __ibuf,
						std::streamsize __ilen)
{
	// Sizes of external and pending output.
	streamsize __elen;
	streamsize __plen;
	if (__check_facet(_M_codecvt).always_noconv())
	{
		//__elen = _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
		__elen = fwrite(reinterpret_cast<void*>(__ibuf), 1,
						__ilen, _M_file.file());
		__plen = __ilen;
	}
	else
	{
		// Worst-case number of external bytes needed.
		// XXX Not done encoding() == -1.
		streamsize __blen = __ilen * _M_codecvt->max_length();
		char* __buf = static_cast<char*>(__builtin_alloca(__blen));

		char* __bend;
		const char_type* __iend;
		codecvt_base::result __r;
		__r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen,
				__iend, __buf, __buf + __blen, __bend);

		if (__r == codecvt_base::ok || __r == codecvt_base::partial)
			__blen = __bend - __buf;
		else if (__r == codecvt_base::noconv)
	{
			// Same as the always_noconv case above.
			__buf = reinterpret_cast<char*>(__ibuf);
			__blen = __ilen;
	}
		else
			__throw_ios_failure(__N("llstdio_filebuf::_convert_to_external "
									"conversion error"));
  
		//__elen = _M_file.xsputn(__buf, __blen);
		__elen = fwrite(__buf, 1, __blen, _M_file.file());
		__plen = __blen;

		// Try once more for partial conversions.
		if (__r == codecvt_base::partial && __elen == __plen)
		{
			const char_type* __iresume = __iend;
			streamsize __rlen = this->pptr() - __iend;
			__r = _M_codecvt->out(_M_state_cur, __iresume,
					__iresume + __rlen, __iend, __buf,
					__buf + __blen, __bend);
			if (__r != codecvt_base::error)
			{
				__rlen = __bend - __buf;
				//__elen = _M_file.xsputn(__buf, __rlen);
				__elen = fwrite(__buf, 1, __rlen, _M_file.file());
				__plen = __rlen;
			}
			else
			{
				__throw_ios_failure(__N("llstdio_filebuf::_convert_to_external "
										"conversion error"));
			}
		}
	}
	return __elen == __plen;
}

llstdio_filebuf::int_type llstdio_filebuf::underflow()
{
	int_type __ret = traits_type::eof();
	const bool __testin = _M_mode & ios_base::in;
	if (__testin)
	{
		if (_M_writing)
		{
			if (overflow() == traits_type::eof())
			return __ret;
			//_M_set_buffer(-1);
			//_M_writing = false;
		}
		// Check for pback madness, and if so switch back to the
		// normal buffers and jet outta here before expensive
		// fileops happen...
		_M_destroy_pback();

		if (this->gptr() < this->egptr())
			return traits_type::to_int_type(*this->gptr());

		// Get and convert input sequence.
		const size_t __buflen = _M_buf_size > 1 ? _M_buf_size - 1 : 1;

		// Will be set to true if ::fread() returns 0 indicating EOF.
		bool __got_eof = false;
		// Number of internal characters produced.
		streamsize __ilen = 0;
		codecvt_base::result __r = codecvt_base::ok;
		if (__check_facet(_M_codecvt).always_noconv())
		{
			//__ilen = _M_file.xsgetn(reinterpret_cast<char*>(this->eback()),
			//			__buflen);
			__ilen = fread(reinterpret_cast<void*>(this->eback()), 1,
						__buflen, _M_file.file());
			if (__ilen == 0)
				__got_eof = true;
		}
		else
	    {
			// Worst-case number of external bytes.
			// XXX Not done encoding() == -1.
			const int __enc = _M_codecvt->encoding();
			streamsize __blen; // Minimum buffer size.
			streamsize __rlen; // Number of chars to read.
			if (__enc > 0)
				__blen = __rlen = __buflen * __enc;
			else
			{
				__blen = __buflen + _M_codecvt->max_length() - 1;
				__rlen = __buflen;
			}
			const streamsize __remainder = _M_ext_end - _M_ext_next;
			__rlen = __rlen > __remainder ? __rlen - __remainder : 0;

			// An imbue in 'read' mode implies first converting the external
			// chars already present.
			if (_M_reading && this->egptr() == this->eback() && __remainder)
				__rlen = 0;

			// Allocate buffer if necessary and move unconverted
			// bytes to front.
			if (_M_ext_buf_size < __blen)
			{
				char* __buf = new char[__blen];
				if (__remainder)
					__builtin_memcpy(__buf, _M_ext_next, __remainder);

				delete [] _M_ext_buf;
				_M_ext_buf = __buf;
				_M_ext_buf_size = __blen;
			}
			else if (__remainder)
				__builtin_memmove(_M_ext_buf, _M_ext_next, __remainder);

			_M_ext_next = _M_ext_buf;
			_M_ext_end = _M_ext_buf + __remainder;
			_M_state_last = _M_state_cur;

			do
			{
				if (__rlen > 0)
				{
					// Sanity check!
					// This may fail if the return value of
					// codecvt::max_length() is bogus.
					if (_M_ext_end - _M_ext_buf + __rlen > _M_ext_buf_size)
					{
						__throw_ios_failure(__N("llstdio_filebuf::underflow "
							"codecvt::max_length() "
							"is not valid"));
					}
					//streamsize __elen = _M_file.xsgetn(_M_ext_end, __rlen);
					streamsize __elen = fread(_M_ext_end, 1,
						__rlen, _M_file.file());
					if (__elen == 0)
						__got_eof = true;
					else if (__elen == -1)
					break;
					//_M_ext_end += __elen;
				}

				char_type* __iend = this->eback();
				if (_M_ext_next < _M_ext_end)
				{
					__r = _M_codecvt->in(_M_state_cur, _M_ext_next,
							_M_ext_end, _M_ext_next,
							this->eback(),
							this->eback() + __buflen, __iend);
}
				if (__r == codecvt_base::noconv)
{
					size_t __avail = _M_ext_end - _M_ext_buf;
					__ilen = std::min(__avail, __buflen);
					traits_type::copy(this->eback(),
						reinterpret_cast<char_type*>
						(_M_ext_buf), __ilen);
					_M_ext_next = _M_ext_buf + __ilen;
				}
				else
					__ilen = __iend - this->eback();

				// _M_codecvt->in may return error while __ilen > 0: this is
				// ok, and actually occurs in case of mixed encodings (e.g.,
				// XML files).
				if (__r == codecvt_base::error)
					break;

				__rlen = 1;
			} while (__ilen == 0 && !__got_eof);
		}

		if (__ilen > 0)
		{
			_M_set_buffer(__ilen);
			_M_reading = true;
			__ret = traits_type::to_int_type(*this->gptr());
		}
		else if (__got_eof)
		{
			// If the actual end of file is reached, set 'uncommitted'
			// mode, thus allowing an immediate write without an
			// intervening seek.
			_M_set_buffer(-1);
			_M_reading = false;
			// However, reaching it while looping on partial means that
			// the file has got an incomplete character.
			if (__r == codecvt_base::partial)
				__throw_ios_failure(__N("llstdio_filebuf::underflow "
					"incomplete character in file"));
		}
		else if (__r == codecvt_base::error)
			__throw_ios_failure(__N("llstdio_filebuf::underflow "
					"invalid byte sequence in file"));
		else
			__throw_ios_failure(__N("llstdio_filebuf::underflow "
					"error reading the file"));
	}
	return __ret;
}

std::streamsize llstdio_filebuf::xsgetn(char_type* __s, std::streamsize __n)
{
	// Clear out pback buffer before going on to the real deal...
	streamsize __ret = 0;
	if (_M_pback_init)
	{
		if (__n > 0 && this->gptr() == this->eback())
		{
			*__s++ = *this->gptr();
			this->gbump(1);
			__ret = 1;
			--__n;
		}
		_M_destroy_pback();
	}
       
	// Optimization in the always_noconv() case, to be generalized in the
	// future: when __n > __buflen we read directly instead of using the
	// buffer repeatedly.
	const bool __testin = _M_mode & ios_base::in;
	const streamsize __buflen = _M_buf_size > 1 ? _M_buf_size - 1 : 1;

	if (__n > __buflen && __check_facet(_M_codecvt).always_noconv()
		&& __testin && !_M_writing)
	{
		// First, copy the chars already present in the buffer.
		const streamsize __avail = this->egptr() - this->gptr();
		if (__avail != 0)
		{
			if (__avail == 1)
				*__s = *this->gptr();
			else
				traits_type::copy(__s, this->gptr(), __avail);
			__s += __avail;
			this->gbump(__avail);
			__ret += __avail;
			__n -= __avail;
		}

		// Need to loop in case of short reads (relatively common
		// with pipes).
		streamsize __len;
		for (;;)
		{
			//__len = _M_file.xsgetn(reinterpret_cast<char*>(__s), __n);
			__len = fread(reinterpret_cast<void*>(__s), 1, 
						__n, _M_file.file());
			if (__len == -1)
				__throw_ios_failure(__N("llstdio_filebuf::xsgetn "
										"error reading the file"));
			if (__len == 0)
				break;

			__n -= __len;
			__ret += __len;
			if (__n == 0)
				break;

			__s += __len;
		}

		if (__n == 0)
		{
			_M_set_buffer(0);
			_M_reading = true;
		}
		else if (__len == 0)
		{
			// If end of file is reached, set 'uncommitted'
			// mode, thus allowing an immediate write without
			// an intervening seek.
			_M_set_buffer(-1);
			_M_reading = false;
		}
	}
	else
		__ret += __streambuf_type::xsgetn(__s, __n);

	return __ret;
}

std::streamsize llstdio_filebuf::xsputn(char_type* __s, std::streamsize __n)
{
	// Optimization in the always_noconv() case, to be generalized in the
	// future: when __n is sufficiently large we write directly instead of
	// using the buffer.
	streamsize __ret = 0;
	const bool __testout = _M_mode & ios_base::out;
	if (__check_facet(_M_codecvt).always_noconv()
		&& __testout && !_M_reading)
	{
		// Measurement would reveal the best choice.
		const streamsize __chunk = 1ul << 10;
		streamsize __bufavail = this->epptr() - this->pptr();

		// Don't mistake 'uncommitted' mode buffered with unbuffered.
		if (!_M_writing && _M_buf_size > 1)
			__bufavail = _M_buf_size - 1;

		const streamsize __limit = std::min(__chunk, __bufavail);
		if (__n >= __limit)
		{
			const streamsize __buffill = this->pptr() - this->pbase();
			const char* __buf = reinterpret_cast<const char*>(this->pbase());
			//__ret = _M_file.xsputn_2(__buf, __buffill,
			//			reinterpret_cast<const char*>(__s), __n);
			if (__buffill)
			{
				__ret = fwrite(__buf, 1, __buffill, _M_file.file());
			}
			if (__ret == __buffill)
	{
				__ret += fwrite(reinterpret_cast<const char*>(__s), 1,
								__n, _M_file.file());
	}
			if (__ret == __buffill + __n)
			{
				_M_set_buffer(0);
				_M_writing = true;
}
			if (__ret > __buffill)
				__ret -= __buffill;
			else
				__ret = 0;
		}
		else
			__ret = __streambuf_type::xsputn(__s, __n);
	}
	else
		__ret = __streambuf_type::xsputn(__s, __n);
    return __ret;
}

int llstdio_filebuf::sync()
{
	return (_M_file.sync() == 0 ? 0 : -1);
}
#endif

/************** input file stream ********************************/


llifstream::llifstream() : _M_filebuf(),
#if LL_WINDOWS
	std::istream(&_M_filebuf) {}
#else
	std::istream()
{
	this->init(&_M_filebuf);
}
#endif

// explicit
llifstream::llifstream(const std::string& _Filename,
		ios_base::openmode _Mode) : _M_filebuf(),
#if LL_WINDOWS
	std::istream(&_M_filebuf)
{
	llutf16string wideName = utf8str_to_utf16str( _Filename );
	if (_M_filebuf.open(wideName.c_str(), _Mode | ios_base::in) == 0)
	{
		_Myios::setstate(ios_base::failbit);
	}
}
#else
	std::istream()
{
	this->init(&_M_filebuf);
	this->open(_Filename.c_str(), _Mode | ios_base::in);
}
#endif

// explicit
llifstream::llifstream(const char* _Filename, 
		ios_base::openmode _Mode) : _M_filebuf(),
#if LL_WINDOWS
	std::istream(&_M_filebuf)
{
	llutf16string wideName = utf8str_to_utf16str( _Filename );
	if (_M_filebuf.open(wideName.c_str(), _Mode | ios_base::in) == 0)
	{
		_Myios::setstate(ios_base::failbit);
}
}
#else
	std::istream()
{
	this->init(&_M_filebuf);
	this->open(_Filename, _Mode | ios_base::in);
}
#endif


// explicit
llifstream::llifstream(_Filet *_File,
		ios_base::openmode _Mode, size_t _Size) :
	_M_filebuf(_File, _Mode, _Size),
#if LL_WINDOWS
	std::istream(&_M_filebuf) {}
#else
	std::istream()
{
	this->init(&_M_filebuf);
}
#endif

#if !LL_WINDOWS
// explicit
llifstream::llifstream(int __fd,
		ios_base::openmode _Mode, size_t _Size) :
	_M_filebuf(__fd, _Mode, _Size),
	std::istream()
{
	this->init(&_M_filebuf);
}
#endif

bool llifstream::is_open() const
{	// test if C stream has been opened
	return _M_filebuf.is_open();
}

void llifstream::open(const char* _Filename, ios_base::openmode _Mode)
{	// open a C stream with specified mode

#if LL_WINDOWS
	llutf16string wideName = utf8str_to_utf16str( _Filename );
	if (_M_filebuf.open( wideName.c_str(), _Mode | ios_base::in) == 0)
	{
		_Myios::setstate(ios_base::failbit);
	}
	else
	{
		_Myios::clear();
	}
#else
	if (_M_filebuf.open(_Filename, _Mode | ios_base::in) == 0)
	{
		this->setstate(ios_base::failbit);
	}
	else
	{
		this->clear();
	}
#endif
}

void llifstream::close()
{	// close the C stream
	if (_M_filebuf.close() == 0)
	{
#if LL_WINDOWS
		_Myios::setstate(ios_base::failbit);
#else
		this->setstate(ios_base::failbit);
#endif
		}
	}


/************** output file stream ********************************/


llofstream::llofstream() : _M_filebuf(),
#if LL_WINDOWS
	std::ostream(&_M_filebuf) {}
#else
	std::ostream()
{
	this->init(&_M_filebuf);
}
#endif

// explicit
llofstream::llofstream(const std::string& _Filename,
		ios_base::openmode _Mode) : _M_filebuf(),
#if LL_WINDOWS
	std::ostream(&_M_filebuf)
{
	llutf16string wideName = utf8str_to_utf16str( _Filename );
	if (_M_filebuf.open( wideName.c_str(), _Mode | ios_base::out) == 0)
	{
		_Myios::setstate(ios_base::failbit);
	}
}
#else
	std::ostream()
{
	this->init(&_M_filebuf);
	this->open(_Filename.c_str(), _Mode | ios_base::out);
}
#endif

// explicit
llofstream::llofstream(const char* _Filename,
		ios_base::openmode _Mode) : _M_filebuf(),
#if LL_WINDOWS
	std::ostream(&_M_filebuf)
{
	llutf16string wideName = utf8str_to_utf16str( _Filename );
	if (_M_filebuf.open( wideName.c_str(), _Mode | ios_base::out) == 0)
	{
		_Myios::setstate(ios_base::failbit);
	}
}
#else
	std::ostream()
{
	this->init(&_M_filebuf);
	this->open(_Filename, _Mode | ios_base::out);
}
#endif

// explicit
llofstream::llofstream(_Filet *_File,
			ios_base::openmode _Mode, size_t _Size) :
	_M_filebuf(_File, _Mode, _Size),
#if LL_WINDOWS
	std::ostream(&_M_filebuf) {}
#else
	std::ostream()
{
	this->init(&_M_filebuf);
}
#endif

#if !LL_WINDOWS
// explicit
llofstream::llofstream(int __fd,
			ios_base::openmode _Mode, size_t _Size) :
	_M_filebuf(__fd, _Mode, _Size),
	std::ostream()
{
	this->init(&_M_filebuf);
}
#endif

bool llofstream::is_open() const
{	// test if C stream has been opened
	return _M_filebuf.is_open();
}

void llofstream::open(const char* _Filename, ios_base::openmode _Mode)
{	// open a C stream with specified mode
#if LL_WINDOWS
	llutf16string wideName = utf8str_to_utf16str( _Filename );
	if (_M_filebuf.open( wideName.c_str(), _Mode | ios_base::out) == 0)
{
		_Myios::setstate(ios_base::failbit);
	}
	else
	{
		_Myios::clear();
	}
#else
	if (_M_filebuf.open(_Filename, _Mode | ios_base::out) == 0)
	{
		this->setstate(ios_base::failbit);
	}
	else
	{
		this->clear();
	}
#endif
}

void llofstream::close()
{	// close the C stream
	if (_M_filebuf.close() == 0)
	{
#if LL_WINDOWS
		_Myios::setstate(ios_base::failbit);
#else
		this->setstate(ios_base::failbit);
#endif
	}
}

/************** helper functions ********************************/

std::streamsize llifstream_size(llifstream& ifstr)
{
	if(!ifstr.is_open()) return 0;
	std::streampos pos_old = ifstr.tellg();
	ifstr.seekg(0, ios_base::beg);
	std::streampos pos_beg = ifstr.tellg();
	ifstr.seekg(0, ios_base::end);
	std::streampos pos_end = ifstr.tellg();
	ifstr.seekg(pos_old, ios_base::beg);
	return pos_end - pos_beg;
}

std::streamsize llofstream_size(llofstream& ofstr)
{
	if(!ofstr.is_open()) return 0;
	std::streampos pos_old = ofstr.tellp();
	ofstr.seekp(0, ios_base::beg);
	std::streampos pos_beg = ofstr.tellp();
	ofstr.seekp(0, ios_base::end);
	std::streampos pos_end = ofstr.tellp();
	ofstr.seekp(pos_old, ios_base::beg);
	return pos_end - pos_beg;
}