comparison foosdk/sdk/foobar2000/helpers/text_file_loader.cpp @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #include "StdAfx.h"
2
3 #include "text_file_loader.h"
4
5 #include <pfc/string_conv.h>
6
7 static const unsigned char utf8_header[3] = {0xEF,0xBB,0xBF};
8
9 namespace text_file_loader
10 {
11 void write(const service_ptr_t<file> & p_file,abort_callback & p_abort,const char * p_string,bool is_utf8)
12 {
13 p_file->seek(0,p_abort);
14 p_file->set_eof(p_abort);
15 if (is_utf8)
16 {
17 p_file->write_object(utf8_header,sizeof(utf8_header),p_abort);
18 p_file->write_object(p_string,strlen(p_string),p_abort);
19 }
20 else
21 {
22 pfc::stringcvt::string_ansi_from_utf8 bah(p_string);
23 p_file->write_object(bah,bah.length(),p_abort);
24 }
25 }
26 void read(const service_ptr_t<file> & p_file, abort_callback & p_abort, pfc::string_base & p_out, bool & is_utf8) {
27 read_v2( p_file, p_abort, p_out, is_utf8, false );
28 }
29 void read_v2(const service_ptr_t<file> & p_file,abort_callback & p_abort,pfc::string_base & p_out,bool & is_utf8, bool forceUTF8) {
30 p_out.reset();
31 p_file->reopen( p_abort );
32
33 pfc::array_t<char> mem;
34 t_filesize size64;
35 size64 = p_file->get_size(p_abort);
36 if (size64 == filesize_invalid)//typically HTTP
37 {
38 pfc::string8 ansitemp;
39 t_size done;
40 enum { delta = 1024 * 64, max = 1024 * 512 };
41
42 is_utf8 = forceUTF8;
43 char temp[3];
44 done = p_file->read(temp, 3, p_abort);
45 if (done != 3)
46 {
47 if (done > 0) {
48 if ( is_utf8 ) {
49 p_out.set_string( temp, done );
50 } else {
51 p_out = pfc::stringcvt::string_utf8_from_ansi(temp, done);
52 }
53
54 }
55 return;
56 }
57 if (!memcmp(utf8_header, temp, 3)) is_utf8 = true;
58 else if (is_utf8) p_out.add_string(temp,3);
59 else ansitemp.add_string(temp, 3);
60
61 mem.set_size(delta);
62
63 for(;;)
64 {
65 done = p_file->read(mem.get_ptr(),delta,p_abort);
66 if (done > 0)
67 {
68 if (is_utf8) p_out.add_string(mem.get_ptr(),done);
69 else ansitemp.add_string(mem.get_ptr(),done);
70 }
71 if (done < delta) break;
72 }
73
74 if (!is_utf8)
75 {
76 p_out = pfc::stringcvt::string_utf8_from_ansi(ansitemp);
77 }
78
79 return;
80 }
81 else
82 {
83 if (size64 > hardlimit_bytes) throw exception_io_data();//hard limit
84 t_size size = pfc::downcast_guarded<t_size>(size64);
85 mem.set_size(size+1);
86 char * asdf = mem.get_ptr();
87 p_file->read_object(asdf,size,p_abort);
88 asdf[size]=0;
89 if (size>=3 && !memcmp(utf8_header,asdf,3)) {
90 is_utf8 = true;
91 p_out.add_string(asdf+3);
92 } else if (forceUTF8) {
93 is_utf8 = true;
94 p_out = asdf;
95 } else {
96 is_utf8 = false;
97 p_out = pfc::stringcvt::string_utf8_from_ansi(asdf);
98 }
99 return;
100 }
101 }
102
103 void write(const char * p_path,abort_callback & p_abort,const char * p_string,bool is_utf8)
104 {
105 service_ptr_t<file> f;
106 filesystem::g_open_write_new(f,p_path,p_abort);
107 write(f,p_abort,p_string,is_utf8);
108 }
109
110 void read(const char * p_path, abort_callback & p_abort, pfc::string_base & p_out, bool & is_utf8) {
111 read_v2( p_path, p_abort, p_out, is_utf8, false );
112 }
113 void read_v2(const char * p_path,abort_callback & p_abort,pfc::string_base & p_out,bool & is_utf8, bool forceUTF8)
114 {
115 service_ptr_t<file> f;
116 filesystem::g_open_read(f,p_path,p_abort);
117 read_v2(f,p_abort,p_out,is_utf8,forceUTF8);
118 }
119
120 }