bool realloc (size_t N, void (*pferror) ()) { if (this->realloc (N)) { return true; } else { pferror(); return false; } }
dynarray (size_t N, void (*pferror) ()) : m_num_elements {N} { m_ptr = (T*) quan::malloc (N * sizeof (T)); if (m_ptr == nullptr) { pferror(); m_num_elements = 0; } }
/** Create value from a string. * @param src_str_len The length of the actual buffer (not the number size). * @param src_str The source string. * @return The value in the source string. * @note Must have first char be digit * @note Currently we only do Long Ints */ value_t string_to_value(size_t src_str_len, char const *src_str) { value_t val; // char buf[40]; // for long int: log10(2^128) ~ 39 // size_t index; if((src_str_len == 0) || (src_str_len > 40)) pferror("string_to_value","string length given is not within usage range"); /* Prep. the number string to use in atol() */ // copy number digits // for (index = 0; (index < src_str_len) && (index < (40-1)) /*need null byte*/ && IS_DIGIT(src_str[index]); index++) { // buf[index] = src_str[index]; // } // place null-byte // buf[index] = '\0'; /* Create the value using atol() */ val.type = VAL_LINT; ///< @note Here we assume it is a Long Int val.data.lint = (sys_int_long) atol(src_str); ///< @warning Uses system atol() function at index 0 on the bare string given return val; }
int main(int argc, char* argv[]) { int retval; int frameFd, ctlListenFd, ledFd; struct sockaddr_in addr; in_port_t portNum= 3456; int option; const char* devPath= "/dev/ledfloor0"; bool verbose= false; int ctlFd= 0; ledFd= open(devPath, O_WRONLY); if (ledFd == -1) { pferror(errno, "Can't open ledfloor device"); abort(); } frameFd= socket(AF_INET, SOCK_DGRAM, 0); if (frameFd == -1) { pferror(errno, "line %d", __LINE__); abort(); } addr.sin_family= AF_INET; addr.sin_port= htons(portNum); addr.sin_addr.s_addr= htonl(INADDR_ANY); retval= bind(frameFd, (struct sockaddr*) &addr, sizeof(addr)); if (retval == -1) { pferror(errno, "line %d", __LINE__); abort(); } ctlListenFd= socket(AF_INET, SOCK_STREAM, 0); if (ctlListenFd == -1) { pferror(errno, "line %d", __LINE__); abort(); } option= 1; retval= setsockopt(ctlListenFd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)); if (retval == -1) { pferror(errno, "line %d", __LINE__); abort(); } retval= bind(ctlListenFd, (struct sockaddr*) &addr, sizeof(addr)); if (retval == -1) { pferror(errno, "line %d", __LINE__); abort(); } retval= listen(ctlListenFd, 1); if (retval == -1) { pferror(errno, "line %d", __LINE__); abort(); } if (verbose) { printf("Listenning on %s:%u...\n", inet_ntoa(addr.sin_addr), portNum); } buffer= malloc(LFCOLS * 3 * LFROWS); while(true) { size_t done= 0; fd_set rdfds; int max= 0; FD_ZERO(&rdfds); FD_SET(frameFd, &rdfds); FD_SET(ctlListenFd, &rdfds); max= frameFd > ctlListenFd ? frameFd : ctlListenFd; if (ctlFd != 0) { FD_SET(ctlFd, &rdfds); max= ctlFd > max ? ctlFd : max; } retval= select(max + 1, &rdfds, 0, 0, 0); if (retval == -1) { pferror(errno, "line %d", __LINE__); abort(); } if (FD_ISSET(frameFd, &rdfds)) { // read a frame struct sockaddr_in srcAddr; socklen_t addrLen; addrLen= sizeof(srcAddr); retval= recvfrom(frameFd, buffer + done, LFCOLS * 3 * LFROWS - done, 0, (struct sockaddr*) &srcAddr, &addrLen); if (retval == -1) { pferror(errno, "Error reading from network"); abort(); } done+= retval; if (verbose) { printf("Received %d bytes from %s\n", retval, inet_ntoa(srcAddr.sin_addr)); } // write a frame if (done == LFCOLS * 3 * LFROWS) { done= 0; while (done < LFCOLS * 3 * LFROWS) { retval= write(ledFd, buffer + done, LFCOLS * 3 * LFROWS - done); if (retval == -1) { pferror(errno, "Error writing to ledfloor"); abort(); } done+= retval; } if (verbose) { printf("Wrote a frame\n"); } done= 0; } } else if (FD_ISSET(ctlListenFd, &rdfds)) { struct sockaddr_in srcAddr; socklen_t addrLen; if (ctlFd != 0) { retval= close(ctlFd); if (retval == -1) { pferror(errno, "line %d", __LINE__); abort(); } } addrLen= sizeof(srcAddr); ctlFd= accept(ctlListenFd, (struct sockaddr*) &srcAddr, &addrLen); if (retval == -1) { pferror(errno, "line %d", __LINE__); abort(); } if (verbose) { printf("New control connection from %s\n", inet_ntoa(srcAddr.sin_addr)); } } else if (FD_ISSET(ctlFd, &rdfds)) { struct command_t command; retval= read(ctlFd, &command, sizeof(command)); if (retval == -1) { pferror(errno, "line %d", __LINE__); abort(); } else if (retval == 0) { retval= close(ctlFd); if (retval == -1) { pferror(errno, "line %d", __LINE__); abort(); } ctlFd= 0; if (verbose) { printf("Closed control connection\n"); } } else if (retval < sizeof(command)) { fprintf(stderr, "Warning: command missing %lu bytes\n", sizeof(command) - retval); } else { static uint16_t gamma_c[256]; int i; command.latch_ndelay= ntohl(command.latch_ndelay); retval= ioctl(ledFd, LF_IOCSLATCHNDELAY, &command.latch_ndelay); if (retval == -1) { pferror(errno, "line %d", __LINE__); abort(); } command.clk_ndelay= ntohl(command.clk_ndelay); retval= ioctl(ledFd, LF_IOCSCLKNDELAY, &command.clk_ndelay); if (retval == -1) { pferror(errno, "line %d", __LINE__); abort(); } for (i= 0; i < 256; i++) { gamma_c[i]= reverse12(floor(pow((double) i / 255., command.gamma) * 4095)); } retval= ioctl(ledFd, LF_IOCSGAMMATABLE, gamma_c); if (retval == -1) { pferror(errno, "line %d", __LINE__); abort(); } } } else { fprintf(stderr, "Warning: no fd is set after select\n"); } } }
ESR_ReturnCode PFileReadLCHAR(PFile* self, LCHAR* value, size_t len) { size_t i, bufferSize, count, totalRead = 0; ESR_ReturnCode rc = ESR_SUCCESS; /* Skip whitespace before token */ do { count = pfread(value, sizeof(LCHAR), len, self); totalRead += count; if (count < len) { if (pferror(self)) { rc = ESR_READ_ERROR; PLogError(ESR_rc2str(rc)); goto CLEANUP; } else { rc = ESR_INVALID_STATE; PLogError(L("%s: reached end of file before finding token"), ESR_rc2str(rc)); goto CLEANUP; } } /* locate first non-whitespace character */ for (i = 0; i < count && LISSPACE(value[i]); ++i); } while (i == count); bufferSize = count - i; /* Fill remainder of buffer */ if (bufferSize < len) { count = pfread(value + bufferSize, sizeof(LCHAR), len - bufferSize, self); bufferSize += count; totalRead += count; if (count < len - bufferSize && pferror(self)) { rc = ESR_READ_ERROR; PLogError(ESR_rc2str(rc)); goto CLEANUP; } } /* locate first whitespace character */ for (i = 0; i < bufferSize && !LISSPACE(value[i]); ++i); if (i < bufferSize) { /* unread anything after the token */ if (PFileSeek(self, -(int)(bufferSize - i), SEEK_CUR)) { rc = ESR_SEEK_ERROR; PLogError(ESR_rc2str(rc)); } totalRead -= bufferSize - i; value[i] = L('\0'); } return rc; CLEANUP: if (PFileSeek(self, - (int) count, SEEK_CUR)) PLogError(L("ESR_SEEK_ERROR")); return rc; }
ESR_ReturnCode PFileReadInt(PFile* self, int* value) { LCHAR number[MAX_INT_DIGITS+1]; size_t i, bufferSize, count, totalRead = 0; ESR_ReturnCode rc; /* Skip whitespace before token */ do { count = pfread(number, sizeof(LCHAR), MAX_INT_DIGITS, self); totalRead += count; if (count < MAX_INT_DIGITS) { if (pferror(self)) { rc = ESR_READ_ERROR; PLogError(ESR_rc2str(rc)); goto CLEANUP; } else { rc = ESR_INVALID_STATE; PLogError(L("%s: reached end of file before finding token"), ESR_rc2str(rc)); goto CLEANUP; } } /* locate first non-whitespace character */ for (i = 0; i < count && LISSPACE(number[i]); ++i); } while (i == count); bufferSize = count - i; /* Fill remainder of buffer */ if (bufferSize < MAX_INT_DIGITS) { count = pfread(number + bufferSize, sizeof(LCHAR), MAX_INT_DIGITS - bufferSize, self); bufferSize += count; totalRead += count; if (count < MAX_INT_DIGITS - bufferSize && pferror(self)) { rc = ESR_READ_ERROR; PLogError(ESR_rc2str(rc)); goto CLEANUP; } } /* locate first whitespace character */ for (i = 0; i < bufferSize && !LISSPACE(number[i]); ++i); if (i < bufferSize) { /* unread anything after the token */ if (PFileSeek(self, - (int)(bufferSize - i), SEEK_CUR)) { rc = ESR_SEEK_ERROR; PLogError(ESR_rc2str(rc)); } totalRead -= bufferSize - i; number[i] = L('\0'); } if (number[0] != L('-') && !LISDIGIT(number[0])) { rc = ESR_INVALID_STATE; PLogError(L("%s: token was not number (%s)"), ESR_rc2str(rc), number); goto CLEANUP; } CHKLOG(rc, lstrtoi(number, value, 10)); return rc; CLEANUP: if (PFileSeek(self, - (int) count, SEEK_CUR)) PLogError(L("ESR_SEEK_ERROR")); return rc; }