foobar2000 SDK  2015-08-03
Functions
text_file_loader Namespace Reference

Functions

void read (const service_ptr_t< file > &p_file, abort_callback &p_abort, pfc::string_base &p_out, bool &is_utf8)
 
void read (const char *p_path, abort_callback &p_abort, pfc::string_base &p_out, bool &is_utf8)
 
void write (const service_ptr_t< file > &p_file, abort_callback &p_abort, const char *p_string, bool is_utf8)
 
void write (const char *p_path, abort_callback &p_abort, const char *p_string, bool is_utf8)
 

Function Documentation

void text_file_loader::read ( const service_ptr_t< file > &  p_file,
abort_callback p_abort,
pfc::string_base p_out,
bool &  is_utf8 
)

Definition at line 29 of file text_file_loader.cpp.

29  {
30  p_out.reset();
31  if (p_file->can_seek())
32  {
33  p_file->seek(0,p_abort);
34  }
35 
37  t_filesize size64;
38  size64 = p_file->get_size(p_abort);
39  if (size64 == filesize_invalid)//typically HTTP
40  {
41  pfc::string8 ansitemp;
42  is_utf8 = false;
43  enum {delta = 1024*64, max = 1024*512};
44  char temp[3];
45  t_size done;
46  done = p_file->read(temp,3,p_abort);
47  if (done != 3)
48  {
49  if (done > 0) p_out = pfc::stringcvt::string_utf8_from_ansi(temp,done);
50  return;
51  }
52  if (!memcmp(utf8_header,temp,3)) is_utf8 = true;
53  else ansitemp.add_string(temp,3);
54 
55  mem.set_size(delta);
56 
57  for(;;)
58  {
59  done = p_file->read(mem.get_ptr(),delta,p_abort);
60  if (done > 0)
61  {
62  if (is_utf8) p_out.add_string(mem.get_ptr(),done);
63  else ansitemp.add_string(mem.get_ptr(),done);
64  }
65  if (done < delta) break;
66  }
67 
68  if (!is_utf8)
69  {
70  p_out = pfc::stringcvt::string_utf8_from_ansi(ansitemp);
71  }
72 
73  return;
74  }
75  else
76  {
77  if (size64>1024*1024*128) throw exception_io_data();//hard limit
78  t_size size = pfc::downcast_guarded<t_size>(size64);
79  mem.set_size(size+1);
80  char * asdf = mem.get_ptr();
81  p_file->read_object(asdf,size,p_abort);
82  asdf[size]=0;
83  if (size>3 && !memcmp(utf8_header,asdf,3)) {is_utf8 = true; p_out.add_string(asdf+3); }
84  else {
85  is_utf8 = false;
87  }
88  return;
89  }
90  }
const t_item * get_ptr() const
Definition: array.h:213
virtual void add_string(const char *p_string, t_size p_length=~0)=0
static const t_filesize filesize_invalid
Invalid/unknown file size constant. Also see: t_filesize.
Definition: filesystem.h:16
string_utf8_from_win1252 string_utf8_from_ansi
Definition: string_conv.h:553
void add_string(const char *p_string, t_size p_length=~0)
Definition: string8_impl.h:4
size_t t_size
Definition: int_types.h:48
void set_size(t_size p_size)
Definition: array.h:104
t_uint64 t_filesize
Type used for file size related variables.
Definition: filesystem.h:8
static const unsigned char utf8_header[3]
void text_file_loader::read ( const char *  p_path,
abort_callback p_abort,
pfc::string_base p_out,
bool &  is_utf8 
)

Definition at line 99 of file text_file_loader.cpp.

100  {
102  filesystem::g_open_read(f,p_path,p_abort);
103  read(f,p_abort,p_out,is_utf8);
104  }
void read(const char *p_path, abort_callback &p_abort, pfc::string_base &p_out, bool &is_utf8)
void text_file_loader::write ( const service_ptr_t< file > &  p_file,
abort_callback p_abort,
const char *  p_string,
bool  is_utf8 
)

Definition at line 9 of file text_file_loader.cpp.

10  {
11  p_file->seek(0,p_abort);
12  p_file->set_eof(p_abort);
13  if (is_utf8)
14  {
15  p_file->write_object(utf8_header,sizeof(utf8_header),p_abort);
16  p_file->write_object(p_string,strlen(p_string),p_abort);
17  }
18  else
19  {
20 #ifdef _WIN32
22  p_file->write_object(bah,bah.length(),p_abort);
23 #else
24  throw exception_io_data();
25 #endif
26  }
27  }
static const unsigned char utf8_header[3]
void text_file_loader::write ( const char *  p_path,
abort_callback p_abort,
const char *  p_string,
bool  is_utf8 
)

Definition at line 92 of file text_file_loader.cpp.

93  {
95  filesystem::g_open_write_new(f,p_path,p_abort);
96  write(f,p_abort,p_string,is_utf8);
97  }
void write(const char *p_path, abort_callback &p_abort, const char *p_string, bool is_utf8)