Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: UberMouse on Mar 4th, 2012  |  syntax: C  |  size: 5.85 KB  |  views: 97  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <string.h>
  4. #include "hacks.h"
  5.  
  6. typedef struct
  7. {
  8.     char *folder;
  9.     char *filePath;
  10.     char *fileName;
  11. } MediaInfo;
  12.  
  13. typedef struct
  14. {
  15.     MediaInfo *fileInfo;
  16.     int seasons;
  17. } TvShow;
  18.  
  19. typedef struct
  20. {
  21.     void *data;
  22.     struct Node *next;
  23.     struct Node *prev;
  24. } Node;
  25.  
  26. typedef struct
  27. {
  28.     Node *head;
  29.     Node *tail;
  30.     int len;
  31. } LinkedList;
  32.  
  33. typedef struct
  34. {
  35.     char *contents;
  36.     int len;
  37. } String;
  38.  
  39. const char MOVIES_LOCATION[] = "G:\\Videos\\Movies\\";
  40. const char TVSHOWS_LOCATION[] = "G:\\Videos\\TV Shows\\";
  41. const char GAMES_LOCATION[] = "D:\\Games\\";
  42. LinkedList movies;
  43. LinkedList games;
  44. LinkedList tvShows;
  45.  
  46. Node *createEmptyNode(void)
  47. {
  48.     Node *n = malloc(sizeof(int) * 3);
  49.     n->data = null;
  50.     n->next = null;
  51.     n->prev = null;
  52. }
  53.  
  54. MediaInfo *createEmptyMediaInfo(void)
  55. {
  56.     MediaInfo *mi = malloc(sizeof(MediaInfo));
  57.     mi->fileName = null;
  58.     mi->filePath = null;
  59.     mi->folder = null;
  60. }
  61.  
  62. TvShow *createEmptyTvShow(void)
  63. {
  64.     TvShow *tv = malloc(sizeof(TvShow));
  65.     tv->fileInfo = null;
  66.     tv->seasons = null;
  67. }
  68.  
  69. String *createEmptyString(void) {
  70.     String *s = malloc(sizeof(String));
  71.     s->contents = malloc(sizeof(char) * 15);
  72.     strcpy(s->contents, "");
  73.     s->len = 15;
  74. }
  75.  
  76. LinkedList linkedList_create(void)
  77. {
  78.     Node *head = null;
  79.     Node *tail = null;
  80.     LinkedList list = {head, tail, 0};
  81.     return list;
  82. }
  83.  
  84. LinkedList linkedList_add(LinkedList list, void *data)
  85. {
  86.     Node *cur = list.head;
  87.     if(list.head != null)
  88.     {
  89.         while(cur->next != null)
  90.             cur = cur->next;
  91.         Node *next = createEmptyNode();
  92.         next->data = malloc(sizeof(data));
  93.         next->data = data;
  94.         next->prev = cur;
  95.         cur->next = next;
  96.  
  97.     }
  98.     else
  99.     {
  100.         Node *head = createEmptyNode();
  101.         head->data = malloc(sizeof(data));
  102.         head->data = data;
  103.         list.head = head;
  104.     }
  105.     list.len += 1;
  106.     return list;
  107. }
  108.  
  109. void *linkedList_get(LinkedList list, int index)
  110. {
  111.     Node *cur = list.head;
  112.     int i = 0;
  113.     if(index != 0)
  114.     {
  115.         while(cur->next != null && i < index)
  116.         {
  117.             cur = cur->next;
  118.             i++;
  119.         }
  120.     }
  121.     if(index == i)
  122.         return cur->data;
  123.     return null;
  124. }
  125.  
  126. LinkedList getFilesInDir(char* dir, LinkedList list)
  127. {
  128.     WIN32_FIND_DATAA    find;
  129.     HANDLE    hFile;
  130.     char filter[150];
  131.     MediaInfo *mi = malloc(sizeof(MediaInfo));
  132.     strcpy(filter, dir);
  133.     strcat(filter, "*.*");
  134.  
  135.     if((hFile=FindFirstFile(filter, &find)) != INVALID_HANDLE_VALUE)
  136.     {
  137.         while(FindNextFile(hFile, &find))
  138.         {
  139.             char* filename = malloc(sizeof(char) * (strlen(find.cFileName) + 1));
  140.             char* filepath = malloc(sizeof(char) * (strlen(dir) + strlen(find.cFileName) + 1));
  141.             strcpy(filename, find.cFileName);
  142.             strcpy(filepath, dir);
  143.             strcat(filepath, filename);
  144.             if(strcmp(filename, "..") && (strstr(filename, ".") == null))
  145.             {
  146.                 mi->fileName = filename;
  147.                 mi->filePath = filepath;
  148.                 mi->folder = dir;
  149.                 list = linkedList_add(list, mi);
  150.                 mi = createEmptyMediaInfo();
  151.             }
  152.         }
  153.         FindClose(hFile);
  154.     }
  155.     else
  156.         printf("%s: file(s) not found\n", filter);
  157.     return list;
  158. }
  159.  
  160. LinkedList getTvShows(LinkedList list)
  161. {
  162.     LinkedList tvShowFolders = getFilesInDir(TVSHOWS_LOCATION, linkedList_create());
  163.     LinkedList _tmpSeasons;
  164.     MediaInfo *mi;
  165.     TvShow *tv = createEmptyTvShow();
  166.     int i = 0;
  167.     while(i < tvShowFolders.len)
  168.     {
  169.         mi = linkedList_get(tvShowFolders, i);
  170.         tv->fileInfo = mi;
  171.         char *tvShowFolder = malloc(sizeof(char) * (strlen(mi->filePath) + 2));
  172.         strcpy(tvShowFolder, mi->filePath);
  173.         strcat(tvShowFolder, "\\");
  174.         _tmpSeasons = getFilesInDir(tvShowFolder, linkedList_create());
  175.         tv->seasons = _tmpSeasons.len;
  176.         list = linkedList_add(list, tv);
  177.         tv = linkedList_get(list, 0);
  178.         tv = createEmptyTvShow();
  179.         mi = createEmptyMediaInfo();
  180.         i++;
  181.     }
  182.     return list;
  183. }
  184.  
  185. String *string_concat(String *s, char *str) {
  186.     int len = strlen(s->contents) + strlen(str);
  187.     if(len > s->len)
  188.         realloc(s->contents, sizeof(char));
  189.     if(s->contents == "")
  190.         strcpy(s->contents, str);
  191.     else
  192.         strcat(s->contents, str);
  193.     return s;
  194. }
  195.  
  196. int main()
  197. {
  198.     int i = 0;
  199.     MediaInfo *mi;
  200.     TvShow *tv;
  201.     int size = 0;
  202.     char *_tmp;
  203.     String *data = createEmptyString();
  204.  
  205.     STARTUPINFO si;
  206.     PROCESS_INFORMATION pi;
  207.  
  208.     ZeroMemory( &si, sizeof(si) );
  209.     si.cb = sizeof(si);
  210.     ZeroMemory( &pi, sizeof(pi) );
  211.  
  212.     movies = getFilesInDir(MOVIES_LOCATION, linkedList_create());
  213.     games = getFilesInDir(GAMES_LOCATION, linkedList_create());
  214.     tvShows = getTvShows(linkedList_create());
  215.     while(i < movies.len) {
  216.         data = string_concat(data, "Movie:");
  217.         mi = linkedList_get(movies, i);
  218.         data = string_concat(data, mi->fileName);
  219.         data = string_concat(data, ";:;");
  220.         i++;
  221.     }
  222.     i = 0;
  223.     while(i < games.len) {
  224.         data = string_concat(data, "Game:");
  225.         mi = linkedList_get(movies, i);
  226.         data = string_concat(data, mi->fileName);
  227.         data = string_concat(data, ";:;");
  228.         i++;
  229.     }
  230.     i = 0;
  231.     while(i < tvShows.len) {
  232.         data = string_concat(data, "TvShow:");
  233.         tv = linkedList_get(tvShows, i);
  234.         mi = tv->fileInfo;
  235.         data = string_concat(data, mi->fileName);
  236.         data = string_concat(data, "Seasons:");
  237.         data = string_concat(data, tv->seasons);
  238.         data = string_concat(data, ";:;");
  239.         i++;
  240.     }
  241.     printf(data->contents);
  242.     return 0;
  243. }
clone this paste RAW Paste Data