shuerhaaken avatar shuerhaaken committed 84d66a7 Merge

Merged in rat4/libtaginfo/patch (pull request #6)

c bindings

Comments (0)

Files changed (2)

bindings/c/taginfo_c.cc

         free(*it);
     strings.clear();
 }
+
+static char **string_list_to_c_array(const StringList &list, int *out_length) {
+    if(list.isEmpty()) {
+        //cout << "list not there"  << endl;
+        return NULL;
+    }
+    uint len = list.size();
+    char **array = (char**)malloc(len * sizeof(char*));
+    int j = 0;
+    for(StringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
+        array[j] = ::strdup((*it).toCString(false));
+        if(stringManagementEnabled)
+            strings.prepend(array[j]);
+        j++;
+    }
+    *out_length = len;
+    return array;
+}
+
+static StringList string_list_from_c_array(const char* const* array, int length) {
+    StringList list;
+    for(int j = 0; j < length; j++) {
+        if(array[j]) {
+            list.append(::strdup(array[j]));
+        }
+    }
+    return list;
+}
 //*****************/STRING MANAGEMENT****************************************************
 
 
     i->set_is_compilation((bool)is_compilation);
 }
 
-
 char **taginfo_info_get_track_labels(const TagInfo_Info *info,
                                      int *labels_length) {
     const Info *i = reinterpret_cast<const Info *>(info);
     StringList list = i->get_track_labels_list();
-    
-    if(list.isEmpty()) {
-        //cout << "list not there"  << endl;
-        return NULL;
-    }
-    int len = list.size();
-    char **labels = (char**)malloc(len * sizeof(char*));
-    int j = 0;
-    for(StringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
-        labels[j] = ::strdup( (*it).toCString(false));
-        if(stringManagementEnabled)
-            strings.prepend(labels[j]);
-        j++;
-    }
-    *labels_length = len;
-    return labels;
+    return string_list_to_c_array(list, labels_length);
 }
 void taginfo_info_set_track_labels(TagInfo_Info *info,
-                                   char** labels, int labels_length) {
+                                   const char* const* labels, int labels_length) {
     Info *i = reinterpret_cast<Info *>(info);
-    StringList list = StringList();
-    for(int j = 0; j < labels_length; j++) {
-        if(labels[j]) {
-            list.append(::strdup(labels[j]));
-        }
-    }
+    StringList list = string_list_from_c_array(labels, labels_length);
     i->set_track_labels_list(list);
 }
 
                                      int *labels_length) {
     const Info *i = reinterpret_cast<const Info *>(info);
     StringList list = i->get_album_labels_list();
-    
-    if(list.isEmpty()) {
-        //cout << "list not there"  << endl;
-        return NULL;
-    }
-    int len = list.size();
-    char **labels = (char**)malloc(len * sizeof(char*));
-    int j = 0;
-    for(StringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
-        labels[j] = ::strdup( (*it).toCString(false));
-        if(stringManagementEnabled)
-            strings.prepend(labels[j]);
-        j++;
-    }
-    *labels_length = len;
-    return labels;
+    return string_list_to_c_array(list, labels_length);
 }
 void taginfo_info_set_album_labels(TagInfo_Info *info,
-                                   char** labels, int labels_length) {
+                                   const char* const* labels, int labels_length) {
     Info *i = reinterpret_cast<Info *>(info);
-    StringList list = StringList();
-    for(int j = 0; j < labels_length; j++) {
-        if(labels[j]) {
-            list.append(::strdup(labels[j]));
-        }
-    }
+    StringList list = string_list_from_c_array(labels, labels_length);
     i->set_album_labels_list(list);
 }
 
                                       int * labels_length) {
     const Info *i = reinterpret_cast<const Info *>(info);
     StringList list = i->get_artist_labels_list();
-    
-    if(list.isEmpty()) {
-        //cout << "list not there"  << endl;
-        return NULL;
-    }
-    int len = list.size();
-    char **labels = (char**)malloc(len * sizeof(char*));
-    int j = 0;
-    for(StringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
-        labels[j] = ::strdup( (*it).toCString(false));
-        if(stringManagementEnabled)
-            strings.prepend(labels[j]);
-        j++;
-    }
-    *labels_length = len;
-    return labels;
+    return string_list_to_c_array(list, labels_length);
 }
 void taginfo_info_set_artist_labels(TagInfo_Info *info,
-                                    char** labels, int labels_length) {
+                                    const char* const* labels, int labels_length) {
     Info *i = reinterpret_cast<Info *>(info);
-    StringList list = StringList();
-    for(int j = 0; j < labels_length; j++) {
-        if(labels[j]) {
-            list.append(::strdup(labels[j]));
-        }
-    }
+    StringList list = string_list_from_c_array(labels, labels_length);
     i->set_artist_labels_list(list);
 }
 

bindings/c/taginfo_c.h

 char **taginfo_info_get_track_labels(const TagInfo_Info *info,
                                      int *data_length);
 void taginfo_info_set_track_labels(TagInfo_Info *info,
-                                   char** data, int data_length);
+                                   const char* const* data, int data_length);
 
 char **taginfo_info_get_album_labels(const TagInfo_Info *info,
                                      char** data, int *data_length);
 void taginfo_info_set_album_labels(TagInfo_Info *info,
-                                   char** data, int data_length);
+                                   const char* const* data, int data_length);
 
 char **taginfo_info_get_artist_labels(const TagInfo_Info *info,
                                       int *data_length);
 void taginfo_info_set_artist_labels(TagInfo_Info *info,
-                                    char** data, int data_length);
+                                    const char* const* data, int data_length);
 
 BOOL taginfo_info_can_handle_image(const TagInfo_Info *info);
 BOOL taginfo_info_get_image(const TagInfo_Info *info,
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.