Verified Commit 39656742 authored by Alvarez, Gonzalo's avatar Alvarez, Gonzalo
Browse files

PsiBase64: better interface

parent 6ef8ba85
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -42,9 +42,9 @@ int main()
	const PsimagLite::String s = "ADP GmbH\nAnalyse Design & Programmierung"
	                             "\nGesellschaft mit beschränkter Haftung" ;

	PsimagLite::PsiBase64 base64;
	PsimagLite::String encoded = base64.encode(s);
	PsimagLite::PsiBase64::Encode base64encode(s);
	PsimagLite::String encoded = base64encode();
	std::cout<<"encoded: "<<encoded<<"\n";
	std::cout<<"decoded: "<<base64.decode(encoded)<<"\n";
	std::cout<<"decoded: "<<PsimagLite::PsiBase64::Decode(encoded)()<<"\n";
}
+3 −3
Original line number Diff line number Diff line
@@ -196,7 +196,7 @@ public:
					continue;
				}

				if (str = end) {
				if (str == end) {
					mode = 2;
					break;
				}
@@ -217,8 +217,8 @@ public:

			assert(mode == 2);

			PsiBase64 base;
			data_ = base.decode(buffer);
			PsiBase64::Decode base64decode(buffer);
			data_ = base64decode();
			check();

			if (verbose_) {
+92 −75
Original line number Diff line number Diff line
@@ -42,14 +42,29 @@ namespace PsimagLite {

class PsiBase64 {

	static const String base64Chars_;

public:

	class Encode {

	public:

	const String& encode(const String& str)
		Encode(const String& str)
		{
		return encode(reinterpret_cast<const unsigned char*>(str.c_str()),str.length());
			encode_(reinterpret_cast<const unsigned char*>(str.c_str()),str.length());
		}

	const String& encode(unsigned char const* bytesToEncode, unsigned int inLen)
		Encode(unsigned char const* bytesToEncode, unsigned int inLen)
		{
			encode_(bytesToEncode,inLen);
		}

		const String& operator()() const { return buffer_; }

	private:

		void encode_(unsigned char const* bytesToEncode, unsigned int inLen)
		{
			buffer_ = "";
			int i = 0;
@@ -85,11 +100,16 @@ public:

				while ((i++ < 3)) buffer_ += '=';
			}

		return buffer_;
		}

	const String& decode(const String& encodedString)
		String buffer_;
	}; // class Encode

	class Decode {

	public:

		Decode(const String& encodedString)
		{
			buffer_ = "";
			int inLen = encodedString.size();
@@ -127,11 +147,9 @@ public:

				for (j = 0; (j < i - 1); j++) buffer_ += charArray3[j];
			}

		return buffer_;
		}

	const String& result() const { return buffer_; }
		const String& operator()() const { return buffer_; }

	private:

@@ -139,10 +157,9 @@ private:
			return (isalnum(c) || (c == '+') || (c == '/'));
		}

	static const String base64Chars_;

		String buffer_;
};
	}; // class Decode
}; // class PsiBase64
}

#endif // PSIBASE64_H