NRTAPI(nrt_ListNode *) nrt_ListNode_construct(nrt_ListNode * prev, nrt_ListNode * next, NRT_DATA * data, nrt_Error * error) { nrt_ListNode *node = (nrt_ListNode *) NRT_MALLOC(sizeof(nrt_ListNode)); if (node == NULL) { /* Init the error with the string value of errno */ nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); /* Return if we have a problem */ return NULL; } /* Attention!! Achtung!! This is a data pointer copy, */ /* not copying an object. That means that YOU the user */ /* of this API MUST allocate it yourself */ /* And, of course, YOU must delete it as well!! */ node->data = data; /* Attach up our nodes */ node->next = next; node->prev = prev; /* Return the new node */ return node; }
MemoryIO::MemoryIO(size_t capacity) throw(nitf::NITFException) : IOInterface(create(NRT_MALLOC(capacity), capacity, true)) { // NOTE: We are telling the C layer to adopt this memory which means it // will use NRT_FREE() to deallocate it. So, we must allocate with // NRT_MALLOC(). setManaged(false); }
char *cloneString(char *data, nrt_Error * error) { int data_len = strlen(data); char *new_data = (char *) NRT_MALLOC(data_len + 1); new_data[data_len] = 0; assert(new_data); strcpy(new_data, data); return new_data; }
NRTAPI(void) nrt_Pair_init(nrt_Pair * pair, const char *key, NRT_DATA * data) { size_t len = strlen(key); pair->key = (char *) NRT_MALLOC(len + 1); /* Help, we have an unchecked malloc here! */ pair->key[len] = 0; strcpy(pair->key, key); pair->data = data; }
NRTPROT(nrt_IntStack *) nrt_IntStack_construct(nrt_Error * error) { nrt_IntStack *stk = (nrt_IntStack *) NRT_MALLOC(sizeof(nrt_IntStack)); if (!stk) { nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); return NULL; } stk->sp = -1; return stk; }
NRTAPI(nrt_DLL *) nrt_DLL_construct(nrt_Error * error) { nrt_DLL *dll = (nrt_DLL *) NRT_MALLOC(sizeof(nrt_DLL)); if (!dll) { nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); } dll->libname = NULL; dll->lib = NULL; return dll; }
NRTAPI(nrt_DateTime *) nrt_DateTime_fromString(const char *string, const char *format, nrt_Error * error) { struct tm t; nrt_DateTime *dateTime = NULL; double millis = 0.0; /* NOTE: _NRT_strptime() does not use the tm_isdst flag at all. */ t.tm_isdst = -1; if (!_NRT_strptime(string, format, &t, &millis)) { nrt_Error_initf(error, NRT_CTXT, NRT_ERR_INVALID_OBJECT, "Unknown error caused by the call to strptime with string [%s] and format string [%s]", string, format); return NULL; } /* Create a DateTime object */ dateTime = (nrt_DateTime *) NRT_MALLOC(sizeof(nrt_DateTime)); if (!dateTime) { nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); return NULL; } /* Initialize it from the tm struct * TODO: Update _NRT_strptime() to just use a DateTime directly */ dateTime->year = t.tm_year + 1900; /* 0-based so add 1 */ dateTime->month = t.tm_mon + 1; dateTime->dayOfMonth = t.tm_mday; dateTime->dayOfWeek = t.tm_wday + 1; dateTime->dayOfYear = t.tm_yday + 1; dateTime->hour = t.tm_hour; dateTime->minute = t.tm_min; dateTime->second = t.tm_sec + millis / 1000.0; /* Compute the # of milliseconds */ if (!nrt_DateTime_updateMillis(dateTime, error)) { NRT_FREE(dateTime); return NULL; } return dateTime; }
NRTAPI(nrt_DateTime *) nrt_DateTime_fromMillis(double millis, nrt_Error * error) { nrt_DateTime *dt = NULL; dt = (nrt_DateTime *) NRT_MALLOC(sizeof(nrt_DateTime)); if (!dt) { nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); return NULL; } nrt_DateTime_setTimeInMillis(dt, millis, error); return dt; }
NRTAPI(nrt_List *) nrt_List_construct(nrt_Error * error) { /* New allocate a list */ nrt_List *l; l = (nrt_List *) NRT_MALLOC(sizeof(nrt_List)); if (!l) { /* Initialize the error and return NULL */ nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); return NULL; } /* Null-initialize the link pointers */ l->first = l->last = NULL; return l; }
NRTAPI(NRT_BOOL) nrt_DLL_load(nrt_DLL * dll, const char *libname, nrt_Error * error) { dll->libname = (char *) NRT_MALLOC(strlen(libname) + 1); if (!dll->libname) { nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); return NRT_FAILURE; } strcpy(dll->libname, libname); dll->lib = dlopen(libname, RTLD_LAZY); if (!dll->lib) { nrt_Error_init(error, dlerror(), NRT_CTXT, NRT_ERR_LOADING_DLL); NRT_FREE(dll->libname); dll->libname = NULL; return NRT_FAILURE; } return NRT_SUCCESS; }
NRTAPI(nrt_List *) nrt_Utils_splitString(char *str, unsigned int max, nrt_Error * error) { unsigned int count = 0; nrt_List *parts; char *op, *cur, *end; size_t strLen; parts = nrt_List_construct(error); if (!parts) return NULL; strLen = strlen(str); end = str + strLen; op = str; if (max == 1) { char *val = NRT_MALLOC(strLen + 1); if (!val) { nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); return NULL; } memset(val, 0, strLen + 1); memcpy(val, str, strLen); nrt_List_pushBack(parts, val, error); } else { /* strtok is not thread safe */ while (op < end) { char *val = NULL; int sz; /* skip past white space */ while (isspace(*op) && op < end) ++op; cur = op; while (!isspace(*op) && op < end) ++op; if (cur == op) break; sz = op - cur; val = NRT_MALLOC(sz + 1); if (!val) { nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); return NULL; } memset(val, 0, sz + 1); memcpy(val, cur, sz); nrt_List_pushBack(parts, val, error); count++; /* check the count limit */ if (max != 0 && count == (max - 1) && op < end) { /* push on the rest of the string - skip spaces first */ while (isspace(*op) && op < end) ++op; if (op < end) { sz = end - op; val = NRT_MALLOC(sz + 1); if (!val) { nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); return NULL; } memset(val, 0, sz + 1); memcpy(val, op, sz); nrt_List_pushBack(parts, val, error); } break; } } } return parts; }
NRTAPI(NRT_BOOL) nrt_DateTime_formatMillis(double millis, const char *format, char *outBuf, size_t maxSize, nrt_Error * error) { time_t timeInSeconds; double fractSeconds; struct tm t; char *newFmtString = NULL; const char *endString = NULL; size_t begStringLen = 0; size_t formatLength; size_t startIndex = 0; size_t i, j; NRT_BOOL found = 0; timeInSeconds = (time_t) (millis / 1000); t = *gmtime(&timeInSeconds); fractSeconds = (millis / 1000.0) - timeInSeconds; /* Search for "%...S" string */ formatLength = strlen(format); for (i = 0; i < formatLength && !found; ++i) { if (format[i] == '%') { startIndex = i; for (j = startIndex + 1; j < formatLength; ++j) { if (format[j] == '%') { break; } if (format[j] == 'S') { found = 1; formatLength = j - startIndex + 1; begStringLen = startIndex; endString = format + j + 1; } } } } /* If we found a "%...S" string, parse it */ /* to find out how many decimal places to use */ if (found) { int decimalPlaces = 0; /* Figure out how many decimal places we need... */ for (i = startIndex + 1; i < startIndex + (formatLength - 1); ++i) { if (format[i] == '.') { /* The digits that follow should be */ /* the number of decimal places */ sscanf(format + i + 1, "%d", &decimalPlaces); } } if (decimalPlaces > 0) { char buf[256]; size_t newFmtLen = 0; size_t bufIdx = 0; size_t endStringLen = endString ? strlen(endString) : 0; newFmtLen = begStringLen + 1; newFmtString = (char *) NRT_MALLOC(newFmtLen); if (!newFmtString) { nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); goto CATCH_ERROR; } memset(newFmtString, 0, newFmtLen); if (begStringLen > 0) { /* do the first part of the format */ strncpy(newFmtString, format, begStringLen); if (strftime(outBuf, maxSize, newFmtString, &t) == 0) { nrt_Error_initf(error, NRT_CTXT, NRT_ERR_INVALID_OBJECT, "Unknown error caused by the call to strftime with format string: [%s]", format); goto CATCH_ERROR; } bufIdx = strlen(outBuf); } /* do the seconds - separately */ memset(buf, 0, 256); if (strftime(buf, 256, "%S", &t) == 0) { nrt_Error_initf(error, NRT_CTXT, NRT_ERR_INVALID_OBJECT, "Unknown error caused by the call to strftime with format string: [%s]", format); goto CATCH_ERROR; } if (strlen(buf) + bufIdx + 1 > maxSize) { nrt_Error_initf(error, NRT_CTXT, NRT_ERR_INVALID_OBJECT, "Format string will cause buffer to overflow: [%s]", format); goto CATCH_ERROR; } /* tack it on the end */ strcpy(outBuf + bufIdx, buf); bufIdx = strlen(outBuf); memset(buf, 0, 256); NRT_SNPRINTF(buf, 256, "%.*f", decimalPlaces, fractSeconds); if (strlen(buf) + bufIdx + 1 > maxSize) { nrt_Error_initf(error, NRT_CTXT, NRT_ERR_INVALID_OBJECT, "Format string will cause buffer to overflow: [%s]", format); goto CATCH_ERROR; } /* tack on the fractional seconds - spare the leading 0 */ strcpy(outBuf + bufIdx, buf + 1); bufIdx = strlen(outBuf); if (endStringLen > 0) { /* tack on the end part */ memset(buf, 0, 256); if (strftime(buf, 256, endString, &t) == 0) { nrt_Error_initf(error, NRT_CTXT, NRT_ERR_INVALID_OBJECT, "Unknown error caused by the call to strftime with format string: [%s]", format); goto CATCH_ERROR; } if (strlen(buf) + bufIdx + 1 > maxSize) { nrt_Error_initf(error, NRT_CTXT, NRT_ERR_INVALID_OBJECT, "Format string will cause buffer to overflow: [%s]", format); goto CATCH_ERROR; } strcpy(outBuf + bufIdx, buf); } } } if (newFmtString == NULL) { if (strftime (outBuf, maxSize, newFmtString != NULL ? newFmtString : format, &t) == 0) { nrt_Error_initf(error, NRT_CTXT, NRT_ERR_INVALID_OBJECT, "Unknown error caused by the call to strftime with format string: [%s]", newFmtString != NULL ? newFmtString : format); goto CATCH_ERROR; } } else NRT_FREE(newFmtString); return NRT_SUCCESS; CATCH_ERROR: if (newFmtString) NRT_FREE(newFmtString); return NRT_FAILURE; }