パスを含む多くのファイル名 (最大 250000 だと思います) を解析し、そこからいくつかの部分を抽出する必要があります。
次に例を示します。
オリジナル:/my/complete/path/to/80/01/a9/1d.pdf
必要:8001a91d
私が探している「パターン」は常に「/8」で始まります。抽出する必要がある部分は、8 桁の 16 進数の文字列です。
私の考えは次のとおりです(デモ用に簡略化されています):
/* original argument */
char *path = "/my/complete/path/to/80/01/a9/1d.pdf";
/* pointer to substring */
char *begin = NULL;
/* final char array to be build */
char *hex = (char*)malloc(9);
/* find "pattern" */
begin = strstr(path, "/8");
if(begin == NULL)
return 1;
/* jump to first needed character */
begin++;
/* copy the needed characters to target char array */
strncpy(hex, begin, 2);
strncpy(hex+2, begin+3, 2);
strncpy(hex+4, begin+6, 2);
strncpy(hex+6, begin+9, 2);
strncpy(hex+8, "\0", 1);
/* print final char array */
printf("%s\n", hex);
これは機能します。私はそれが最も賢い方法ではないと感じています。そして、自分には見えない罠があるかもしれないと。
それで、誰かがこのポインターシフト方法で何が危険なのか提案がありますか? あなたの意見では、どのような点が改善されますか?
Cはそのようにする機能を提供していs|/(8.)/(..)/(..)/(..)\.|\1\2\3\4|
ますか? 私の記憶が正しければ、一部のスクリプト言語にはそのような機能があります。お分かりでしょうが。