Guest User

Untitled

a guest
Apr 26th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <dirent.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10.  
  11. int move(char* loc,char* dest);
  12. int CodToInt(char* cod);
  13. char* substring(char* input,int a,int b);
  14. char* getMoveRegister(char* career, char* year, char* input);
  15. char* getYear(char* codYear);
  16. char* getCareer(char* codCareer);
  17.  
  18. int
  19. main (void)
  20. {
  21. DIR *dp;
  22. FILE *file;
  23.  
  24. struct dirent *ep;
  25.  
  26. //descomponer nombre
  27. char* year;
  28. char* career;
  29. char* codYear;
  30. char* codCareer;
  31. //registro log.txt
  32. char* moveRegister;
  33. //crear directorio
  34. char* directory;
  35. //mover archivos
  36. char* input;
  37. char* output;
  38.  
  39. int length;
  40.  
  41. file = fopen("./tarea/log.txt","a+");
  42. dp = opendir ("./tarea");
  43.  
  44. directory = (char*)malloc(50*sizeof(char));
  45. input = (char*)malloc(50*sizeof(char));
  46. output = (char*)malloc(50*sizeof(char));
  47.  
  48. if (dp != NULL)
  49. {
  50. while ((ep = readdir (dp)))
  51. {
  52. /*filtra archivos que no comportan formato*/
  53. length = strlen(ep->d_name);
  54. if((length == 14) || (length == 16))
  55. {
  56. year = substring(ep->d_name,0,4);
  57. if((strcmp(year,"2011") == 0) || (strcmp(year,"2010") == 0))
  58. {
  59. /*saca codigo de carrera*/
  60. codCareer = substring(ep->d_name,4,6);
  61. career = getCareer(codCareer);
  62. }
  63. else
  64. {
  65. codYear = substring(ep->d_name,0,2);
  66.  
  67. /*recalcula year*/
  68. free(year);
  69. year = getYear(codYear);
  70.  
  71. /*saca codigo de carrera*/
  72. codCareer = substring(ep->d_name,2,4);
  73. career = getCareer(codCareer);
  74. }
  75.  
  76. if((career != NULL) && (year != NULL))
  77. {
  78. //crea directorio
  79. sprintf(directory,"tarea/%s",year);
  80. mkdir(directory, S_IRWXU | S_IRWXG | S_IRWXO);
  81. sprintf(directory,"%s/%s",directory,career);
  82. mkdir(directory, S_IRWXU | S_IRWXG | S_IRWXO);
  83.  
  84. //mueve archivos
  85. sprintf(input,"tarea/%s",ep->d_name);
  86. sprintf(output,"%s/%s",directory,ep->d_name);
  87.  
  88. printf("de %s\n", input);
  89. printf("a %s\n", output);
  90.  
  91. if(move(input,output) != 0)
  92. perror("error mover\n");
  93.  
  94.  
  95.  
  96. moveRegister = getMoveRegister(career,year,ep->d_name);
  97. fputs (moveRegister, file);
  98. free(moveRegister);
  99. moveRegister = NULL;
  100. }
  101. }/*endif*/
  102. }/*endWhiel*/
  103.  
  104. fclose(file);
  105. (void) closedir (dp);
  106. }/*endif*/
  107. else
  108. perror ("No se puede crear el directorio");
  109.  
  110. printf("libera espacio\n");
  111. free(year);
  112. year = NULL;
  113. free(codYear);
  114. codYear = NULL;
  115. free(career);
  116. career = NULL;
  117. free(codCareer);
  118. codCareer = NULL;
  119. free(directory);
  120. directory = NULL;
  121. free(input);
  122. input = NULL;
  123. free(output);
  124. output = NULL;
  125.  
  126. return 0;
  127. }
  128.  
  129. /*obtiene subcadena de input(desde "a" a "b")*/
  130. char* substring(char* input,int a,int b)
  131. {
  132. char* output;
  133.  
  134. if(a>b) output = NULL;
  135. else
  136. output = (char*)malloc((b-a + 1)*sizeof(char));
  137.  
  138. strncpy(output, input+a, (b-a));
  139. output[b-a] = '\0';
  140. return output;
  141. }
  142.  
  143. /*transforma el codigo(año o carrera) en entero*/
  144. int CodToInt(char* cod)
  145. {
  146. int n,m;
  147.  
  148. n = (int)cod[0] - 48;
  149. m = (int)cod[1] - 48;
  150.  
  151. return (10*n+m);
  152. }
  153.  
  154.  
  155. /*funcion retorna el string del registro*/
  156. char* getMoveRegister(char* career, char* year, char* input)
  157. {
  158. char* output;
  159.  
  160. output = (char*)malloc(100*sizeof(char));
  161. sprintf(output, "El archivo '%s' fue movido a la carpeta 'tareas/%s/%s' [ x kb]\n", input, year,career);
  162.  
  163.  
  164. return(output);
  165. }
  166.  
  167.  
  168. /*funcion transforma codigo de año en el año (20-29)*/
  169. char* getYear(char* codYear)
  170. {
  171. int iCod;
  172. char* output;
  173. output = (char*)malloc(5*sizeof(char));
  174.  
  175. /*revisa que se valido el codigo*/
  176. iCod = CodToInt(codYear);
  177.  
  178. if((iCod >= 20) && (iCod <= 29))
  179. {
  180. output[0] = codYear[0];
  181. output[1] = '0';
  182. output[2] = '0';
  183. output[3] = codYear[1];
  184. output[4] = '\0';
  185. }
  186. else
  187. output = NULL;
  188.  
  189. return output;
  190. }
  191.  
  192.  
  193.  
  194. /*funcion que retorna string carrera segun el codigo*/
  195. char* getCareer(char* codCareer)
  196. {
  197. char* output;
  198. int iCod;
  199.  
  200. output = (char*)malloc(30*sizeof(char));
  201. iCod = CodToInt(codCareer);
  202.  
  203. switch(iCod)
  204. {
  205. case 4:
  206. output = "PlanComun";
  207. break;
  208. case 3:
  209. output = "PPI";
  210. break;
  211. case 30:
  212. output = "Telematica";
  213. break;
  214. case 21:
  215. output = "Electronica";
  216. break;
  217. case 23:
  218. output = "Electrica";
  219. break;
  220. case 90:
  221. output = "Especial";
  222. break;
  223. case 73:
  224. output = "Informatica";
  225. break;
  226. case 51:
  227. output = "Quimica";
  228. break;
  229. case 84:
  230. output = "Ambiental";
  231. break;
  232. case 41:
  233. output = "Mecanica";
  234. break;
  235. case 60:
  236. output = "Industrial";
  237. break;
  238. case 66:
  239. output = "Comercial";
  240. break;
  241. case 11:
  242. output = "ObrasCiviles";
  243. break;
  244. case 02:
  245. output = "Especial";
  246. break;
  247. case 44:
  248. output = "IDP";
  249. break;
  250. case 43:
  251. output = "MecanicaIndustrial";
  252. break;
  253. case 13:
  254. output = "Arquitectura";
  255. break;
  256. case 12:
  257. output = "ConstruccionCivil";
  258. break;
  259. default:
  260. output = NULL;
  261. }
  262.  
  263. return output;
  264.  
  265. }
  266.  
  267. //funcion para mover archivos
  268. int move(char* loc,char* dest)
  269. {
  270. FILE *source,*target;
  271. int byte;
  272.  
  273.  
  274. source = fopen(loc,"rb+");
  275. if(source==NULL)
  276. {
  277. printf("problema directorio fuente!!.");
  278. exit(0);
  279. }
  280.  
  281. target = fopen(dest,"wb+");
  282. if(target==NULL)
  283. {
  284. printf("problema directorio objetivo!!!.");
  285. fclose(source);
  286. exit(0);
  287. }
  288.  
  289. while(1)
  290. {
  291. byte=fgetc(source);
  292. if(byte==EOF)
  293. {
  294. break;
  295. }
  296. else
  297. fputc(byte,target);
  298.  
  299.  
  300. }
  301.  
  302. fclose(source);
  303. fclose(target);
  304.  
  305. if(unlink (loc) < 0)
  306. perror("error borrar\n");
  307. }
Add Comment
Please, Sign In to add comment