Good day!

There is some need to download http files from the server. The problem is that the files may contain special characters that must be replaced with their code, for example, the well-known space with% 20, the character of the number with% e0, and so on. There are ready-made functions, if you believe the Internet, for Sharp, Php, ... I can not find it for vc ++. Do you really need to enter internal requirements for file names or write your own converter for replacing special characters for url.

The essence of the program is that the list of files is transferred to it, it downloads them from a certain site address, the specified section.

Thanks in advance for your help.

  • @ pincher1519, read your question more closely and looked at the text of the program. To encode non-ASCII characters (judging by the question you also need), in the pct_encode function pct_encode replace if (isspace (c) || ​​iscntrl (c) || ​​c == '%') {if (isspace (c) || ​​iscntrl (c) || ​​c == '%' || c> 126) { - avp

1 answer 1

I did it in principle with a different purpose, but maybe it will suit you.

 // avp 2013 по мотивам http://tools.ietf.org/html/rfc3986#section-2.1 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* Заменяет isspace() символы и % на %HH Input: ilen байт в char *input Result: байты в char *out максимальной длины osize их количество по адресу olen. Returns: количство "оттранслированных" байт из input */ size_t pct_encode (const char *input, size_t ilen, char *out, size_t osize, size_t *olen) { size_t i, l; for (l = i = 0; i < ilen && l < osize; i++) { int c = (unsigned char)input[i]; if (isspace(c) || iscntrl(c) || c == '%') { if (l + 2 < osize) { sprintf(out + l, "%%%02X", c); l += 3; } else // нет места break; } else out[l++] = c; } *olen = l; return i; } // вызывать только с VALID HEXCODE static inline int hex (int c) { if ('0' <= c && c <= '9') c -= '0'; else if ('A' <= c && c <= 'F') c -= ('A' - 10) ; else c -= ('a' - 10); return c; } /* Заменяет %HH в тексте на байты Input: ilen байт в char *input флаг ignerr - игнорировать ошибки формата %HH Result: байты в char *out максимальной длины osize их количество по адресу olen. Returns: количство "оттранслированных" байт из input */ size_t pct_decode (const char *input, size_t ilen, char *out, size_t osize, size_t *olen, int ignerr) { size_t i, l; for (l = i = 0; i < ilen && l < osize; i++) { int c1, c2; if (input[i] == '%') { if (i + 2 < ilen && isxdigit(c1 = input[i + 1]) && isxdigit(c2 = input[i + 2])) { out[l++] = hex(c1) * 16 + hex(c2); i += 2; } else if (ignerr) out[l++] = input[i]; else break; } else out[l++] = input[i]; } *olen = l; return i; } 
  • @avp, Thanks, let's try to run away the functions and compare with the expected result. - pincher1519 4:38 pm
  • @avp, Thank you again, your solution is also quite viable, but the developers made their sledges, which passed all the tests of testers. Unfortunately, this is a self-written implementation and does not depend on the changing technologies of the OS and the Internet. Tomorrow is allowed to write in the file names of the character? or *, and all the logic will be broken ... - pincher1519