13497b265076f338e320fcf01d3e46379492669b2Paulo Casanova/*
23497b265076f338e320fcf01d3e46379492669b2Paulo Casanova * Copyright (C) 2010 The Android Open Source Project
33497b265076f338e320fcf01d3e46379492669b2Paulo Casanova *
43497b265076f338e320fcf01d3e46379492669b2Paulo Casanova * Licensed under the Apache License, Version 2.0 (the "License");
53497b265076f338e320fcf01d3e46379492669b2Paulo Casanova * you may not use this file except in compliance with the License.
63497b265076f338e320fcf01d3e46379492669b2Paulo Casanova * You may obtain a copy of the License at
73497b265076f338e320fcf01d3e46379492669b2Paulo Casanova *
83497b265076f338e320fcf01d3e46379492669b2Paulo Casanova *      http://www.apache.org/licenses/LICENSE-2.0
93497b265076f338e320fcf01d3e46379492669b2Paulo Casanova *
103497b265076f338e320fcf01d3e46379492669b2Paulo Casanova * Unless required by applicable law or agreed to in writing, software
113497b265076f338e320fcf01d3e46379492669b2Paulo Casanova * distributed under the License is distributed on an "AS IS" BASIS,
123497b265076f338e320fcf01d3e46379492669b2Paulo Casanova * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133497b265076f338e320fcf01d3e46379492669b2Paulo Casanova * See the License for the specific language governing permissions and
143497b265076f338e320fcf01d3e46379492669b2Paulo Casanova * limitations under the License.
153497b265076f338e320fcf01d3e46379492669b2Paulo Casanova */
163497b265076f338e320fcf01d3e46379492669b2Paulo Casanova
173497b265076f338e320fcf01d3e46379492669b2Paulo Casanova#ifndef SCOPED_UTF_CHARS_H_included
183497b265076f338e320fcf01d3e46379492669b2Paulo Casanova#define SCOPED_UTF_CHARS_H_included
193497b265076f338e320fcf01d3e46379492669b2Paulo Casanova
203497b265076f338e320fcf01d3e46379492669b2Paulo Casanova#include <string.h>
213497b265076f338e320fcf01d3e46379492669b2Paulo Casanova#include <jni.h>
223497b265076f338e320fcf01d3e46379492669b2Paulo Casanova
233497b265076f338e320fcf01d3e46379492669b2Paulo Casanova#include "android-base/logging.h"
243497b265076f338e320fcf01d3e46379492669b2Paulo Casanova
253497b265076f338e320fcf01d3e46379492669b2Paulo Casanova// This file was copied with some minor modifications from libnativehelper.
263497b265076f338e320fcf01d3e46379492669b2Paulo Casanova// As soon as libnativehelper can be compiled for Windows, this file should be
273497b265076f338e320fcf01d3e46379492669b2Paulo Casanova// replaced with libnativehelper's implementation.
283497b265076f338e320fcf01d3e46379492669b2Paulo Casanovaclass ScopedUtfChars {
293497b265076f338e320fcf01d3e46379492669b2Paulo Casanova public:
303497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  ScopedUtfChars(JNIEnv* env, jstring s) : env_(env), string_(s) {
313497b265076f338e320fcf01d3e46379492669b2Paulo Casanova    CHECK(s != nullptr);
323497b265076f338e320fcf01d3e46379492669b2Paulo Casanova    utf_chars_ = env->GetStringUTFChars(s, nullptr);
333497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  }
343497b265076f338e320fcf01d3e46379492669b2Paulo Casanova
353497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  ScopedUtfChars(ScopedUtfChars&& rhs) :
363497b265076f338e320fcf01d3e46379492669b2Paulo Casanova      env_(rhs.env_), string_(rhs.string_), utf_chars_(rhs.utf_chars_) {
373497b265076f338e320fcf01d3e46379492669b2Paulo Casanova    rhs.env_ = nullptr;
383497b265076f338e320fcf01d3e46379492669b2Paulo Casanova    rhs.string_ = nullptr;
393497b265076f338e320fcf01d3e46379492669b2Paulo Casanova    rhs.utf_chars_ = nullptr;
403497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  }
413497b265076f338e320fcf01d3e46379492669b2Paulo Casanova
423497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  ~ScopedUtfChars() {
433497b265076f338e320fcf01d3e46379492669b2Paulo Casanova    if (utf_chars_) {
443497b265076f338e320fcf01d3e46379492669b2Paulo Casanova      env_->ReleaseStringUTFChars(string_, utf_chars_);
453497b265076f338e320fcf01d3e46379492669b2Paulo Casanova    }
463497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  }
473497b265076f338e320fcf01d3e46379492669b2Paulo Casanova
483497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  ScopedUtfChars& operator=(ScopedUtfChars&& rhs) {
493497b265076f338e320fcf01d3e46379492669b2Paulo Casanova    if (this != &rhs) {
503497b265076f338e320fcf01d3e46379492669b2Paulo Casanova      // Delete the currently owned UTF chars.
513497b265076f338e320fcf01d3e46379492669b2Paulo Casanova      this->~ScopedUtfChars();
523497b265076f338e320fcf01d3e46379492669b2Paulo Casanova
533497b265076f338e320fcf01d3e46379492669b2Paulo Casanova      // Move the rhs ScopedUtfChars and zero it out.
543497b265076f338e320fcf01d3e46379492669b2Paulo Casanova      env_ = rhs.env_;
553497b265076f338e320fcf01d3e46379492669b2Paulo Casanova      string_ = rhs.string_;
563497b265076f338e320fcf01d3e46379492669b2Paulo Casanova      utf_chars_ = rhs.utf_chars_;
573497b265076f338e320fcf01d3e46379492669b2Paulo Casanova      rhs.env_ = nullptr;
583497b265076f338e320fcf01d3e46379492669b2Paulo Casanova      rhs.string_ = nullptr;
593497b265076f338e320fcf01d3e46379492669b2Paulo Casanova      rhs.utf_chars_ = nullptr;
603497b265076f338e320fcf01d3e46379492669b2Paulo Casanova    }
613497b265076f338e320fcf01d3e46379492669b2Paulo Casanova    return *this;
623497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  }
633497b265076f338e320fcf01d3e46379492669b2Paulo Casanova
643497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  const char* c_str() const {
653497b265076f338e320fcf01d3e46379492669b2Paulo Casanova    return utf_chars_;
663497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  }
673497b265076f338e320fcf01d3e46379492669b2Paulo Casanova
683497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  size_t size() const {
693497b265076f338e320fcf01d3e46379492669b2Paulo Casanova    return strlen(utf_chars_);
703497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  }
713497b265076f338e320fcf01d3e46379492669b2Paulo Casanova
723497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  const char& operator[](size_t n) const {
733497b265076f338e320fcf01d3e46379492669b2Paulo Casanova    return utf_chars_[n];
743497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  }
753497b265076f338e320fcf01d3e46379492669b2Paulo Casanova
763497b265076f338e320fcf01d3e46379492669b2Paulo Casanova private:
773497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  JNIEnv* env_;
783497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  jstring string_;
793497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  const char* utf_chars_;
803497b265076f338e320fcf01d3e46379492669b2Paulo Casanova
813497b265076f338e320fcf01d3e46379492669b2Paulo Casanova  DISALLOW_COPY_AND_ASSIGN(ScopedUtfChars);
823497b265076f338e320fcf01d3e46379492669b2Paulo Casanova};
833497b265076f338e320fcf01d3e46379492669b2Paulo Casanova
843497b265076f338e320fcf01d3e46379492669b2Paulo Casanova#endif  // SCOPED_UTF_CHARS_H_included
85