Advertisement
userxbw

Get files and insert into list C w/o header file

Aug 18th, 2022
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.82 KB | None | 0 0
  1. /* filelist.c
  2.  
  3. Copyright (C) 1999-2003 Tom Gilbert.
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to
  7. deal in the Software without restriction, including without limitation the
  8. rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. sell copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11.  
  12. The above copyright notice and this permission notice shall be included in
  13. all copies of the Software and its documentation and acknowledgment shall be
  14. given in the documentation and software packages that this Software was
  15. used.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. imPLIED, INCLUDING BUT NOT LimITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. THE AUTHORS BE LIABLE FOR ANY CLAim, DAMAGES OR OTHER LIABILITY, WHETHER
  21. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  
  24. //aug 10 2017
  25.  
  26. Modifactions made by me.
  27. Same permissions are passed on as formentioned pertaining to the
  28. modifactions made by me.
  29. Copyright (C) 2017 - ... Michael Hearas.
  30.  
  31. */
  32. //aug 10 2017
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <sys/stat.h>
  37. #include <dirent.h>
  38. #include <errno.h>
  39. #include <time.h>
  40.  
  41. // get pid
  42. #include <sys/types.h>
  43. #include <unistd.h>
  44. // end
  45.  
  46. #include <Imlib2.h>
  47. #include "filelist.h"
  48.  
  49. mylist* filelist = NULL;
  50. mylist *ret = NULL;
  51.  
  52.  
  53.  int isDirectory(const char *path)
  54.  {
  55.      struct stat status;
  56.      stat(path, &status);
  57.      
  58.      if (S_ISDIR(status.st_mode))
  59.         return 1;
  60.    
  61.     return 0;
  62.      
  63.  }
  64.  
  65.  int is_regular_file(const char *path)
  66. {
  67.     struct stat path_stat;
  68.     stat(path, &path_stat);
  69.     return S_ISREG(path_stat.st_mode);
  70. }
  71.  
  72.  
  73. Imlib_Image check_image_err(Imlib_Image image)
  74. {
  75. Imlib_Load_Error err;
  76. char *filename;
  77.  
  78.  
  79. filename = get_file_name();
  80.  
  81. printf("filename\n%s\n", filename);
  82.  
  83. if(filename == NULL)
  84. {
  85.     printf("File null\n");
  86.     exit(1);
  87. }
  88.  
  89.  image = imlib_load_image_with_error_return(filename, &err);
  90.  
  91. if (err)
  92. {
  93.  
  94.     //spit out image path and name stdout
  95.     printf("Bad Image: %s\n", filename);
  96.     //get next image
  97.     image = NULL;
  98.     check_image_err(image);
  99.  
  100. }
  101.  
  102. if(image == NULL)
  103. {
  104.     printf("No Image\n");
  105.     check_image_err(image);
  106.  
  107. }
  108.     return image;
  109. }
  110. char *get_file_name(void)
  111. {
  112.     ret = list_time_jump(ret,1);
  113.   //  printf("%s\n", ret->filename);
  114.     return (ret->filename);
  115. }
  116.  
  117. mylist *list_time_jump(mylist *l, int num)
  118. {
  119.  
  120.     int i;
  121.  
  122.  
  123.     if(l)
  124.         ret = l;
  125.     else
  126.         ret = filelist;
  127.     //limits it movement
  128.     for (i = 0; i < num; i++)
  129.     {
  130.         if (ret->next)
  131.         {
  132.             ret = ret->next;
  133.         }
  134.         else
  135.         {
  136.             ret = filelist;
  137.         }
  138.     }
  139.     return (ret);
  140. }
  141.  
  142. mylist * list_new(void)
  143. {
  144.    mylist *l;
  145.  
  146.    l = (mylist *) malloc(sizeof(mylist));
  147.    l->filename = NULL;
  148.    l->listcount = 0;
  149.    l->next = NULL;
  150.    l->prev = NULL;
  151.    return (l);
  152. }
  153.  
  154.  
  155. void add_2_front(mylist** filelist, char *filename)
  156. {
  157.     mylist* new_file = (mylist *)malloc(sizeof(mylist));
  158.     new_file->filename = strdup(filename);
  159.     new_file->listcount = 0;
  160.  
  161.     if(!(*filelist)){
  162.         new_file->next = NULL;
  163.         (*filelist) = new_file;
  164.     }else{
  165.         new_file->next = (*filelist);
  166.         (*filelist) = new_file;
  167.     }
  168.  return;
  169. }
  170.  
  171.  
  172. void insertNode(mylist** head, char *filename, int c)
  173. {
  174.     while (*head && strcmp((*head)->filename, filename) < 0)
  175.         head = &(*head)->next;
  176.  
  177.     mylist *p = (mylist *) malloc(sizeof *p);
  178.     p->filename = strdup(filename);
  179.     p->listcount = c;
  180.     p->next = *head;
  181.     *head = p;
  182. }
  183.  
  184. char *get_im_name(void)
  185. {
  186.     mylist* curr  = filelist;
  187.  
  188.     //get current file in list
  189.         if(curr->filename)
  190.         {
  191.             return (curr->filename);
  192.         }
  193.         else
  194.         {
  195.             curr = curr->next;
  196.         }
  197.  
  198.     return(curr->filename);
  199.  
  200. }
  201.  
  202. char *get_number_in_list(int numberfile)
  203. {
  204.     int ln;
  205.     mylist* current = filelist;
  206.     ln = list_length(filelist);
  207.  
  208. if(numberfile > ln)
  209. {
  210.     printf("number ( %d ) larger than list length\n", numberfile);
  211.     exit(1);
  212. }
  213.  
  214. while (1)
  215. {
  216.  
  217.     if (current->listcount == numberfile)
  218.     {
  219.         return(current->filename);
  220.     }
  221.  
  222.    if(current->next)
  223.      current = current->next;
  224.     else
  225.       current = filelist;
  226.  
  227.   }
  228. }
  229.  
  230. void add_file_count(mylist* filelist)
  231. {
  232.     mylist* current = filelist;
  233.     int count = 1;
  234.     while(current)
  235.     {
  236.         current->listcount = count;
  237.         current = current->next;
  238.         count++;
  239.     }
  240. }
  241.  
  242. void display_list(mylist* filelist)
  243. {
  244.     mylist* current_node = filelist;
  245.     printf("Linked List:\n");
  246.     while(current_node){
  247.         printf("[%d]->%s\n", current_node->listcount, current_node->filename);
  248.         current_node = current_node->next;
  249.     }
  250.     printf("NULL\n");
  251. }
  252.  
  253.  
  254. void GetFiles(char *name, int indent)
  255. {
  256.     DIR *dir;
  257.     struct dirent *entry;
  258.     char *newName;
  259.  
  260.     newName = strdup(name);
  261.     // to remove leading /
  262.     //so it does not have /dir//sub-dir
  263.     int len = strlen(newName);
  264.     if (newName[len - 1] == '/')
  265.         newName[len - 1] = '\0';
  266.  
  267.      if (!(dir = opendir(newName)))
  268.      {
  269.         free(newName);
  270.         return;
  271.     }
  272.  
  273.  
  274.     while ((entry = readdir(dir)) != NULL)
  275.     {
  276.         if (entry->d_type == DT_DIR)
  277.         {
  278.             char path[1024];
  279.             if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
  280.                 continue;
  281.                 //printf("1 path %s\n%s\n", newName,  entry->d_name);
  282.                 snprintf(path, sizeof(path), "%s/%s", newName, entry->d_name);
  283.                 GetFiles(path, indent + 2);
  284.         }
  285.         if ( check_file_ext(entry->d_name) )
  286.         {
  287.             char *fullname = malloc(strlen(newName)+strlen(entry->d_name)+2);//+2 null-terminator and /
  288.             if ( fullname == NULL)
  289.             {
  290.                 printf("Not enough Memory\n");
  291.                 exit(1);
  292.             }
  293.             strcpy(fullname, newName);
  294.             strcat(fullname, "/");
  295.             strcat(fullname, entry->d_name);
  296.             //printf("%s\n", fullname);
  297.             add_2_front(&filelist, fullname);
  298.             free(fullname);
  299.         }
  300.  
  301.     }
  302.     closedir(dir);
  303.     free(newName);
  304.  
  305. }
  306.  
  307. int check_file_ext(char *name1)
  308. {
  309.         /*
  310.  
  311.     returns:
  312.  
  313.          1 if proper extension found
  314.  
  315.          0 if improper extension found
  316.  
  317.     */
  318.  
  319.     char *ext = NULL;
  320.     char *extensions[] = {".jpg", ".JPG", ".png", ".PNG", ".jpeg",
  321.      ".JPEG", ".xpm", ".gif"};
  322.  
  323.     ext = strrchr(name1, '.');
  324.     if (!ext)
  325.          return 0;  
  326.  
  327.     for (int i = 0; i < sizeof(extensions)/sizeof(extensions[0]); i++)
  328.        if ( !strcmp( extensions[i], ext) )
  329.             return 1;
  330.  
  331.     return 0;
  332. }
  333.  
  334. int list_length(mylist * l)
  335. {
  336.    int length;
  337.  
  338.    length = 0;
  339.    while (l)
  340.    {
  341.       length++;
  342.       l = l->next;
  343.    }
  344.    return (length);
  345. }
  346.  
  347. int  get_length(void)
  348. {
  349.     return (list_length(filelist));
  350. }
  351.  
  352. void prepare_filelist(void)
  353. {
  354.     filelist = list_randomize(filelist);
  355. }
  356.  
  357. mylist *  list_randomize(mylist * list)
  358. {
  359.     int len, r, i;
  360.     mylist **farray, *f, *t;
  361.  
  362.    if (!list)
  363.       return (NULL);
  364.    len = list_length(list);
  365.  
  366.    if (len <= 1)
  367.       return (list);
  368.    farray = (mylist **) malloc(sizeof(mylist *) * len);
  369.    for (f = list, i = 0; f; f = f->next, i++)
  370.    {
  371.       farray[i] = f;
  372.    }
  373.    srand(getpid() * time(NULL) % ((unsigned int) -1));
  374.    for (i = 0; i < len - 1; i++)
  375.    {
  376.      r = i + rand() / (RAND_MAX / (len - i) + 1 );
  377.       t = farray[r];
  378.       farray[r] = farray[i];
  379.       farray[i] = t;
  380.    }
  381.    list = farray[0];
  382.    list->prev = NULL;
  383.    list->next = farray[1];
  384.    for (i = 1, f = farray[1]; i < len - 1; i++, f = f->next)
  385.    {
  386.       f->prev = farray[i - 1];
  387.       f->next = farray[i + 1];
  388.    }
  389.    f->prev = farray[len - 2];
  390.    f->next = NULL;
  391.    free(farray);
  392.    return (list);
  393. }
  394.  
  395.  
  396. void remove_link(mylist** head, char *value_to_remove)
  397. {
  398.     mylist* temp_node = NULL;
  399.     mylist* current_node = (*head);
  400.     if((*head)->filename == value_to_remove){
  401.         (*head) = (*head)->next;
  402.     }else{
  403.         while(current_node){
  404.             if(current_node->filename == value_to_remove)
  405. {
  406.                 temp_node = current_node->next;
  407.                 current_node->next = NULL;
  408.                 current_node = temp_node;
  409.             }
  410.             current_node = current_node->next;
  411.         }
  412.     }
  413.     return;
  414. }
  415.  
  416. void free_file(mylist *file)
  417. {
  418.     if (!file)
  419.         return;
  420.     if(file->filename)
  421.         free(file->filename);
  422.  
  423.     free(file);
  424.     return;
  425. }
  426.  
  427.  
  428. mylist* list_unlink(mylist * root, mylist * l)
  429. {
  430.    if (!l)
  431.       return (root);
  432.  
  433.    if ((!root) || ((l == root) && (!l->next)))
  434.       return (NULL);
  435.  
  436.    if (l->prev)
  437.       l->prev->next = l->next;
  438.    if (l->next)
  439.       l->next->prev = l->prev;
  440.    if (root == l)
  441.       root = root->next;
  442.    return (root);
  443. }
  444.  
  445. mylist * list_move_down_by_one(mylist * root, mylist * l)
  446. {
  447.    mylist *temp;
  448.  
  449.    if (!l || !l->next)
  450.       return (root);
  451.  
  452.    // store item we link next to
  453.    temp = l->next;
  454.    // remove from list
  455.    root = list_unlink(root, l);
  456.    // add back one before
  457.    l->next = temp->next;
  458.    l->prev = temp;
  459.    if (temp->next)
  460.       temp->next->prev = l;
  461.    temp->next = l;
  462.  
  463.    return (root);
  464. }
  465.  
  466.  
  467. mylist * list_add_end(mylist * root, char *filename, int c)
  468. {
  469.    mylist *l, *last;
  470.    int ln;
  471.    ln = list_length(root);
  472.    ln += root->listcount + 1;
  473.  
  474.    last = list_last(root);
  475.    l = list_new();
  476.    l->prev = last;
  477.    l->filename = filename;
  478.    l->listcount = ln;
  479.    if (last)
  480.    {
  481.       last->next = l;
  482.       return (root);
  483.    }
  484.    else
  485.    {
  486.       return (l);
  487.    }
  488. }
  489.  
  490. mylist * list_last(mylist * l)
  491. {
  492.    if (l)
  493.    {
  494.       while (l->next)
  495.          l = l->next;
  496.    }
  497.    printf("list_last, %s\n", l->filename);
  498.    return (l);
  499. }
  500.  
  501.  
  502.  
  503. // sorted List functions
  504. // how to call it.
  505. /*
  506.  filelist = list_sort(filelist, cmp_filename);
  507.  */
  508.  
  509.  
  510. int cmp_filename(mylist *file1, mylist *file2)
  511. {
  512.     return(strcmp(file1->filename, file2->filename));
  513. }
  514.  
  515. mylist *list_sort(mylist * list, compare_fn cmp)
  516. {
  517.    mylist *l1, *l2;
  518.  
  519.    if (!list)
  520.       return (NULL);
  521.    if (!list->next)
  522.       return (list);
  523.  
  524.    l1 = list;
  525.    l2 = list->next;
  526.  
  527.    while ((l2 = l2->next) != NULL)
  528.    {
  529.       if ((l2 = l2->next) == NULL)
  530.          break;
  531.       l1 = l1->next;
  532.    }
  533.    l2 = l1->next;
  534.    l1->next = NULL;
  535.  
  536.    return (list_sort_merge
  537.            (list_sort(list, cmp), list_sort(l2, cmp), cmp));
  538. }
  539.  
  540. mylist *list_sort_merge(mylist * l1, mylist * l2 , compare_fn cmp)
  541. {
  542.    mylist list, *l, *lprev;
  543.  
  544.    l = &list;
  545.    lprev = NULL;
  546.  
  547.    while (l1 && l2)
  548.    {
  549.       if (strcmp(l1->filename, l2->filename) < 0)
  550.       {
  551.          l->next = l1;
  552.          l = l->next;
  553.          l->prev = lprev;
  554.          lprev = l;
  555.          l1 = l1->next;
  556.       }
  557.       else
  558.       {
  559.          l->next = l2;
  560.          l = l->next;
  561.          l->prev = lprev;
  562.          lprev = l;
  563.          l2 = l2->next;
  564.       }
  565.    }
  566.    l->next = l1 ? l1 : l2;
  567.    l->next->prev = l;
  568.  
  569.    return (list.next);
  570. }
  571. char *remove_ext (char* mystr, char dot, char sep)
  572.  
  573. {
  574.     char *retstr, *lastdot, *lastsep;
  575.  
  576.     // Error checks and allocate string.
  577.  
  578.     if (mystr == NULL)
  579.         return NULL;
  580.     if ((retstr = malloc (strlen (mystr) + 1)) == NULL)
  581.         return NULL;
  582.  
  583.     // Make a copy and find the relevant characters.
  584.  
  585.     strcpy (retstr, mystr);
  586.     lastdot = strrchr (retstr, dot);
  587.     lastsep = (sep == 0) ? NULL : strrchr (retstr, sep);
  588.  
  589.     // If it has an extension separator.
  590.  
  591.     if (lastdot != NULL) {
  592.         // and it's before the extenstion separator.
  593.  
  594.         if (lastsep != NULL) {
  595.             if (lastsep < lastdot) {
  596.                 // then remove it.
  597.  
  598.                 *lastdot = '\0';
  599.             }
  600.         } else {
  601.             // Has extension separator with no path separator.
  602.  
  603.             *lastdot = '\0';
  604.         }
  605.     }
  606.  
  607.     // Return the modified string.
  608.  
  609.     return retstr;
  610. }
  611.  
  612. char *remove_back(char *front)
  613. {
  614.     char *frontTemp , *returnEnd;
  615.  
  616.     if (front == NULL)
  617.         return NULL;
  618.  
  619.     frontTemp = malloc (strlen (front) + 1);
  620.  
  621.     strcpy (frontTemp, front);
  622.     // chop off 11 chars at front
  623.     returnEnd = frontTemp + 11;
  624.  
  625.     return (returnEnd);
  626. }
  627.  
  628.  
  629. char *removeChar( char * string, char letter )
  630. {
  631.  
  632.       for( unsigned int i = 0; i < strlen( string ); i++ )
  633.  
  634.         if( string[i] == letter )
  635.         {
  636.             strcpy( string + i, string + i + 1 );
  637.         }
  638.     return (string);
  639.     }
  640.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement