int main(void) { static struct {int a[8], b[8], sum ; } testcase[] = { {{0,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,1},0}, {{1,1,1,1,1,1,1,1},{1,0,0,0,0,0,0,0},0}, {{1,0,1,0,1,0,1,0},{1,0,1,0,1,0,1,0},-86}, {{1,1,0,1,0,0,0,0},{0,1,1,0,1,1,1,1},1}, {{1,0,1,0,1,0,1,0},{0,1,0,1,0,1,0,1},-1} } ; int k ; InitializeHardware(HEADER, PROJECT_NAME) ; for (;;) { for (k = 0; k < ENTRIES(testcase); k++) { int *a = testcase[k].a ; int *b = testcase[k].b ; int sum[8] ; uint32_t before, after, cycles ; printf(" Test Case %d: ", k+1) ; PrintBits(a) ; printf("+") ; PrintBits(b) ; printf("\n") ; printf(" Correct Sum: ") ; PrintByte(testcase[k].sum) ; printf("\n") ; before = GetClockCycleCount() ; AddBinary(a, b, sum) ; after = GetClockCycleCount() ; cycles = after - before ; printf(" Your Sum: ") ; if (PrintBits(sum) != (uint8_t) testcase[k].sum) printf(" %s", ERROR_FLAG) ; printf("\n") ; printf(" Clock Cycles: %lu\n", cycles) ; printf("\n") ; WaitForPushButton() ; } printf("Press button to start over.\n") ; WaitForPushButton() ; ClearDisplay() ; } }
int main(void) { static struct {int a[8], b[8], sum ; } testcase[] = { {{0,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,1},0}, {{1,1,1,1,1,1,1,1},{1,0,0,0,0,0,0,0},0}, {{1,0,1,0,1,0,1,0},{1,0,1,0,1,0,1,0},-86}, {{1,1,0,1,0,0,0,0},{0,1,1,0,1,1,1,1},1}, {{1,0,1,0,1,0,1,0},{0,1,0,1,0,1,0,1},-1} } ; int k ; for (k = 0; k < ENTRIES(testcase); k++) { int *a = testcase[k].a ; int *b = testcase[k].b ; int sum[8] ; uint32_t before, after, cycles ; printf(" Test Case %d: \n", k+1) ; PrintBits(a) ; printf("\n") ; PrintBits(b) ; printf("\n") ; printf(" Correct Sum: \n") ; PrintByte(testcase[k].sum); printf("\n") ; AddBinary(a, b, sum) ; printf(" Your Sum: \n") ; PrintBits(sum); printf("\n") ; printf("\n") ; } printf("Press button to start over.\n") ; }
size_t atcprintf(char *buffer, size_t maxlen, const char *format, IPluginContext *pCtx, const cell_t *params, int *param) { if (!buffer || !maxlen) { return 0; } int arg; int args = params[0]; char *buf_p; char ch; int flags; int width; int prec; int n; char sign; const char *fmt; size_t llen = maxlen - 1; buf_p = buffer; arg = *param; fmt = format; while (true) { // run through the format string until we hit a '%' or '\0' for (ch = *fmt; llen && ((ch = *fmt) != '\0') && (ch != '%'); fmt++) { *buf_p++ = ch; llen--; } if ((ch == '\0') || (llen <= 0)) { goto done; } // skip over the '%' fmt++; // reset formatting state flags = 0; width = 0; prec = -1; sign = '\0'; rflag: ch = *fmt++; reswitch: switch(ch) { case '-': { flags |= LADJUST; goto rflag; } case '.': { n = 0; while(is_digit((ch = *fmt++))) { n = 10 * n + (ch - '0'); } prec = (n < 0) ? -1 : n; goto reswitch; } case '0': { flags |= ZEROPAD; goto rflag; } case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { n = 0; do { n = 10 * n + (ch - '0'); ch = *fmt++; } while(is_digit(ch)); width = n; goto reswitch; } case 'c': { CHECK_ARGS(0); if (!llen) { goto done; } char *c; pCtx->LocalToString(params[arg], &c); *buf_p++ = *c; llen--; arg++; break; } case 'b': { CHECK_ARGS(0); cell_t *value; pCtx->LocalToPhysAddr(params[arg], &value); AddBinary(&buf_p, llen, *value, width, flags); arg++; break; } case 'd': case 'i': { CHECK_ARGS(0); cell_t *value; pCtx->LocalToPhysAddr(params[arg], &value); AddInt(&buf_p, llen, static_cast<int>(*value), width, flags); arg++; break; } case 'u': { CHECK_ARGS(0); cell_t *value; pCtx->LocalToPhysAddr(params[arg], &value); AddUInt(&buf_p, llen, static_cast<unsigned int>(*value), width, flags); arg++; break; } case 'f': { CHECK_ARGS(0); cell_t *value; pCtx->LocalToPhysAddr(params[arg], &value); AddFloat(&buf_p, llen, sp_ctof(*value), width, prec, flags); arg++; break; } case 'L': { CHECK_ARGS(0); cell_t *value; pCtx->LocalToPhysAddr(params[arg], &value); char buffer[255]; if (*value) { CPlayer *player = g_Players.GetPlayerByIndex(*value); if (!player || !player->IsConnected()) { return pCtx->ThrowNativeError("Client index %d is invalid", *value); } const char *auth = player->GetAuthString(); if (!auth || auth[0] == '\0') { auth = "STEAM_ID_PENDING"; } int userid = engine->GetPlayerUserId(player->GetEdict()); UTIL_Format(buffer, sizeof(buffer), "%s<%d><%s><>", player->GetName(), userid, auth); } else { UTIL_Format(buffer, sizeof(buffer), "Console<0><Console><Console>"); } AddString(&buf_p, llen, buffer, width, prec); arg++; break; } case 'N': { CHECK_ARGS(0); cell_t *value; pCtx->LocalToPhysAddr(params[arg], &value); const char *name = "Console"; if (*value) { CPlayer *player = g_Players.GetPlayerByIndex(*value); if (!player || !player->IsConnected()) { return pCtx->ThrowNativeError("Client index %d is invalid", *value); } name = player->GetName(); } AddString(&buf_p, llen, name, width, prec); arg++; break; } case 's': { CHECK_ARGS(0); char *str; int err; if ((err=pCtx->LocalToString(params[arg], &str)) != SP_ERROR_NONE) { pCtx->ThrowNativeErrorEx(err, "Could not deference string"); return 0; } AddString(&buf_p, llen, str, width, prec); arg++; break; } case 'T': { CHECK_ARGS(1); char *key; bool error; size_t res; cell_t *target; pCtx->LocalToString(params[arg++], &key); pCtx->LocalToPhysAddr(params[arg++], &target); res = Translate(buf_p, llen, pCtx, key, *target, params, &arg, &error); if (error) { return 0; } buf_p += res; llen -= res; break; } case 't': { CHECK_ARGS(0); char *key; bool error; size_t res; cell_t target = g_SourceMod.GetGlobalTarget(); pCtx->LocalToString(params[arg++], &key); res = Translate(buf_p, llen, pCtx, key, target, params, &arg, &error); if (error) { return 0; } buf_p += res; llen -= res; break; } case 'X': { CHECK_ARGS(0); cell_t *value; pCtx->LocalToPhysAddr(params[arg], &value); flags |= UPPERDIGITS; AddHex(&buf_p, llen, static_cast<unsigned int>(*value), width, flags); arg++; break; } case 'x': { CHECK_ARGS(0); cell_t *value; pCtx->LocalToPhysAddr(params[arg], &value); AddHex(&buf_p, llen, static_cast<unsigned int>(*value), width, flags); arg++; break; } case '%': { if (!llen) { goto done; } *buf_p++ = ch; llen--; break; } case '\0': { if (!llen) { goto done; } *buf_p++ = '%'; llen--; goto done; } default: { if (!llen) { goto done; } *buf_p++ = ch; llen--; break; } } } done: *buf_p = '\0'; *param = arg; return (maxlen - llen - 1); }
size_t gnprintf(char *buffer, size_t maxlen, const char *format, void **args) { if (!buffer || !maxlen) { return 0; } int arg = 0; char *buf_p; char ch; int flags; int width; int prec; int n; char sign; const char *fmt; size_t llen = maxlen - 1; buf_p = buffer; fmt = format; while (true) { // run through the format string until we hit a '%' or '\0' for (ch = *fmt; llen && ((ch = *fmt) != '\0') && (ch != '%'); fmt++) { *buf_p++ = ch; llen--; } if ((ch == '\0') || (llen <= 0)) { goto done; } // skip over the '%' fmt++; // reset formatting state flags = 0; width = 0; prec = -1; sign = '\0'; rflag: ch = *fmt++; reswitch: switch(ch) { case '-': { flags |= LADJUST; goto rflag; } case '.': { n = 0; while(is_digit((ch = *fmt++))) { n = 10 * n + (ch - '0'); } prec = (n < 0) ? -1 : n; goto reswitch; } case '0': { flags |= ZEROPAD; goto rflag; } case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { n = 0; do { n = 10 * n + (ch - '0'); ch = *fmt++; } while(is_digit(ch)); width = n; goto reswitch; } case 'c': { if (!llen) { goto done; } char *c = (char *)args[arg]; *buf_p++ = *c; llen--; arg++; break; } case 'b': { int *value = (int *)args[arg]; AddBinary(&buf_p, llen, *value, width, flags); arg++; break; } case 'd': case 'i': { int *value = (int *)args[arg]; AddInt(&buf_p, llen, *value, width, flags); arg++; break; } case 'u': { unsigned int *value = (unsigned int *)args[arg]; AddUInt(&buf_p, llen, *value, width, flags); arg++; break; } case 'f': { float *value = (float *)args[arg]; AddFloat(&buf_p, llen, *value, width, prec, flags); arg++; break; } case 's': { const char *str = (const char *)args[arg]; AddString(&buf_p, llen, str, width, prec); arg++; break; } case 'X': { unsigned int *value = (unsigned int *)args[arg]; flags |= UPPERDIGITS; AddHex(&buf_p, llen, *value, width, flags); arg++; break; } case 'x': { unsigned int *value = (unsigned int *)args[arg]; AddHex(&buf_p, llen, *value, width, flags); arg++; break; } case '%': { if (!llen) { goto done; } *buf_p++ = ch; llen--; break; } case '\0': { if (!llen) { goto done; } *buf_p++ = '%'; llen--; goto done; } default: { if (!llen) { goto done; } *buf_p++ = ch; llen--; break; } } } done: *buf_p = '\0'; return (maxlen - llen - 1); }
size_t atcprintf(D *buffer, size_t maxlen, const S *format, AMX *amx, cell *params, int *param) { int arg; int args = params[0] / sizeof(cell); D *buf_p; D ch; int flags; int width; int prec; int n; //char sign; const S *fmt; size_t llen = maxlen; buf_p = buffer; arg = *param; fmt = format; while (true) { // run through the format string until we hit a '%' or '\0' for (ch = static_cast<D>(*fmt); llen && ((ch = static_cast<D>(*fmt)) != '\0' && ch != '%'); fmt++) { *buf_p++ = static_cast<D>(ch); llen--; } if (ch == '\0' || llen <= 0) goto done; // skip over the '%' fmt++; // reset formatting state flags = 0; width = 0; prec = -1; //sign = '\0'; rflag: ch = static_cast<D>(*fmt++); reswitch: switch(ch) { case '-': flags |= LADJUST; goto rflag; case '.': n = 0; while( is_digit( ( ch = static_cast<D>(*fmt++)) ) ) n = 10 * n + ( ch - '0' ); prec = n < 0 ? -1 : n; goto reswitch; case '0': flags |= ZEROPAD; goto rflag; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': n = 0; do { n = 10 * n + ( ch - '0' ); ch = static_cast<D>(*fmt++); } while( is_digit( ch ) ); width = n; goto reswitch; case 'c': CHECK_ARGS(0); *buf_p++ = static_cast<D>(*get_amxaddr(amx, params[arg])); llen--; arg++; break; case 'b': CHECK_ARGS(0); AddBinary(&buf_p, llen, *get_amxaddr(amx, params[arg]), width, flags); arg++; break; case 'd': case 'i': CHECK_ARGS(0); AddInt(&buf_p, llen, *get_amxaddr(amx, params[arg]), width, flags); arg++; break; case 'u': CHECK_ARGS(0); AddUInt(&buf_p, llen, static_cast<unsigned int>(*get_amxaddr(amx, params[arg])), width, flags); arg++; break; case 'f': CHECK_ARGS(0); AddFloat(&buf_p, llen, amx_ctof(*get_amxaddr(amx, params[arg])), width, prec, flags); arg++; break; case 'X': CHECK_ARGS(0); flags |= UPPERDIGITS; AddHex(&buf_p, llen, static_cast<unsigned int>(*get_amxaddr(amx, params[arg])), width, flags); arg++; break; case 'x': CHECK_ARGS(0); AddHex(&buf_p, llen, static_cast<unsigned int>(*get_amxaddr(amx, params[arg])), width, flags); arg++; break; case 'a': { CHECK_ARGS(0); // %a is passed a pointer directly to a cell string. cell* ptr=reinterpret_cast<cell*>(*get_amxaddr(amx, params[arg])); if (!ptr) { LogError(amx, AMX_ERR_NATIVE, "Invalid vector string handle provided (%d)", *get_amxaddr(amx, params[arg])); return 0; } AddString(&buf_p, llen, ptr, width, prec); arg++; break; } case 's': CHECK_ARGS(0); AddString(&buf_p, llen, get_amxaddr(amx, params[arg]), width, prec); arg++; break; case 'L': case 'l': { const char *lang; int len; if (ch == 'L') { CHECK_ARGS(1); auto currParam = params[arg++]; lang = playerlang(*get_amxaddr(amx, currParam)); if (!lang) lang = get_amxstring(amx, currParam, 2, len); } else { CHECK_ARGS(0); lang = playerlang(g_langMngr.GetDefLang()); } const char *key = get_amxstring(amx, params[arg++], 3, len); const char *def = translate(amx, lang, key); if (!def) { static char buf[255]; ke::SafeSprintf(buf, sizeof(buf), "ML_NOTFOUND: %s", key); def = buf; } size_t written = atcprintf(buf_p, llen, def, amx, params, &arg); buf_p += written; llen -= written; break; } case 'N': { CHECK_ARGS(0); cell *addr = get_amxaddr(amx, params[arg]); char buffer[255]; if (*addr) { CPlayer *player = NULL; if (*addr >= 1 && *addr <= gpGlobals->maxClients) { player = GET_PLAYER_POINTER_I(*addr); } if (!player || !player->initialized) { LogError(amx, AMX_ERR_NATIVE, "Client index %d is invalid", *addr); return 0; } const char *auth = GETPLAYERAUTHID(player->pEdict); if (!auth || auth[0] == '\0') { auth = "STEAM_ID_PENDING"; } int userid = GETPLAYERUSERID(player->pEdict); ke::SafeSprintf(buffer, sizeof(buffer), "%s<%d><%s><%s>", player->name.chars(), userid, auth, player->team.chars()); } else { ke::SafeSprintf(buffer, sizeof(buffer), "Console<0><Console><Console>"); } AddString(&buf_p, llen, buffer, width, prec); arg++; break; } case 'n': { CHECK_ARGS(0); cell *addr = get_amxaddr(amx, params[arg]); const char *name = "Console"; if (*addr) { CPlayer *player = NULL; if (*addr >= 1 && *addr <= gpGlobals->maxClients) { player = GET_PLAYER_POINTER_I(*addr); } if (!player || !player->initialized) { LogError(amx, AMX_ERR_NATIVE, "Client index %d is invalid", *addr); return 0; } name = player->name.chars(); } AddString(&buf_p, llen, name, width, prec); arg++; break; } case '%': *buf_p++ = static_cast<D>(ch); if (!llen) goto done; llen--; break; case '\0': *buf_p++ = static_cast<D>('%'); if (!llen) goto done; llen--; goto done; break; default: *buf_p++ = static_cast<D>(ch); if (!llen) goto done; llen--; break; } } done: *buf_p = static_cast<D>(0); *param = arg; /* if max buffer length consumed, make sure we don't truncate a multi-byte character */ if (llen <= 0 && *(buf_p - 1) & 1 << 7) { llen += UTIL_CheckValidChar(buf_p - 1); *(buf_p - llen) = static_cast<D>(0); } return maxlen-llen; }