| /* |
| * Copyright (c) 2016 Nordic Semiconductor ASA |
| * Copyright (c) 2016 Vinayak Kariappa Chettimada |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| #include <stdint.h> |
| #include "util.h" |
| |
| /* Convert the integer D to a string and save the string in BUF. If |
| * BASE is equal to 'd', interpret that D is decimal, and if BASE is |
| * equal to 'x', interpret that D is hexadecimal. |
| */ |
| void util_itoa(char *buf, int base, int d) |
| { |
| char *p = buf; |
| char *p1, *p2; |
| unsigned long ud = d; |
| int divisor = 10; |
| |
| /* If %d is specified and D is minus, put `-' in the head. */ |
| if (base == 'd' && d < 0) { |
| *p++ = '-'; |
| buf++; |
| ud = -d; |
| } else if (base == 'x') |
| divisor = 16; |
| |
| /* Divide UD by DIVISOR until UD == 0. */ |
| do { |
| int remainder = ud % divisor; |
| *p++ = |
| (remainder < 10) ? remainder + '0' : remainder + 'a' - 10; |
| } while (ud /= divisor); |
| |
| /* Terminate BUF. */ |
| *p = 0; |
| |
| /* Reverse BUF. */ |
| p1 = buf; |
| p2 = p - 1; |
| while (p1 < p2) { |
| char tmp = *p1; |
| *p1 = *p2; |
| *p2 = tmp; |
| p1++; |
| p2--; |
| } |
| } |
| |
| int util_atoi(char *s) |
| { |
| int val = 0; |
| |
| while (*s) { |
| if (*s < '0' || *s > '9') |
| return val; |
| val = (val * 10) + (*s - '0'); |
| s++; |
| } |
| return val; |
| } |
| |
| /* Format a string and print it on the screen, just like the libc |
| * function printf. |
| */ |
| void util_sprintf(char *str, const char *format, ...) |
| { |
| char **arg = (char **)&format; |
| int c; |
| char buf[20]; |
| |
| arg++; |
| |
| while ((c = *format++)) { |
| if (c != '%') |
| *str++ = c; |
| else { |
| char *p; |
| |
| c = *format++; |
| switch (c) { |
| case 'd': |
| case 'u': |
| case 'x': |
| util_itoa(buf, c, *((int *)arg++)); |
| p = buf; |
| goto string; |
| case 's': |
| p = *arg++; |
| if (!p) |
| p = "(null)"; |
| string: |
| while (*p) |
| *str++ = *p++; |
| break; |
| default: |
| *str++ = (*((int *)arg++)); |
| break; |
| } |
| } |
| } |
| |
| *str = 0; |
| } |
| |
| uint8_t util_ones_count_get(uint8_t *octets, uint8_t octets_len) |
| { |
| uint8_t one_count = 0; |
| |
| while (octets_len--) { |
| uint8_t bite; |
| |
| bite = *octets; |
| while (bite) { |
| bite &= (bite - 1); |
| one_count++; |
| } |
| octets++; |
| } |
| |
| return one_count; |
| } |