summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThiep Ha <thiepha@gmail.com>2016-05-11 17:54:45 +0900
committerThiep Ha <thiepha@gmail.com>2016-05-11 18:00:19 +0900
commite7805d97b68c67d1aabf0521fa510e3848ce98ce (patch)
treed4e1e0408454bdcf3db1be635312b51ffade394e
parent3256a602823dd4bf59290380bc03e0010ec554d1 (diff)
elm cnp: simplify data preparer
Use eina_strbuf to simplify the data preparer for uri. This helps remove the complex for loop and also reduce the memory allocated for uri array. This also removes redundant checking.
-rw-r--r--src/lib/elementary/elm_cnp.c141
1 files changed, 35 insertions, 106 deletions
diff --git a/src/lib/elementary/elm_cnp.c b/src/lib/elementary/elm_cnp.c
index 17f46f8041..361089187b 100644
--- a/src/lib/elementary/elm_cnp.c
+++ b/src/lib/elementary/elm_cnp.c
@@ -1077,14 +1077,15 @@ _x11_data_preparer_uri(Ecore_X_Event_Selection_Notify *notify,
1077{ 1077{
1078 Ecore_X_Selection_Data *data; 1078 Ecore_X_Selection_Data *data;
1079 Ecore_X_Selection_Data_Files *files; 1079 Ecore_X_Selection_Data_Files *files;
1080 char *p, *s, *stripstr = NULL; 1080 char *p, *stripstr = NULL;
1081 1081
1082 data = notify->data; 1082 data = notify->data;
1083 cnp_debug("data->format is %d %p %p\n", data->format, notify, data); 1083 cnp_debug("data->format is %d %p %p\n", data->format, notify, data);
1084 if (data->content == ECORE_X_SELECTION_CONTENT_FILES) 1084 if (data->content == ECORE_X_SELECTION_CONTENT_FILES)
1085 { 1085 {
1086 int i, len = 0; 1086 Efreet_Uri *uri;
1087 Efreet_Uri **uri; 1087 Eina_Strbuf *strbuf;
1088 int i;
1088 1089
1089 cnp_debug("got a files list\n"); 1090 cnp_debug("got a files list\n");
1090 files = notify->data; 1091 files = notify->data;
@@ -1098,62 +1099,27 @@ _x11_data_preparer_uri(Ecore_X_Event_Selection_Notify *notify,
1098 stripstr = p = strdup(files->files[0]); 1099 stripstr = p = strdup(files->files[0]);
1099 */ 1100 */
1100 1101
1101 uri = calloc(1, sizeof(*uri) * files->num_files); 1102 strbuf = eina_strbuf_new();
1102 if (!uri) return EINA_FALSE; 1103 if (!strbuf)
1104 return EINA_FALSE;
1103 1105
1104 for (i = 0; i < files->num_files ; i++) 1106 for (i = 0; i < files->num_files ; i++)
1105 { 1107 {
1106 uri[i] = efreet_uri_decode(files->files[i]); 1108 uri = efreet_uri_decode(files->files[i]);
1107 if (!uri[i]) 1109 if (uri)
1108 { 1110 {
1109 /* Is there any reason why we care of URI without scheme? */ 1111 eina_strbuf_append(strbuf, uri->path);
1110 if (files->files[i][0] != '/') continue; 1112 efreet_uri_free(uri);
1111 len += strlen(files->files[i]) + 1;
1112 } 1113 }
1113 else 1114 else
1114 { 1115 {
1115 if (strcmp(uri[i]->protocol, "file")) 1116 eina_strbuf_append(strbuf, files->files[i]);
1116 {
1117 efreet_uri_free(uri[i]);
1118 uri[i] = NULL;
1119 continue;
1120 }
1121 len += strlen(uri[i]->path) + 1;
1122 }
1123 }
1124 p = NULL;
1125 if (len > 0)
1126 {
1127 s = stripstr = malloc(len + 1);
1128 for (i = 0; i < files->num_files ; i++)
1129 {
1130 if (uri[i])
1131 p = (char *)uri[i]->path;
1132 else
1133 p = files->files[i];
1134
1135 if (s)
1136 {
1137 len = strlen(p);
1138 strcpy(s, p);
1139 if (i < (files->num_files - 1))
1140 {
1141 s[len] = '\n';
1142 s[len + 1] = 0;
1143 s += len + 1;
1144 }
1145 else
1146 {
1147 s[len] = 0;
1148 s += len;
1149 }
1150 }
1151
1152 if (uri[i])
1153 efreet_uri_free(uri[i]);
1154 } 1117 }
1118 if (i < (files->num_files - 1))
1119 eina_strbuf_append(strbuf, "\n");
1155 } 1120 }
1156 free(uri); 1121 stripstr = eina_strbuf_string_steal(strbuf);
1122 eina_strbuf_free(strbuf);
1157 } 1123 }
1158 else 1124 else
1159 { 1125 {
@@ -1171,9 +1137,8 @@ _x11_data_preparer_uri(Ecore_X_Event_Selection_Notify *notify,
1171 else 1137 else
1172 { 1138 {
1173 free(p); 1139 free(p);
1174 stripstr = (char *)eina_memdup((unsigned char *)uri->path, strlen(uri->path), EINA_TRUE); 1140 stripstr = strdup(uri->path);
1175 efreet_uri_free(uri); 1141 efreet_uri_free(uri);
1176 if (!stripstr) return EINA_FALSE;
1177 } 1142 }
1178 } 1143 }
1179 1144
@@ -2726,7 +2691,7 @@ _wl_data_preparer_uri(Wl_Cnp_Selection *sel, Elm_Selection_Data *ddata, Ecore_Wl
2726{ 2691{
2727 cnp_debug("In\n"); 2692 cnp_debug("In\n");
2728 2693
2729 char *p, *s, *stripstr = NULL; 2694 char *p, *stripstr = NULL;
2730 char *data = ev->data; 2695 char *data = ev->data;
2731 Dropable *drop; 2696 Dropable *drop;
2732 const char *type = NULL; 2697 const char *type = NULL;
@@ -2738,67 +2703,33 @@ _wl_data_preparer_uri(Wl_Cnp_Selection *sel, Elm_Selection_Data *ddata, Ecore_Wl
2738 { 2703 {
2739 int num_files = 0; 2704 int num_files = 0;
2740 char **files = NULL; 2705 char **files = NULL;
2741 int i, len = 0; 2706 Efreet_Uri *uri;
2742 Efreet_Uri **uri; 2707 Eina_Strbuf *strbuf;
2708 int i;
2743 2709
2744 _wl_selection_parser(ev->data, ev->len, &files, &num_files); 2710 _wl_selection_parser(ev->data, ev->len, &files, &num_files);
2745 cnp_debug("got a files list\n"); 2711 cnp_debug("got a files list\n");
2746 uri = calloc(1, sizeof(*uri) * num_files);
2747 if (!uri) return EINA_FALSE;
2748 2712
2749 for (i = 0; i < num_files ; i++) 2713 strbuf = eina_strbuf_new();
2714 if (!strbuf)
2715 return EINA_FALSE;
2716 for (i = 0; i < num_files; i++)
2750 { 2717 {
2751 uri[i] = efreet_uri_decode(files[i]); 2718 uri = efreet_uri_decode(files[i]);
2752 if (!uri[i]) 2719 if (uri)
2753 { 2720 {
2754 /* Is there any reason why we care of URI without scheme? */ 2721 eina_strbuf_append(strbuf, uri->path);
2755 if (files[i][0] != '/') continue; 2722 efreet_uri_free(uri);
2756 len += strlen(files[i]) + 1;
2757 } 2723 }
2758 else 2724 else
2759 { 2725 {
2760 if (strcmp(uri[i]->protocol, "file")) 2726 eina_strbuf_append(strbuf, files[i]);
2761 {
2762 efreet_uri_free(uri[i]);
2763 uri[i] = NULL;
2764 continue;
2765 }
2766 len += strlen(uri[i]->path) + 1;
2767 }
2768 }
2769 p = NULL;
2770 if (len > 0)
2771 {
2772 s = stripstr = malloc(len + 1);
2773 for (i = 0; i < num_files ; i++)
2774 {
2775 if (uri[i])
2776 p = (char *)uri[i]->path;
2777 else
2778 p = files[i];
2779
2780 if (s)
2781 {
2782 len = strlen(p);
2783 strcpy(s, p);
2784 if (i < (num_files - 1))
2785 {
2786 s[len] = '\n';
2787 s[len + 1] = 0;
2788 s += len + 1;
2789 }
2790 else
2791 {
2792 s[len] = 0;
2793 s += len;
2794 }
2795 }
2796
2797 if (uri[i])
2798 efreet_uri_free(uri[i]);
2799 } 2727 }
2728 if (i < (num_files - 1))
2729 eina_strbuf_append(strbuf, "\n");
2800 } 2730 }
2801 free(uri); 2731 stripstr = eina_strbuf_string_steal(strbuf);
2732 eina_strbuf_free(strbuf);
2802 } 2733 }
2803 else 2734 else
2804 { 2735 {
@@ -2817,10 +2748,8 @@ _wl_data_preparer_uri(Wl_Cnp_Selection *sel, Elm_Selection_Data *ddata, Ecore_Wl
2817 else 2748 else
2818 { 2749 {
2819 free(p); 2750 free(p);
2820 stripstr = (char *)eina_memdup((unsigned char *)uri->path, 2751 stripstr = strdup(uri->path);
2821 strlen(uri->path), EINA_TRUE);
2822 efreet_uri_free(uri); 2752 efreet_uri_free(uri);
2823 if (!stripstr) return EINA_FALSE;
2824 } 2753 }
2825 } 2754 }
2826 2755