/* Thread worker callback function to process a single file */ VOID CALLBACK wcfunc (PTP_CALLBACK_INSTANCE Instance, PVOID Context, PTP_WORK Work) /* Count the number of characters, works, and lines in */ /* the file targ->filename */ /* NOTE: Simple version; results may differ from wc utility */ { WORK_OBJECT_ARG * threadArgs; MAPPED_FILE_HANDLE fhandle; int iThrd; unsigned int ch, c, nl, nw, nc; int isspace_c; // Current character int isspace_ch = 1; // Previous character. Assume space to count first word int error; unsigned int fsize; char *fin, *fend; iThrd = InterlockedIncrement (&WorkerId) - 1; threadArgs = (WORK_OBJECT_ARG *)Context; threadArgs->wcerror = 1; /* Error for now */ fin = (char*) map_file(threadArgs->filename, &fsize, &error, &fhandle); if (NULL == fin) { return; } fend = fin + fsize; ch = nw = nc = nl = 0; while (fin < fend) { c = *fin++; isspace_c = is_a_space(c); if (!isspace_c && isspace_ch) { nw++; } isspace_ch = isspace_c; if (c == '\n') { nl++; isspace_ch = 1; // Start looking for a new word } } UnMapFile(&fhandle); threadArgs->kchar = fsize; threadArgs->kword = nw; threadArgs->kline = nl; threadArgs->wcerror = 0; return; }
static const char *rstrip( const char *c ) { while ( is_a_space( *c ) ) --c; return c + 1; }