comparison src/name/blackcap/exifwasher/exiv2/native.cpp @ 1:42277ce58ace

Improve exception handling; always use absolute paths on libs.
author David Barts <n5jrn@me.com>
date Tue, 31 Mar 2020 15:38:25 -0700
parents db63d01a23c6
children efd9fe2d70d7
comparison
equal deleted inserted replaced
0:db63d01a23c6 1:42277ce58ace
29 const char *cPath = jEnv->GetStringUTFChars(path, NULL); 29 const char *cPath = jEnv->GetStringUTFChars(path, NULL);
30 jlong ret = 0; 30 jlong ret = 0;
31 try { 31 try {
32 ret = reinterpret_cast<jlong> (Exiv2::ImageFactory::open(cPath).release()); 32 ret = reinterpret_cast<jlong> (Exiv2::ImageFactory::open(cPath).release());
33 } catch (...) { 33 } catch (...) {
34 jEnv->ExceptionClear();
34 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception"); 35 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
35 const char *pfx = "unable to open "; 36 const char *pfx = "unable to open ";
36 char *message = (char *) malloc(strlen(cPath) + strlen(pfx) + 1); 37 char *message = (char *) malloc(strlen(cPath) + strlen(pfx) + 1);
37 strcpy(message, pfx); 38 strcpy(message, pfx);
38 strcat(message, cPath); 39 strcat(message, cPath);
49 * Signature: ()V 50 * Signature: ()V
50 */ 51 */
51 JNIEXPORT void JNICALL Java_name_blackcap_exifwasher_exiv2_Image__1writeMetadata 52 JNIEXPORT void JNICALL Java_name_blackcap_exifwasher_exiv2_Image__1writeMetadata
52 (JNIEnv *jEnv, jobject jThis) { 53 (JNIEnv *jEnv, jobject jThis) {
53 Exiv2::Image *image = reinterpret_cast<Exiv2::Image *> (getPointer(jEnv, jThis)); 54 Exiv2::Image *image = reinterpret_cast<Exiv2::Image *> (getPointer(jEnv, jThis));
55 if (jEnv->ExceptionCheck()) return;
54 try { 56 try {
55 image->writeMetadata(); 57 image->writeMetadata();
56 } catch (...) { 58 } catch (...) {
59 jEnv->ExceptionClear();
57 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception"); 60 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
58 jEnv->ThrowNew(ex, "unable to write metadata"); 61 jEnv->ThrowNew(ex, "unable to write metadata");
59 } 62 }
60 } 63 }
61 64
65 * Signature: ()J 68 * Signature: ()J
66 */ 69 */
67 JNIEXPORT jlong JNICALL Java_name_blackcap_exifwasher_exiv2_Image__1getMetadata 70 JNIEXPORT jlong JNICALL Java_name_blackcap_exifwasher_exiv2_Image__1getMetadata
68 (JNIEnv *jEnv, jobject jThis) { 71 (JNIEnv *jEnv, jobject jThis) {
69 Exiv2::Image *image = reinterpret_cast<Exiv2::Image *> (getPointer(jEnv, jThis)); 72 Exiv2::Image *image = reinterpret_cast<Exiv2::Image *> (getPointer(jEnv, jThis));
73 if (jEnv->ExceptionCheck()) return 0;
70 try { 74 try {
71 image->readMetadata(); 75 image->readMetadata();
72 } catch (...) { 76 } catch (...) {
77 jEnv->ExceptionClear();
73 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception"); 78 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
74 jEnv->ThrowNew(ex, "unable to read metadata"); 79 jEnv->ThrowNew(ex, "unable to read metadata");
80 return 0;
75 } 81 }
76 return reinterpret_cast<jlong> (&(image->exifData())); 82 return reinterpret_cast<jlong> (&(image->exifData()));
77 } 83 }
78 84
79 /* 85 /*
82 * Signature: ()V 88 * Signature: ()V
83 */ 89 */
84 JNIEXPORT void JNICALL Java_name_blackcap_exifwasher_exiv2_Image__1dtor 90 JNIEXPORT void JNICALL Java_name_blackcap_exifwasher_exiv2_Image__1dtor
85 (JNIEnv *jEnv, jobject jThis) { 91 (JNIEnv *jEnv, jobject jThis) {
86 Exiv2::Image *image = reinterpret_cast<Exiv2::Image *> (getPointer(jEnv, jThis)); 92 Exiv2::Image *image = reinterpret_cast<Exiv2::Image *> (getPointer(jEnv, jThis));
93 if (jEnv->ExceptionCheck()) return;
87 delete image; 94 delete image;
88 } 95 }
89 96
90 #ifdef __cplusplus 97 #ifdef __cplusplus
91 } 98 }
104 * Signature: (Ljava/lang/String;)V 111 * Signature: (Ljava/lang/String;)V
105 */ 112 */
106 JNIEXPORT void JNICALL Java_name_blackcap_exifwasher_exiv2_ExifData__1erase 113 JNIEXPORT void JNICALL Java_name_blackcap_exifwasher_exiv2_ExifData__1erase
107 (JNIEnv *jEnv, jobject jThis, jstring key) { 114 (JNIEnv *jEnv, jobject jThis, jstring key) {
108 Exiv2::ExifData *data = reinterpret_cast<Exiv2::ExifData *> (getPointer(jEnv, jThis)); 115 Exiv2::ExifData *data = reinterpret_cast<Exiv2::ExifData *> (getPointer(jEnv, jThis));
116 if (jEnv->ExceptionCheck()) return;
109 const char *cKey = jEnv->GetStringUTFChars(key, NULL); 117 const char *cKey = jEnv->GetStringUTFChars(key, NULL);
110 Exiv2::ExifData::iterator found = data->findKey(Exiv2::ExifKey(std::string(cKey))); 118 Exiv2::ExifData::iterator found = data->findKey(Exiv2::ExifKey(std::string(cKey)));
111 try { 119 try {
112 data->erase(found); 120 data->erase(found);
113 } catch (...) { 121 } catch (...) {
122 jEnv->ExceptionClear();
114 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception"); 123 jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
115 const char *pfx = "unable to delete "; 124 const char *pfx = "unable to delete ";
116 char *message = (char *) malloc(strlen(cKey) + strlen(pfx) + 1); 125 char *message = (char *) malloc(strlen(cKey) + strlen(pfx) + 1);
117 strcpy(message, pfx); 126 strcpy(message, pfx);
118 strcat(message, cKey); 127 strcat(message, cKey);
128 * Signature: (Ljava/lang/String;)Lname/blackcap/exifwasher/exiv2/ExifData/Value; 137 * Signature: (Ljava/lang/String;)Lname/blackcap/exifwasher/exiv2/ExifData/Value;
129 */ 138 */
130 JNIEXPORT jobject JNICALL Java_name_blackcap_exifwasher_exiv2_ExifData__1value 139 JNIEXPORT jobject JNICALL Java_name_blackcap_exifwasher_exiv2_ExifData__1value
131 (JNIEnv *jEnv, jobject jThis, jstring key) { 140 (JNIEnv *jEnv, jobject jThis, jstring key) {
132 Exiv2::ExifData *data = reinterpret_cast<Exiv2::ExifData *> (getPointer(jEnv, jThis)); 141 Exiv2::ExifData *data = reinterpret_cast<Exiv2::ExifData *> (getPointer(jEnv, jThis));
142 if (jEnv->ExceptionCheck()) return NULL;
133 const char *cKey = jEnv->GetStringUTFChars(key, NULL); 143 const char *cKey = jEnv->GetStringUTFChars(key, NULL);
134 Exiv2::ExifData::const_iterator found = data->findKey(Exiv2::ExifKey(std::string(cKey))); 144 Exiv2::ExifData::const_iterator found = data->findKey(Exiv2::ExifKey(std::string(cKey)));
135 jEnv->ReleaseStringUTFChars(key, cKey); 145 jEnv->ReleaseStringUTFChars(key, cKey);
136 if (found == data->end()) { 146 if (found == data->end()) {
137 return NULL; 147 return NULL;
138 } 148 }
139 jclass klass = jEnv->FindClass("name/blackcap/exifwasher/exiv2/ExifData$Value"); 149 jclass klass = jEnv->FindClass("name/blackcap/exifwasher/exiv2/ExifData$Value");
150 if (jEnv->ExceptionCheck()) return NULL;
140 jstring type = jEnv->NewStringUTF(found->typeName()); 151 jstring type = jEnv->NewStringUTF(found->typeName());
141 jstring value = jEnv->NewStringUTF(found->toString().c_str()); 152 jstring value = jEnv->NewStringUTF(found->toString().c_str());
142 jmethodID method = jEnv->GetMethodID(klass, "<init>", "(Ljava/lang/String;Ljava/lang/String;)V"); 153 jmethodID method = jEnv->GetMethodID(klass, "<init>", "(Ljava/lang/String;Ljava/lang/String;)V");
154 if (jEnv->ExceptionCheck()) return NULL;
143 return jEnv->NewObject(klass, method, type, value); 155 return jEnv->NewObject(klass, method, type, value);
144 } 156 }
145 157
146 /* 158 /*
147 * Class: name_blackcap_exifwasher_exiv2_ExifData 159 * Class: name_blackcap_exifwasher_exiv2_ExifData
149 * Signature: ()[Ljava/lang/String; 161 * Signature: ()[Ljava/lang/String;
150 */ 162 */
151 JNIEXPORT jobjectArray JNICALL Java_name_blackcap_exifwasher_exiv2_ExifData__1keys 163 JNIEXPORT jobjectArray JNICALL Java_name_blackcap_exifwasher_exiv2_ExifData__1keys
152 (JNIEnv *jEnv, jobject jThis) { 164 (JNIEnv *jEnv, jobject jThis) {
153 Exiv2::ExifData *data = reinterpret_cast<Exiv2::ExifData *> (getPointer(jEnv, jThis)); 165 Exiv2::ExifData *data = reinterpret_cast<Exiv2::ExifData *> (getPointer(jEnv, jThis));
166 if (jEnv->ExceptionCheck()) return NULL;
154 jclass klass = jEnv->FindClass("java/lang/String"); 167 jclass klass = jEnv->FindClass("java/lang/String");
168 if (jEnv->ExceptionCheck()) return NULL;
155 jobjectArray ret = jEnv->NewObjectArray(data->count(), klass, NULL); 169 jobjectArray ret = jEnv->NewObjectArray(data->count(), klass, NULL);
170 if (jEnv->ExceptionCheck()) return NULL;
156 Exiv2::ExifData::const_iterator end = data->end(); 171 Exiv2::ExifData::const_iterator end = data->end();
157 jsize j = 0; 172 jsize j = 0;
158 for (Exiv2::ExifData::const_iterator i = data->begin(); i != end; ++i) { 173 for (Exiv2::ExifData::const_iterator i = data->begin(); i != end; ++i) {
159 jEnv->SetObjectArrayElement(ret, j++, jEnv->NewStringUTF(i->key().c_str())); 174 jEnv->SetObjectArrayElement(ret, j++, jEnv->NewStringUTF(i->key().c_str()));
175 if (jEnv->ExceptionCheck()) break;
160 } 176 }
161 return ret; 177 return ret;
162 } 178 }
163 179
164 #ifdef __cplusplus 180 #ifdef __cplusplus