Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <io.h>
  3. #include <string.h>
  4.  
  5. #include <vector>
  6. #include <string>
  7.  
  8. void Usage(const char* msg = NULL)
  9. {
  10. if (msg)
  11. printf("%sn", msg);
  12. printf("Usage: coderep (prefix) (infile) (listfile) (outfile)n");
  13. }
  14.  
  15. char* OpenAndReadFile(const char* filename)
  16. {
  17. FILE* fp = fopen(filename, "rb");
  18. if (!fp)
  19. return NULL;
  20. fseek(fp, 0, SEEK_END);
  21. int size = (int)ftell(fp);
  22. fseek(fp, 0, SEEK_SET);
  23. char* buf = new char[size + 2];
  24. fread(buf, 1, size, fp);
  25. buf[size] = '';
  26. fclose(fp);
  27. return buf;
  28. }
  29.  
  30. void ReplaceAll(std::string& str, const std::string& from, const std::string& to)
  31. {
  32. size_t start_pos = 0;
  33. while ((start_pos = str.find(from, start_pos)) != std::string::npos)
  34. {
  35. str.replace(start_pos, from.length(), to);
  36. start_pos += to.length();
  37. }
  38. }
  39.  
  40. char* rndCh = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  41. int rndLen = 62;
  42.  
  43. // Generate random string from number
  44. void genRand(int index, std::string* str)
  45. {
  46. std::vector<int> ni;
  47. while (index > 0)
  48. {
  49. ni.push_back(index % 62);
  50. index = index / 62;
  51. }
  52. if (!ni.size())
  53. ni.push_back(0);
  54.  
  55. std::vector<int>::reverse_iterator ip;
  56. for (ip = ni.rbegin(); ip != ni.rend(); ++ip)
  57. (*str) += rndCh[*ip];
  58. }
  59.  
  60. int main(int argc, char* argv[])
  61. {
  62. if (argc != 5)
  63. {
  64. Usage();
  65. return 1;
  66. }
  67.  
  68. // Read inputs and check parms
  69. const char* prefix = argv[1];
  70. char* code = OpenAndReadFile(argv[2]);
  71. if (!code || !*code)
  72. {
  73. Usage("Failed reading code");
  74. return 1;
  75. }
  76. char* list = OpenAndReadFile(argv[3]);
  77. if (!list || !*list)
  78. {
  79. delete[] code;
  80. Usage("Failed reading list");
  81. return 1;
  82. }
  83. const char* outfile = argv[4];
  84. if (!outfile || !*outfile)
  85. {
  86. Usage("Need output file");
  87. return 1;
  88. }
  89.  
  90. // Split list
  91. std::string scode(code);
  92. std::vector<std::string> vlist;
  93. std::string acc;
  94. const char* lp = list;
  95. while (*lp)
  96. {
  97. if (*lp == L',')
  98. {
  99. vlist.push_back(acc);
  100. acc.clear();
  101. }
  102. else
  103. {
  104. acc += *lp;
  105. }
  106. ++lp;
  107. }
  108. if (acc.size() > 0)
  109. vlist.push_back(acc);
  110.  
  111. // Do translation
  112. int index = 0;
  113. std::string rstr;
  114. std::vector<std::string>::iterator ilist;
  115. for (ilist = vlist.begin(); ilist != vlist.end(); ++ilist)
  116. {
  117. rstr = prefix;
  118. genRand(index, &rstr);
  119. ++index;
  120.  
  121. ReplaceAll(scode, *ilist, rstr);
  122. }
  123.  
  124. // Write results
  125. FILE* outp = fopen(outfile, "wb");
  126. if (!outp)
  127. {
  128. Usage("Error creating output file");
  129. return 1;
  130. }
  131. fwrite(scode.c_str(), sizeof(char), scode.size(), outp);
  132. fclose(outp);
  133.  
  134. return 0;
  135. }
  136.  
  137. rndCh = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  138. rndLen = rndCh.length;
  139.  
  140. // Generate random string from number
  141. function genRand(index) {
  142. var ni = [], rv = "";
  143. while (index > 0) {
  144. ni.push(index % 62);
  145. index = ~~(index / 62);
  146. }
  147. if (!ni.length) {
  148. ni.push(0);
  149. }
  150. while (ni.length > 0) {
  151. rv += rndCh[ni.pop()];
  152. }
  153. return rv;
  154. }
  155.  
  156. $(document).ready(function() {
  157. $("#gobutton").on("click", function(e) {
  158. var prefix, code, rlist, ir, nrep, nrval;
  159.  
  160. prefix = $("#myprefix").val();
  161. if (!prefix) {
  162. alert("Need prefix");
  163. return;
  164. }
  165. code = $("#mycode").val();
  166. if (!code) {
  167. alert("Need code");
  168. return;
  169. }
  170. rlist = $("#mylist").val();
  171. if (!rlist || !rlist.length) {
  172. alert("Need replacement list");
  173. return;
  174. }
  175. rlist = rlist.split(",");
  176.  
  177. for (ir = 0; ir < rlist.length; ++ir) {
  178. if (rlist[ir]) {
  179. // Generate next random
  180. nrval = prefix + genRand(ir);
  181. nrep = new RegExp(rlist[ir], "g");
  182. code = code.replace(nrep, nrval);
  183. }
  184. }
  185.  
  186. $("#myoutput").val(code);
  187. });
  188. });
  189.  
  190. sed -i -e 's:bfirstNeedleb:firstReplacement:g' -e 's:bsecondNeedleb:secondReplacement:g' [... and so on...] /path/to/first/file /path/to/second/file [etc...]
  191.  
  192. sed -i -e 's:bget(([A-Z]{1,3})[a-zA-Z]*()):g1:g'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement