int cpbRestore (void) { int ok = 0; FILE *stream = cpbOpenFile("r"); if (stream) { size_t size = 0X1000; char buffer[size]; size_t length = 0; cpbClearContent(); ok = 1; do { size_t count = fread(&buffer[length], 1, (size - length), stream); int done = (length += count) < size; if (ferror(stream)) { logSystemError("fread"); ok = 0; } else { const char *next = buffer; size_t left = length; while (left > 0) { const char *start = next; wint_t wi = convertUtf8ToWchar(&next, &left); if (wi == WEOF) { length = next - start; if (left > 0) { logBytes(LOG_ERR, "invalid UTF-8 character", start, length); ok = 0; break; } memmove(buffer, start, length); } else { wchar_t wc = wi; if (!cpbAddContent(&wc, 1)) { ok = 0; break; } } } } if (done) break; } while (ok); if (fclose(stream) == EOF) { logSystemError("fclose"); ok = 0; } } return ok; }
int cpbSetContent (const wchar_t *characters, size_t length) { cpbClearContent(); return cpbAddContent(characters, length); }