File: Directory.h

package info (click to toggle)
android-file-transfer 4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 2,156 kB
  • sloc: cpp: 12,409; python: 140; lex: 47; xml: 24; sh: 13; makefile: 8
file content (146 lines) | stat: -rw-r--r-- 3,562 bytes parent folder | download | duplicates (3)
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
/*
    This file is part of Android File Transfer For Linux.
    Copyright (C) 2015-2020  Vladimir Menshakov

    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; either version 2.1 of the License,
    or (at your option) any later version.

    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#ifndef AFTL_MTP_BACKEND_LINUX_USB_DIRECTORY_H
#define AFTL_MTP_BACKEND_LINUX_USB_DIRECTORY_H

#include <mtp/ByteArray.h>
#include <Exception.h>
#include <vector>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <stddef.h>

namespace mtp { namespace usb
{
	class File : Noncopyable
	{
		FILE *_f;

	public:
		File(const std::string &path) : _f(fopen(path.c_str(), "rb"))
		{
			if (!_f)
				throw posix::Exception("open " + path);
		}

		~File()
		{ fclose(_f); }

		FILE *GetHandle()
		{ return _f; }

		std::string ReadLine(size_t bufSize = 1024)
		{
			std::vector<char> buf(bufSize);
			if (!fgets(buf.data(), buf.size(), _f))
				throw posix::Exception("fgets");
			return buf.data();
		}

		ByteArray ReadAll()
		{
			static const size_t step = 4096;

			fseek(_f, 0, SEEK_SET);
			ByteArray buf;
			size_t r;
			do
			{
				size_t offset = buf.size();
				buf.resize(offset + step);
				r = fread(buf.data() + offset, 1, step, _f);
			}
			while(r == step);
			buf.resize(buf.size() - step + r);
			return buf;
		}

		int ReadInt(int base)
		{
			int r;
			switch(base)
			{
				case 16: if (fscanf(_f, "%x", &r) != 1) throw std::runtime_error("cannot read number"); break;
				case 10: if (fscanf(_f, "%d", &r) != 1) throw std::runtime_error("cannot read number"); break;
				default: throw std::runtime_error("invalid base");
			}
			return r;
		}
	};

	class Directory : Noncopyable
	{
	private:
		DIR *				_dir;
		std::vector<u8>		_buffer;

	public:
		Directory(const std::string &path): _dir(opendir(path.c_str()))
		{
			if (!_dir)
				throw posix::Exception("opendir");

			long name_max = pathconf(path.c_str(), _PC_NAME_MAX);
			if (name_max == -1)         /* Limit not defined, or error */
				name_max = 255;         /* Take a guess */

			_buffer.resize(offsetof(struct dirent, d_name) + name_max + 1);
		}

		std::string Read()
		{
			dirent *entry;
#if 0
			dirent *buffer = static_cast<dirent *>(static_cast<void *>(_buffer.data()));
			//thread safety is not in posix yet, but those shitheads already deprecated it
			int r = readdir_r(_dir, buffer, &entry);
#endif
			entry = readdir(_dir);
			return entry? entry->d_name: "";
		}

		~Directory()
		{ closedir(_dir); }

		static int ReadInt(const std::string &path, int base = 16)
		{
			File f(path);
			return f.ReadInt(base);
		}

		static std::string ReadString(const std::string &path)
		{
			File f(path);
			std::string str = f.ReadLine();
			size_t end = str.find_last_not_of(" \t\r\n\f");
			return end != str.npos? str.substr(0, end + 1): str;
		}

		static ByteArray ReadAll(const std::string &path)
		{
			File f(path);
			return f.ReadAll();
		}
	};
}}

#endif	/* DIRECTORY_H */