/*****************************************************************************\ * Copyright (c) 2004 Mark Aylett * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * * "Software"), to deal in the Software without restriction, including * * without limitation the rights to use, copy, modify, merge, publish, * * distribute, sublicense, and/or sell copies of the Software, and to permit * * persons to whom the Software is furnished to do so, subject to the * * following conditions: * * * * The above copyright notice and this permission notice shall be included * * in all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN * * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * * USE OR OTHER DEALINGS IN THE SOFTWARE. * \*****************************************************************************/ static const char rcsid[] = "$Id: mar_util_c.c,v 1.6 2004/12/14 13:09:23 marayl Exp $"; #include "mar_util_c.h" #include "mar_debug_c.h" #include #include static const char MAR_NL = '\n'; MAR_EXTERN int mar_atopair_(mar_pair_t* pair, const char* src) { /** * Split pair in the form of "key=data". */ const char* data = strchr(src, '='); size_t size; if (NULL == data) { MAR_DEBUG_MSG("Failed to split pair:" " No separator found"); errno = EINVAL; return -1; } size = data - src; if (sizeof(pair->key_) <= size) { MAR_DEBUG_MSG("Failed to split pair:" " Maximum key size exceeded"); errno = EINVAL; return -1; } memcpy(pair->key_, src, size); pair->key_[size] = '\0'; pair->data_ = ++data; pair->size_ = strlen(data); return 0; } MAR_EXTERN int mar_confirm_(const char* prompt) { char buf[3]; printf("%s? (y or [n]): ", prompt); switch (mar_readline_(buf, sizeof(buf), stdin)) { case 1: break; default: return 0; } switch (buf[0]) { case 'y': case 'Y': return 1; } return 0; } MAR_EXTERN int mar_insertstream_(mar_t mar, FILE* stream) { char buf[1024]; while (!feof(stream)) { size_t size = fread(buf, 1, sizeof(buf), stdin); if (size) mar_write(mar, buf, size); } return 0; } MAR_EXTERN ssize_t mar_readline_(char* buf, size_t size, FILE* stream) { char* p = fgets(buf, size, stream); if (!p) { if (feof(stream)) { MAR_DEBUG_MSG("Failed to read line:" " End of file encountered\n"); return -2; } MAR_DEBUG_MSG("Failed to read line:" " Error reading stream\n"); return -1; } if (!(p = strchr(buf, '\n'))) { MAR_DEBUG_MSG("Failed to read line:" " No newline found\n"); errno = EINVAL; return -1; } *p = '\0'; return p - buf; } MAR_EXTERN int mar_streamset_(mar_t mar, FILE* stream) { char buf[1024]; mar_pair_t pair; for (;;) { switch (mar_readline_(buf, sizeof(buf), stream)) { case -2: goto eof; case -1: return -1; } if (-1 == mar_atopair_(&pair, buf)) return -1; if (-1 == mar_setpair(mar, &pair, NULL)) return -1; } eof: return 0; } MAR_EXTERN int mar_writedata_(FILE* stream, const void* data, size_t size) { if (size != fwrite(data, 1, size, stream)) return -1; if (1 != fwrite(&MAR_NL, 1, 1, stream)) return -1; return 0; }