diff options
Diffstat (limited to 'calc.c')
| -rw-r--r-- | calc.c | 59 |
1 files changed, 42 insertions, 17 deletions
@@ -1,4 +1,5 @@ #include "calc.h" +#include "itoa_lut.h" #include <time.h> #include <errno.h> #include <stdint.h> @@ -27,6 +28,25 @@ uint32_t subnetmask_for_netspace(unsigned int desired_netspace) { return start; } +void write_ip(char **chunk_ptr, uint32_t ip) { + // local ptr so we don't have to deref every time + char *ptr = *chunk_ptr; + LutEntry e = {.bits = itoa_lut[(ip >> 24) & 0xff]}; + WRITE_STR(ptr, e.fields.chars, e.fields.len) + ptr[0] = '.'; + ptr += 1; + e.bits = itoa_lut[(ip >> 16) & 0xff]; + WRITE_STR(ptr, e.fields.chars, e.fields.len) + ptr[0] = '.'; + ptr += 1; + e.bits = itoa_lut[(ip >> 8) & 0xff]; + WRITE_STR(ptr, e.fields.chars, e.fields.len) + ptr[0] = '.'; + ptr += 1; + e.bits = itoa_lut[ip & 0xff]; + WRITE_STR(ptr, e.fields.chars, e.fields.len) + *chunk_ptr = ptr; +} int main(int argc, char** argv) { struct timespec start; @@ -64,31 +84,36 @@ int main(int argc, char** argv) { fprintf(out, "[\n"); // file start uint32_t offset = 0; int i = 0; + char chunk[1024] = {0}; while (i<req.count) { const uint32_t network = req.network + offset; const uint32_t first = network + 1; const uint32_t broadcast = first + ips_per_subnet - 2; const uint32_t last = broadcast - 1; offset += ips_per_subnet; + char* chunk_ptr = chunk; + WRITE_STR(chunk_ptr, "\t{\n\t\t\"Reseau\": \"", 16) + write_ip(&chunk_ptr, network); + WRITE_STR(chunk_ptr, "\",\n", 3) + WRITE_STR(chunk_ptr, "\t\t\"Premiere\": \"", 15) + write_ip(&chunk_ptr, first); + WRITE_STR(chunk_ptr, "\",\n", 3) + WRITE_STR(chunk_ptr, "\t\t\"Derniere\": \"", 15) + write_ip(&chunk_ptr, last); + WRITE_STR(chunk_ptr, "\",\n", 3) + WRITE_STR(chunk_ptr, "\t\t\"Broadcast\": \"", 16) + write_ip(&chunk_ptr, broadcast); + WRITE_STR(chunk_ptr, "\",\n", 3) + WRITE_STR(chunk_ptr, "\t\t\"Nbr\": \"", 10) + LutEntry e = {.bits = itoa_lut[ips_per_subnet - 2]}; + WRITE_STR(chunk_ptr, e.fields.chars, e.fields.len); if (i+1 == req.count) { - fprintf(out, - "\t{\n\t\t\"Reseau\": \"%hu.%hu.%hu.%hu\",\n\t\t\"Premiere\": \"%hu.%hu.%hu.%hu\",\n\t\t\"Derniere\": \"%hu.%hu.%hu.%hu\",\n\t\t\"Broadcast\": \"%hu.%hu.%hu.%hu\",\n\t\t\"Nbr\": \"%hu\"\n\t}\n", - UINT_TO_SEGMENTS(network), - UINT_TO_SEGMENTS(first), - UINT_TO_SEGMENTS(last), - UINT_TO_SEGMENTS(broadcast), - ips_per_subnet - 2 - ); - break; + WRITE_STR(chunk_ptr, "\"\n\t}\n", 5) + }else { + WRITE_STR(chunk_ptr, "\"\n\t},\n", 6) } - fprintf(out, - "\t{\n\t\t\"Reseau\": \"%hu.%hu.%hu.%hu\",\n\t\t\"Premiere\": \"%hu.%hu.%hu.%hu\",\n\t\t\"Derniere\": \"%hu.%hu.%hu.%hu\",\n\t\t\"Broadcast\": \"%hu.%hu.%hu.%hu\",\n\t\t\"Nbr\": \"%hu\"\n\t},\n", - UINT_TO_SEGMENTS(network), - UINT_TO_SEGMENTS(first), - UINT_TO_SEGMENTS(last), - UINT_TO_SEGMENTS(broadcast), - ips_per_subnet - 2 - ); + + fwrite(chunk, sizeof(char), chunk_ptr - chunk, out); i++; } fprintf(out, "]"); // file end |
