Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. #pragma once
  2. void SvgWork(string input, string output, float kx, float ky)
  3. {
  4. char
  5. ch,
  6. *outfile = new char[output.length() + 1];
  7. ifstream fin;
  8. FILE *fout;
  9. string
  10. key_osvg = "<svg",
  11. key_csvg = "</sv",
  12. key_obody = "<body",
  13. key_cbody = "</bod",
  14. atribute,
  15. to_out,
  16. values,
  17. values_changed,
  18. bword,
  19. word;
  20. bool
  21. figure2 = false,
  22. atag_open = false,
  23. atag_close = false,
  24. btag_open = false,
  25. btag_close = false,
  26. ctag_open = false,
  27. ctag_close = false,
  28. first_exit = false,
  29. firstcloser=false,
  30. is = false,
  31. space = false,
  32. wdot1 = false,
  33. wdot2 = false,
  34. body = false,
  35. svg = false;
  36. int
  37. j = 0,
  38. M = 4,
  39. i = 0,
  40. ctags_closecount=0,
  41. N = 6;
  42. float k = kx < ky ? kx : ky;
  43. outfile = stoch(output);
  44. fin.open(input);
  45. fopen_s(&fout, outfile, "w");
  46. if (!fin.is_open())
  47. {
  48. cout << "Ошибка открытия файла!" << endl; fin.close(); fclose(fout);
  49. }
  50. else
  51. {
  52. while (fin.get(ch))
  53. {
  54. /* body */
  55. if (body)
  56. {
  57. /* Внутри тега */
  58. if (ch == '<')
  59. {
  60. btag_open = true;
  61. btag_close = false;
  62. first_exit = false;
  63. }
  64. if (ch == '>')
  65. {
  66. btag_open = false;
  67. btag_close = true;
  68. }
  69. /* Фильтр на позицию внутри тега svg */
  70. if (btag_open && !btag_close)
  71. {
  72. if ((j < M) && !first_exit)
  73. {
  74. ++j;
  75. bword += ch;
  76. }
  77. else
  78. {
  79. if (bword == key_osvg)
  80. {
  81.  
  82. for (int i=0;i< bword.length()-1;++i)
  83. fprintf(fout, "%c", bword[i]);
  84. fprintf(fout, "%c", '\"');
  85. //cout << stoch(bword);
  86. svg = true;
  87.  
  88. }
  89. if (bword == key_csvg)
  90. {
  91. fprintf(fout, "%c", 'g');
  92. fprintf(fout, "%c", '>');
  93. svg = false;
  94. }
  95. first_exit = true;
  96. bword = "";
  97. j = 0;
  98. }
  99. }
  100. /* svg */
  101. if (svg)
  102. {
  103.  
  104. if (ch == '>') {
  105. ctag_close = true;
  106. ctag_open = false;
  107. }
  108. if (ch == '<') {
  109. ctag_close = false;
  110. ctag_open = true;
  111. }
  112.  
  113. if ((ctag_close)&&(ch=='>')) {
  114. ctags_closecount++;
  115. }
  116. if ((ctags_closecount == 1) && (ch >= 'a') && (ch <= 'z')) {
  117. fprintf(fout, "%c", '<');
  118. //cout << "<";
  119. ctags_closecount++;
  120. }
  121.  
  122.  
  123. if (wdot2)
  124. {
  125. values_changed = ChangeSvg(atribute, values, k);
  126. to_out = atribute + '=' + '\"' + values_changed + '\"';
  127. //cout<<to_out<<endl;
  128. char *outword = new char[to_out.length + 1];
  129. outword = stoch(to_out);
  130. for (int k= 0; k <= to_out.length(); ++i) {
  131. fprintf(fout, "&c", outword[k]);
  132. cout << outword[k];
  133. }
  134. fprintf(fout, "&s", outword);
  135. cout << "!";
  136. cout << endl;
  137. //cout << stoch(to_out);
  138.  
  139. atribute = values = "";
  140. is = space = wdot1 = wdot2 = false;
  141. }
  142. if (!space && !wdot2&& ch != '<') fprintf(fout, "%c", ch);
  143.  
  144. /* Атрибут */
  145. if (ch == '=')
  146. is = true;
  147. if (space && !is)
  148. atribute += ch;
  149.  
  150. /* Значение атрибута */
  151. if (wdot1 && !wdot2 && ch != '\"') values += ch;
  152. if (ch == '\"' && wdot1) wdot2 = true;
  153. if (ch == '\"' && !wdot1) wdot1 = true;
  154. if (ch == ' ' && !wdot1 && !wdot2) space = true;
  155. if (ch == '<') { space = false; atribute = ""; }
  156. if (ch == '<' && figure2) fprintf(fout, "%с", ch);
  157. if (ch == '<' && !figure2) figure2 = true;
  158. }
  159. else
  160. {
  161. fprintf(fout, "%c", ch);
  162. }
  163.  
  164. }
  165. else fprintf(fout, "%c", ch);
  166. /* Внутри тега */
  167. if (ch == '<')
  168. {
  169. atag_open = true;
  170. atag_close = false;
  171. }
  172. if (ch == '>')
  173. {
  174. atag_close = true;
  175. atag_open = false;
  176. word = "";
  177. i = 0;
  178. }
  179. /* Фильтр на позицию внутри тега <body> */
  180. if (atag_open && !atag_close)
  181. {
  182. if (i < N)
  183. {
  184. word += ch;
  185. ++i;
  186. if (word == key_obody)
  187. body = true;
  188. if (word == key_cbody)
  189. body = false;
  190. }
  191. else
  192. {
  193. word = "";
  194. i = 0;
  195. }
  196. }
  197. }
  198. }
  199. fin.close();
  200. fclose(fout);
  201. }
  202.  
  203. /* Меняет значения атрибутов svg */
  204. string ChangeSvg(string name, string value, float k)
  205. {
  206. string result;
  207.  
  208. if (CmprSvg(name))
  209. {
  210. result = ChangeNumSvg(value, k);
  211. return result;
  212. }
  213. return value;
  214. }
  215.  
  216. /* Сравнивает атрибут с библиотекой изменяемых атрибутов */
  217. bool CmprSvg(string word)
  218. {
  219. string
  220. current_lyb_word,
  221. svg_library = "cx cy rx ry r x1 x2 y1 y2 width height viewBox";
  222. char
  223. ch;
  224. for (int i = 0; i <= svg_library.length(); ++i)
  225. {
  226. ch = svg_library[i];
  227. if (ch != ' ' && i != svg_library.length())
  228. current_lyb_word += ch;
  229. else
  230. {
  231. if (word == current_lyb_word)
  232. return 1;
  233. current_lyb_word = "";
  234. }
  235. }
  236. return 0;
  237. }
  238.  
  239. /* Меняет числа в атрибутах */
  240. string ChangeNumSvg(string word, float k)
  241. {
  242. string
  243. result,
  244. current_num;
  245. char
  246. ch;
  247. for (int i = 0; i <= word.length(); ++i)
  248. {
  249. ch = word[i];
  250. if (ch != ' ' && i != word.length())
  251. {
  252. current_num += ch;
  253. }
  254. else
  255. {
  256. current_num = ChangeNumber(current_num, k);
  257. result += (current_num + ' ');
  258. current_num = "";
  259. }
  260. }
  261. result += '\b';
  262. return result;
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement