foobar2000 SDK  2015-01-14
Functions
ProfileCache Namespace Reference

Functions

file::ptr FetchFile (const char *context, const char *name, const char *webURL, t_filetimestamp acceptableAge, abort_callback &abort)
 

Function Documentation

file::ptr ProfileCache::FetchFile ( const char *  context,
const char *  name,
const char *  webURL,
t_filetimestamp  acceptableAge,
abort_callback abort 
)
inline

Definition at line 2 of file ProfileCache.h.

2  {
3  const double timeoutVal = 5;
4 
6  path.fix_dir_separator('\\');
7  path << context;
8  try {
9  filesystem::g_create_directory(path, abort);
10  } catch(exception_io_already_exists) {}
11  path << "\\" << name;
12 
13  bool fetch = false;
14  file::ptr fLocal;
15 
16  try {
17  filesystem::g_open_timeout(fLocal, path, filesystem::open_mode_write_existing, timeoutVal, abort);
18  fetch = fLocal->get_timestamp(abort) < filetimestamp_from_system_timer() - acceptableAge;
19  } catch(exception_io_not_found) {
20  filesystem::g_open_timeout(fLocal, path, filesystem::open_mode_write_new, timeoutVal, abort);
21  fetch = true;
22  }
23  if (fetch) {
24  fLocal->resize(0, abort);
25  file::ptr fRemote;
26  try {
27  filesystem::g_open(fRemote, webURL, filesystem::open_mode_read, abort);
28  } catch(exception_io_not_found) {
29  fLocal.release();
30  try { filesystem::g_remove_timeout(path, timeoutVal, abort); } catch(...) {}
31  throw;
32  }
33  pfc::array_t<t_uint8> buffer; buffer.set_size(64*1024);
34  for(;;) {
35  t_size delta = buffer.get_size();
36  delta = fRemote->read(buffer.get_ptr(), delta, abort);
37  fLocal->write(buffer.get_ptr(), delta, abort);
38  if (delta < buffer.get_size()) break;
39  }
40  fLocal->seek(0, abort);
41  }
42  return fLocal;
43  }
const t_item * get_ptr() const
Definition: array.h:213
t_filetimestamp filetimestamp_from_system_timer()
Definition: filesystem.cpp:684
const char * get_profile_path()
Returns filesystem path to directory with user settings, e.g. file://c:.
size_t t_size
Definition: int_types.h:48
string8_fastalloc string_formatter
Definition: string_base.h:614
void set_size(t_size p_size)
Definition: array.h:104
t_size get_size() const
Definition: array.h:130