Просмотр исходного кода

Add function to get String from FileAccess

George Marques 6 лет назад
Родитель
Сommit
4f0590338f
2 измененных файлов с 18 добавлено и 0 удалено
  1. 17 0
      core/os/file_access.cpp
  2. 1 0
      core/os/file_access.h

+ 17 - 0
core/os/file_access.cpp

@@ -409,6 +409,23 @@ int FileAccess::get_buffer(uint8_t *p_dst, int p_length) const {
 	return i;
 }
 
+String FileAccess::get_as_utf8_string() const {
+	PoolVector<uint8_t> sourcef;
+	int len = get_len();
+	sourcef.resize(len + 1);
+
+	PoolVector<uint8_t>::Write w = sourcef.write();
+	int r = get_buffer(w.ptr(), len);
+	ERR_FAIL_COND_V(r != len, String());
+	w[len] = 0;
+
+	String s;
+	if (s.parse_utf8((const char *)w.ptr())) {
+		return String();
+	}
+	return s;
+}
+
 void FileAccess::store_16(uint16_t p_dest) {
 
 	uint8_t a, b;

+ 1 - 0
core/os/file_access.h

@@ -113,6 +113,7 @@ public:
 	virtual String get_line() const;
 	virtual String get_token() const;
 	virtual Vector<String> get_csv_line(const String &p_delim = ",") const;
+	virtual String get_as_utf8_string() const;
 
 	/**< use this for files WRITTEN in _big_ endian machines (ie, amiga/mac)
 	 * It's not about the current CPU type but file formats.