File Manager / src / utils /

common-utils.c

  1. /*
  2. * Copyright 2014 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17.  
  18. #include "utils/common-utils.h"
  19. #include "utils/app-types.h"
  20. #include "utils/logger.h"
  21.  
  22. char *common_util_strconcat(const char *string1, ...)
  23. {
  24. RETVM_IF(!string1, NULL, "First parameter is NULL");
  25.  
  26. char *curr_string = NULL;
  27. char *res_string = NULL;
  28. Eina_Strbuf *buf = eina_strbuf_new();
  29.  
  30. RETVM_IF(!buf, NULL, "Failed to allocate memory for string buffer");
  31.  
  32. if (eina_strbuf_append(buf, string1) == EINA_FALSE) {
  33. ERR("Failed to append data to string buffer");
  34. eina_strbuf_free(buf);
  35. return NULL;
  36. }
  37.  
  38. va_list args;
  39. va_start(args, string1);
  40. curr_string = va_arg(args, char*);
  41.  
  42. while (curr_string) {
  43. if (eina_strbuf_append(buf, curr_string) == EINA_FALSE) {
  44. ERR("Failed to append data to string buffer");
  45. eina_strbuf_free(buf);
  46. return NULL;
  47. }
  48. curr_string = va_arg(args, char*);
  49. }
  50. va_end(args);
  51.  
  52. res_string = eina_strbuf_string_steal(buf);
  53. eina_strbuf_free(buf);
  54.  
  55. return res_string;
  56. }
  57.  
  58. bool common_util_is_string_empty(const char *string)
  59. {
  60. RETVM_IF(!string, true, "String is NULL");
  61. return !strcmp(string, "");
  62. }
  63.  
  64. int common_util_copy_selected_file_list(Eina_List *source_list, Eina_List **dest_list)
  65. {
  66. RETVM_IF(!source_list, RESULT_TYPE_INVALID_ARG, "Source list is empty");
  67. RETVM_IF(!dest_list, RESULT_TYPE_INVALID_ARG, "Destination list pointer is NULL");
  68.  
  69. Eina_List *list = NULL;
  70. node_info *data = NULL;
  71. EINA_LIST_FOREACH(source_list, list, data) {
  72. if (data && data->is_selected) {
  73. node_info *pNode = calloc(1, sizeof(node_info));
  74. if (pNode) {
  75. pNode->name = strdup(data->name);
  76. pNode->parent_path = strdup(data->parent_path);
  77. pNode->type = data->type;
  78. pNode->is_selected = data->is_selected;
  79. } else {
  80. ERR("Failed to allocate memory");
  81. return RESULT_TYPE_FAIL_ALLOCATE_MEMORY;
  82. }
  83. *dest_list = eina_list_append(*dest_list, pNode);
  84. }
  85. }
  86. return RESULT_TYPE_OK;
  87. }
  88.  
  89. void common_util_clear_file_list(Eina_List **file_list)
  90. {
  91. RETM_IF(!file_list, "File list pointer is NULL");
  92.  
  93. if (*file_list) {
  94. node_info *pNode = NULL;
  95. EINA_LIST_FREE(*file_list, pNode) {
  96. free(pNode->name);
  97. free(pNode->parent_path);
  98. free(pNode);
  99. }
  100. *file_list = NULL;
  101. }
  102. }
  103.  
  104. void common_util_clear_storage_list(Eina_List **storage_list)
  105. {
  106. RETM_IF(!storage_list, "File list pointer is NULL");
  107.  
  108. if (*storage_list) {
  109. storage_info *pNode = NULL;
  110. EINA_LIST_FREE(*storage_list, pNode) {
  111. free(pNode->root_path);
  112. free(pNode->root_name);
  113. free(pNode);
  114. }
  115. *storage_list = NULL;
  116. }
  117. }
  118.  
  119. const char *common_util_get_filename(const char *path)
  120. {
  121. char *result = NULL;
  122.  
  123. if (!path) {
  124. return NULL;
  125. }
  126. if ((result = strrchr(path, '/'))) {
  127. result++;
  128. } else {
  129. result = (char *)path;
  130. }
  131.  
  132. return result;
  133. }