forked from CloudPolis/webdav-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback.cpp
More file actions
109 lines (101 loc) · 3.67 KB
/
Copy pathcallback.cpp
File metadata and controls
109 lines (101 loc) · 3.67 KB
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
/*#***************************************************************************
# __ __ _____ _____
# Project | | | | | \ / ___|
# | |__| | | |\ \ / /
# | | | | ) ) ( (
# | /\ | | |/ / \ \___
# \_/ \_/ |_____/ \_____|
#
# Copyright (C) 2016, The WDC Project, <designerror@yandex.ru>, et al.
#
# This software is licensed as described in the file LICENSE, which
# you should have received as part of this distribution.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the LICENSE file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
############################################################################*/
#include <algorithm>
#include <fstream>
#include <cstring>
#include "callback.hpp"
namespace WebDAV
{
namespace Callback
{
namespace Read
{
size_t stream(char * ptr, size_t item_size, size_t item_count, void * stream)
{
auto in_stream = (std::istream *)stream;
auto read_bytes = static_cast<unsigned long long>(item_size * item_count);
auto position = static_cast<unsigned long long>(in_stream->tellg());
in_stream->seekg(0, std::ios::end);
auto size = static_cast<unsigned long long>(in_stream->tellg());
in_stream->seekg(position, std::ios::beg);
auto rest_bytes = size - position;
read_bytes = std::min<unsigned long long>(read_bytes, rest_bytes);
in_stream->read(ptr, read_bytes);
return read_bytes;
}
size_t buffer(char * ptr, size_t item_size, size_t item_count, void * buffer)
{
auto data = (Data*)buffer;
auto size = static_cast<unsigned long long>(item_size * item_count);
auto rest_bytes = data->size - data->position;
auto copied_bytes = std::min<unsigned long long>(size, rest_bytes);
memcpy(ptr, data->buffer, copied_bytes);
data->position += copied_bytes;
return copied_bytes;
}
}
namespace Write
{
size_t stream(char * ptr, size_t item_size, size_t item_count, void * stream)
{
auto out_stream = (std::ostream *)stream;
size_t write_bytes = item_size * item_count;
out_stream->write(ptr, write_bytes);
return write_bytes;
}
size_t buffer(char * ptr, size_t item_size, size_t item_count, void * buffer)
{
auto data = (Data*)buffer;
auto size = static_cast<unsigned long long>(item_size * item_count);
auto rest_bytes = data->size - data->position;
auto copied_bytes = std::min<unsigned long long>(size, rest_bytes);
memcpy(data->buffer, data->buffer, copied_bytes);
data->position += copied_bytes;
return copied_bytes;
}
}
namespace Append
{
size_t buffer(char * ptr, size_t item_size, size_t item_count, void * buffer)
{
auto data = (Data*)buffer;
auto append_size = item_size * item_count;
auto new_buffer_size = data->size + append_size;
auto new_buffer = new char[new_buffer_size];
if (data->size != 0) memcpy(new_buffer, data->buffer, data->size);
memcpy(new_buffer + data->size, ptr, append_size);
delete[] data->buffer;
data->buffer = new_buffer;
data->size = new_buffer_size;
return append_size;
}
size_t stream(char * ptr, size_t item_size, size_t item_count, void * stream)
{
auto out_stream = (std::ostream *)stream;
size_t write_bytes = item_size * item_count;
out_stream->seekp(0, std::ios::end);
out_stream->write(ptr, write_bytes);
return write_bytes;
}
}
}
}