本文整理汇总了C++中elektraMalloc函数的典型用法代码示例。如果您正苦于以下问题:C++ elektraMalloc函数的具体用法?C++ elektraMalloc怎么用?C++ elektraMalloc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了elektraMalloc函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: elektraResolveSystemBuildin
static int elektraResolveSystemBuildin (ElektraResolved * handle, ElektraResolveTempfile tmpDir, Key * warningsKey)
{
size_t filenameSize = sizeof (KDB_DB_SYSTEM) + elektraStrLen (handle->relPath) + sizeof ("/");
char * resolved = NULL;
if (KDB_DB_SYSTEM[0] == '~')
{
char * resolvedPath = elektraMalloc (filenameSize);
strcpy (resolvedPath, KDB_DB_SYSTEM);
strcat (resolvedPath, "/");
strcat (resolvedPath, handle->relPath);
char * oldPath = handle->relPath;
handle->relPath = resolvedPath;
elektraResolveSystemPasswd (handle, warningsKey);
elektraFree (resolvedPath);
handle->relPath = oldPath;
}
else
{
resolved = elektraMalloc (filenameSize);
strcpy (resolved, KDB_DB_SYSTEM);
strcat (resolved, "/");
strcat (resolved, handle->relPath);
handle->fullPath = resolved;
}
elektraResolveFinishByFilename (handle, tmpDir);
return 1;
}
示例2: elektraResolveSystem
static void elektraResolveSystem (resolverHandle * p, Key * errorKey)
{
char * system = getenv ("ALLUSERSPROFILE");
if (!system)
{
system = "";
ELEKTRA_ADD_WARNING (90, errorKey, "could not get ALLUSERSPROFILE, using /");
}
else
{
escapePath (system);
}
if (p->path[0] == '/')
{
/* Use absolute path */
size_t filenameSize = strlen (system) + strlen (p->path) + 1;
p->filename = elektraMalloc (filenameSize);
strcpy (p->filename, system);
strcat (p->filename, p->path);
return;
}
size_t filenameSize = sizeof (KDB_DB_SYSTEM) + strlen (system) + strlen (p->path) + sizeof ("/") + 1;
p->filename = elektraMalloc (filenameSize);
strcpy (p->filename, system);
strcat (p->filename, KDB_DB_SYSTEM);
strcat (p->filename, "/");
strcat (p->filename, p->path);
return;
}
示例3: loadFile
static char * loadFile (FILE * fh)
{
/* open the file */
char * content = 0;
if (fseek (fh, 0, SEEK_END) != 0) return 0;
long fileSize = ftell (fh);
rewind (fh);
if (fileSize > 0)
{
content = elektraMalloc (fileSize * sizeof (char) + 1);
if (content == 0) return 0;
int readBytes = fread (content, sizeof (char), fileSize, fh);
if (feof (fh) || ferror (fh) || readBytes != fileSize) return 0;
/* null terminate the string, as fread doesn't do it */
content[fileSize] = 0;
}
else if (fileSize == 0)
{
content = elektraMalloc (1);
if (content == 0) return 0;
*content = (char)0;
}
return content;
}
示例4: test_keyGetBinary
static void test_keyGetBinary (const size_t storagePlugin, const char * tmpFile)
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
open_storage_plugin (storagePlugin);
Plugin * plugin = plugins[storagePlugin];
KeySet * ks = metaTestKeySet ();
const char * name = "user/tests/storage/specialkey";
size_t realValueSize = 42;
void * value = elektraMalloc (realValueSize);
memset (value, 42, realValueSize);
Key * key = keyNew (name, KEY_END);
keySetBinary (key, value, realValueSize);
ksAppendKey (ks, key);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
Key * found = ksLookupByName (ks, name, 0);
succeed_if (found, "did not find key");
ssize_t apiValueSize = keyGetValueSize (found);
char * apiValue = elektraMalloc (apiValueSize);
succeed_if (keyGetBinary (found, apiValue, apiValueSize) == (ssize_t) realValueSize, "Key binary has wrong size");
succeed_if (elektraStrNCmp (value, apiValue, realValueSize) == 0, "Key binary value is wrong");
elektraFree (apiValue);
elektraFree (value);
keyDel (parentKey);
ksDel (ks);
closeStoragePlugin (storagePlugin);
}
示例5: setlocale
/**
* Converts string to (@p direction = @c UTF8_TO) and from
* (@p direction = @c UTF8_FROM) UTF-8.
*
* Since Elektra provides portability for key names and string values between
* different codesets, you should use this helper in your backend to convert
* to and from universal UTF-8 strings, when storing key names, values and
* comments.
*
* Broken locales in applications can cause problems too. Make sure to load
* the environment locales in your application using
* @code
setlocale (LC_ALL, "");
* @endcode
*
* Otherwise kdbbUTF8Engine and this plugin will quit
* with error when non-ascii characters appear.
*
* Binary values are not effected.
*
* If iconv() or nl_langinfo() is not available on your system, or if iconv()
* this plugin can't be used.
*
* @param direction must be @c UTF8_TO (convert from current non-UTF-8 to
* UTF-8) or @c UTF8_FROM (convert from UTF-8 to current non-UTF-8)
* @param string before the call: the string to be converted; after the call:
* reallocated to carry the converted string
* @param inputOutputByteSize before the call: the size of the string including
* leading NULL; after the call: the size of the converted string including
* leading NULL
* @retval 0 on success
* @retval -1 on failure
* @ingroup backendhelper
*
*/
int kdbbUTF8Engine (Plugin * handle, int direction, char ** string, size_t * inputOutputByteSize)
{
/* Current solution is not very complete.
* Iconv might well be available when a usable nl_langinfo is not.
* In this case we it should be possible to determine charset through other means
* See http://www.cl.cam.ac.uk/~mgk25/unicode.html#activate for more info on a possible solution */
char * converted = 0;
char *readCursor, *writeCursor;
size_t bufferSize;
iconv_t converter;
if (!*inputOutputByteSize) return 0;
if (!kdbbNeedsUTF8Conversion (handle)) return 0;
if (direction == UTF8_TO)
converter = iconv_open (getTo (handle), getFrom (handle));
else
converter = iconv_open (getFrom (handle), getTo (handle));
if (converter == (iconv_t) (-1)) return -1;
/* work with worst case, when all chars are wide */
bufferSize = *inputOutputByteSize * 4;
converted = elektraMalloc (bufferSize);
if (!converted) return -1;
readCursor = *string;
writeCursor = converted;
/* On some systems and with libiconv, arg1 is const char **.
* ICONV_CONST is defined by configure if the system needs this */
if (iconv (converter, &readCursor, inputOutputByteSize, &writeCursor, &bufferSize) == (size_t) (-1))
{
elektraFree (converted);
iconv_close (converter);
return -1;
}
/* calculate the UTF-8 string byte size, that will be returned */
*inputOutputByteSize = writeCursor - converted;
/* store the current kdbbDecoded string for future free */
readCursor = *string;
/* allocate an optimal size area to store the converted string */
*string = elektraMalloc (*inputOutputByteSize);
/* copy all that matters for returning */
memcpy (*string, converted, *inputOutputByteSize);
/* release memory used by passed string */
elektraFree (readCursor);
/* release buffer memory */
elektraFree (converted);
/* release the conversor engine */
iconv_close (converter);
return 0;
}
示例6: elektraIconvSet
int elektraIconvSet (Plugin * handle, KeySet * returned, Key * parentKey)
{
Key * cur;
if (!kdbbNeedsUTF8Conversion (handle)) return 0;
ksRewind (returned);
while ((cur = ksNext (returned)) != 0)
{
if (keyIsString (cur))
{
/* String or similar type of value */
size_t convertedDataSize = keyGetValueSize (cur);
char * convertedData = elektraMalloc (convertedDataSize);
memcpy (convertedData, keyString (cur), keyGetValueSize (cur));
if (kdbbUTF8Engine (handle, UTF8_TO, &convertedData, &convertedDataSize))
{
ELEKTRA_SET_ERRORF (46, parentKey,
"Could not convert string %s, got result %s,"
" encoding settings are from %s to %s (but swapped for write)",
keyString (cur), convertedData, getFrom (handle), getTo (handle));
elektraFree (convertedData);
return -1;
}
keySetString (cur, convertedData);
elektraFree (convertedData);
}
const Key * meta = keyGetMeta (cur, "comment");
if (meta)
{
/* String or similar type of value */
size_t convertedDataSize = keyGetValueSize (meta);
char * convertedData = elektraMalloc (convertedDataSize);
memcpy (convertedData, keyString (meta), keyGetValueSize (meta));
if (kdbbUTF8Engine (handle, UTF8_TO, &convertedData, &convertedDataSize))
{
ELEKTRA_SET_ERRORF (46, parentKey,
"Could not convert string %s, got result %s,"
" encodings settings are from %s to %s (but swapped for write)",
keyString (meta), convertedData, getFrom (handle), getTo (handle));
elektraFree (convertedData);
return -1;
}
keySetMeta (cur, "comment", convertedData);
elektraFree (convertedData);
}
}
return 1; /* success */
}
示例7: elektraResolveFinishByFilename
/**
* @brief Given filename, calcualtes dirname+tempfile
*
* @param p resolverHandle with filename set
*/
static void elektraResolveFinishByFilename(resolverHandle *p)
{
size_t filenameSize = strlen(p->filename);
p->dirname = elektraMalloc (filenameSize);
char * dup = strdup(p->filename);
//dirname might change the buffer, so better work on a copy
strcpy (p->dirname, dirname(dup));
elektraFree (dup);
p->tempfile = elektraMalloc (filenameSize + POSTFIX_SIZE);
elektraGenTempFilename(p->tempfile, p->filename);
}
示例8: prefToKey
static Key * prefToKey (Key * parentKey, PrefType type, const char * pref)
{
Key * key = keyNew (keyName (parentKey), KEY_END);
keyAddBaseName (key, prefix[type]);
char * localString = elektraStrDup (pref);
char * cPtr = strstr (localString, ",");
*cPtr = '\0';
char * sPtr = localString;
++sPtr;
*sPtr++ = '\0';
char * ePtr = cPtr - 1;
elektraRstrip (sPtr, &ePtr);
size_t keyLen = ePtr - sPtr;
char * prefKey = elektraMalloc (keyLen + 1);
snprintf (prefKey, keyLen + 1, "%s", sPtr);
char * tPtr = strtok (prefKey, ".");
if (tPtr) keyAddBaseName (key, tPtr);
while ((tPtr = strtok (NULL, ".")) != NULL)
{
keyAddBaseName (key, tPtr);
}
elektraFree (prefKey);
sPtr = cPtr + 1;
sPtr = elektraLskip (sPtr);
ePtr = strrchr (sPtr, ')');
*ePtr-- = '\0';
elektraRstrip (sPtr, &ePtr);
size_t argLen = ePtr - sPtr + 1;
char * prefArg = elektraMalloc (argLen + 1);
snprintf (prefArg, argLen + 1, "%s", sPtr);
if (!strcmp (prefArg, "true") || !(strcmp (prefArg, "false")))
{
keySetMeta (key, "type", "boolean");
keySetString (key, prefArg);
}
else if (prefArg[0] == '"' && prefArg[strlen (prefArg) - 1] == '"')
{
// TODO: else if list
keySetMeta (key, "type", "string");
*prefArg = '\0';
*(prefArg + (strlen (prefArg + 1))) = '\0';
keySetString (key, (prefArg + 1));
}
else
{
keySetMeta (key, "type", "integer");
keySetString (key, prefArg);
}
elektraFree (prefArg);
elektraFree (localString);
return key;
}
示例9: keyGenerate
/**
* Generate a C-Style key and stream it.
*
* This keyset can be used to include as c-code for
* applikations using elektra.
*
* @param key the key object to work with
* @param stream the file pointer where to send the stream
* @param options KDB_O_SHOWINDICES, KDB_O_IGNORE_COMMENT, KDB_O_SHOWINFO
* @retval 1 on success
* @ingroup stream
*/
int keyGenerate(const Key * key, FILE *stream, option_t options)
{
size_t s;
char * str;
size_t c;
char * com;
size_t n;
char * nam;
n = keyGetNameSize (key);
if (n>1)
{
nam = (char*) elektraMalloc (n);
if (nam == NULL) return -1;
keyGetName (key, nam, n);
fprintf(stream,"\tkeyNew (\"%s\"", nam);
elektraFree (nam);
}
s = keyGetValueSize (key);
if (s>1)
{
str = (char*) elektraMalloc (s);
if (str == NULL) return -1;
if (keyIsBinary(key)) keyGetBinary(key, str, s);
else keyGetString (key, str, s);
fprintf(stream,", KEY_VALUE, \"%s\"", str);
elektraFree (str);
}
c = keyGetCommentSize (key);
if (c>1)
{
com = (char*) elektraMalloc (c);
if (com == NULL) return -1;
keyGetComment (key, com, c);
fprintf(stream,", KEY_COMMENT, \"%s\"", com);
elektraFree (com);
}
if (! (keyGetMode(key) == 0664 || (keyGetMode(key) == 0775)))
{
fprintf(stream,", KEY_MODE, 0%3o", keyGetMode(key));
}
fprintf(stream,", KEY_END)");
if (options == 0) return 1; /* dummy to make icc happy */
return 1;
}
示例10: elektraResolveUser
static void elektraResolveUser (resolverHandle * p, Key * warningsKey)
{
p->filename = elektraMalloc (KDB_MAX_PATH_LENGTH);
#if defined(_WIN32)
CHAR home[MAX_PATH];
if (SUCCEEDED (SHGetFolderPath (NULL, CSIDL_PROFILE, NULL, 0, home)))
{
escapePath (home);
}
else
{
strcpy (home, "");
ELEKTRA_ADD_WARNING (90, warningsKey, "could not get home (CSIDL_PROFILE), using /");
}
#else
char * home = (char *)getenv ("HOME");
if (!home)
{
home = "";
ELEKTRA_ADD_WARNING (90, warningsKey, "could not get home, using /");
}
#endif
strcpy (p->filename, home);
strcat (p->filename, "/");
strncat (p->filename, p->path, KDB_MAX_PATH_LENGTH);
}
示例11: handleMissingConflict
static int handleMissingConflict (Key * parentKey, Key * key, Key * conflictMeta, OnConflict onConflict)
{
int ret = 0;
const char * problemKeys = elektraMetaArrayToString (key, keyName (conflictMeta), ", ");
switch (onConflict)
{
case ERROR:
ELEKTRA_SET_ERRORF (142, parentKey, "%s has missing subkeys: %s\n", keyName (key), problemKeys);
ret = -1;
break;
case WARNING:
ELEKTRA_ADD_WARNINGF (143, parentKey, "%s has missing subkeys: %s\n", keyName (key), problemKeys);
break;
case INFO:
{
const char * infoString = "has missing subkeys:";
const size_t len = elektraStrLen (infoString) + elektraStrLen (problemKeys) + elektraStrLen (keyName (key));
char * buffer = elektraMalloc (len);
snprintf (buffer, len, "%s %s %s", keyName (key), infoString, problemKeys);
elektraMetaArrayAdd (key, "logs/spec/info", buffer);
elektraFree (buffer);
}
break;
case IGNORE:
break;
}
if (problemKeys)
{
elektraFree ((void *) problemKeys);
}
return ret;
}
示例12: handleInvalidConflict
static int handleInvalidConflict (Key * parentKey, Key * key, OnConflict onConflict)
{
int ret = 0;
switch (onConflict)
{
case ERROR:
ELEKTRA_SET_ERRORF (142, parentKey, "Invalid key %s\n", keyName (key));
ret = -1;
break;
case WARNING:
ELEKTRA_ADD_WARNINGF (143, parentKey, "Invalid key %s\n", keyName (key));
break;
case INFO:
{
const char * infoString = "Invalid key ";
const size_t len = elektraStrLen (infoString) + elektraStrLen (keyName (key)) - 1;
char * buffer = elektraMalloc (len);
snprintf (buffer, len, "Invalid key %s\n", keyName (key));
elektraMetaArrayAdd (key, "logs/spec/info", buffer);
elektraFree (buffer);
}
break;
case IGNORE:
break;
}
return ret;
}
示例13: test_elektraMalloc
static void test_elektraMalloc (void)
{
char * buffer = 0;
buffer = elektraMalloc (50);
exit_if_fail (buffer, "buffer must not be 0 after allocation");
elektraRealloc ((void **)&buffer, 100);
exit_if_fail (buffer, "buffer must not be 0 after reallocation");
elektraRealloc ((void **)&buffer, 20);
exit_if_fail (buffer, "buffer must not be 0 after reallocation");
elektraFree (buffer);
buffer = elektraCalloc (50);
exit_if_fail (buffer, "buffer must not be 0 after allocation");
for (int i = 0; i < 50; ++i)
{
succeed_if (buffer[i] == 0, "elektraCalloc did not initialize buffer with zeros");
}
elektraRealloc ((void **)&buffer, 100);
exit_if_fail (buffer, "buffer must not be 0 after reallocation");
elektraRealloc ((void **)&buffer, 20);
exit_if_fail (buffer, "buffer must not be 0 after reallocation");
char * dup = elektraStrNDup (buffer, 20);
exit_if_fail (dup, "could not duplicate buffer");
elektraFree (buffer);
buffer = 0;
for (int i = 0; i < 20; ++i)
{
succeed_if (dup[i] == 0, "elektraStrNDup did not correctly copy zero-buffer");
}
elektraFree (dup);
}
示例14: ELEKTRA_PLUGIN_FUNCTION
/**
* Check if supplied filename is ok.
*
* This symbol is exported and used during mounting.
*
* @retval 1 on success (Relative path)
* @retval 0 on success (Absolute path)
* @retval -1 on a non-valid file
*/
int ELEKTRA_PLUGIN_FUNCTION(resolver,checkFile)(const char* filename)
{
if (!filename) return -1;
if (filename[0] == '0') return -1;
size_t size = strlen(filename);
char *buffer = elektraMalloc(size + sizeof ("system/"));
strcpy(buffer, "system/");
strcat(buffer, filename);
/* Because of the outbreak bugs these tests are not enough */
Key *check = keyNew(buffer, KEY_END);
if (!strcmp(keyName(check), "")) goto error;
if (!strcmp(keyName(check), "system")) goto error;
keyDel(check);
elektraFree (buffer);
/* Be strict, don't allow any .., even if it would be ok sometimes */
if (strstr (filename, "..") != 0) return -1;
if (filename[0] == '/') return 0;
return 1;
error:
keyDel (check);
elektraFree (buffer);
return -1;
}
示例15: elektraSpecloadOpen
int elektraSpecloadOpen (Plugin * handle, Key * errorKey)
{
Specload * specload = elektraMalloc (sizeof (Specload));
KeySet * conf = elektraPluginGetConfig (handle);
if (ksLookupByName (conf, "system/module", 0) != NULL || ksLookupByName (conf, "system/sendspec", 0) != NULL)
{
elektraFree (specload);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
if (!getAppAndArgs (conf, &specload->app, &specload->argv, errorKey))
{
elektraFree (specload);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
specload->quickDumpConfig = ksNew (0, KS_END);
specload->quickDump = elektraInvokeOpen ("quickdump", specload->quickDumpConfig, errorKey);
if (!specload->quickDump)
{
elektraFree (specload);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
elektraPluginSetData (handle, specload);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}