/* * look for the node with the given name. if not exist, * create a new one and append to the node list. */ static struct makenode *add_target(const char *name) { struct makenode *__restrict node; struct makenode *prev, *t; node = lookup_target(name); if (node) return node; if (posix_memalign((void*)&node, sizeof(void*), alignof(struct makenode)+strsize(name)) < 0) { fprintf(stderr, "Can't malloc: %s\n", strerror(errno)); exit(1); } memset(node, 0, alignof(struct makenode)+strsize(name)); node->name = ((char*)node)+alignof(struct makenode); strcpy(node->name, name); /* append to the list in alphabetical order */ prev = NULL; for (t = tree_list; t; prev = t, t = t->next) if (strcmp(node->name, t->name) < 0) break; if (prev) prev->next = node; else tree_list = node; node->next = t; tree_entries++; return node; }
/* inserts a line break at the cursor */ void insertlb(view_t *view) { buffer_t *b=view->buffer; if(b->readonly) error_throw(ERR_READONLY); /* enlarge the line count */ b->line_count++; /* allocate more space */ b->lines=realloc(b->lines,sizeof(char *)*b->line_count); int nln=view->cursor_line; /* the number of the new line -1 */ /* copy lines down */ int l; for(l=b->line_count-2;l>=nln;l--) b->lines[l+1]=b->lines[l]; /* create the new line */ b->lines[nln]=malloc(view->cursor_x+2); b->lines[nln][0]='\0'; /* copy the first part into the new line*/ strncat(b->lines[nln],b->lines[nln+1],view->cursor_x); /* copy the second part out of the next line */ char *sp=malloc(strsize(b->lines[nln+1]+view->cursor_x)); sp[0]='\0'; strcat(sp,b->lines[nln+1]+view->cursor_x); free(b->lines[nln+1]); /* free old stuff */ b->lines[nln+1]=sp; /* use new stuff */ view->pref_x=0; /* we're at the beginning of the line */ /* move the cursor down */ cursor_down(view); /* we've been modified */ b->modified=1; }
int cconf_write(int fd, struct cconf *c) { int i; SHA_CTX ctx; struct cconf_header hdr; hdr.signature = htonl(CCONF_SIGNATURE); hdr.version = htonl(1); SHA1_Init(&ctx); SHA1_Update(&ctx, &hdr, offsetof(struct cconf_header, crc)); /* leave room for the header to be written later as CRC will be calculated as we write the rest of the data */ lseek(fd, sizeof(hdr), SEEK_SET); /* put number of targets */ sha1_write_int(&ctx, fd, c->nr); for(i = 0; i < c->nr; i++) { int j; struct target *target = c->target + i; if (!target->src) return -1; sha1_write(&ctx, fd, target->src, strsize(target->src)); /* write number of filters */ sha1_write_int(&ctx, fd, target->nr); for(j=0; j < target->nr; j++) { struct filter *f = &target->filter[j]; sha1_write(&ctx, fd, f->pattern, strsize(f->pattern)); sha1_write(&ctx, fd, f->dest, strsize(f->dest)); } } SHA1_Final(hdr.crc, &ctx); /* write header */ lseek(fd, 0, SEEK_SET); sha1_write(&ctx, fd, &hdr, sizeof(hdr)); return 0; }
//Convert double to string. char* doubleToString(long double value){ char* fhPointer; char* shPointer; int firstHalfSize; int secondHalfSize = 0; long firstHalf = value; long double secondHalf = value - firstHalf; short addedZeros; int i = 0; if(secondHalf < 0) secondHalf *= -1; while((secondHalfSize < 6)){ secondHalf *= 10; secondHalfSize++; if((int)secondHalf == 0) addedZeros++; if(secondHalfSize == 6){ if((int)(secondHalf * 10) % 10 >= 5) secondHalf++; } } intToString(firstHalf); firstHalfSize = strsize(tempString); fhPointer = tempString; tempString = NULL; intToString(secondHalf); shPointer = tempString; tempString = NULL; tempString = (char*)malloc(firstHalfSize + addedZeros + secondHalfSize + 2); for(i = i; i < firstHalfSize; i++){ tempString[i] = fhPointer[i]; } if(secondHalfSize) tempString[i++] = '.'; for(i = i; i < firstHalfSize + addedZeros + 1; i++){ tempString[i] = '0'; } for(i = i; i < firstHalfSize + addedZeros + secondHalfSize + 1; i++){ tempString[i] = shPointer[i - firstHalfSize - addedZeros - 1]; } free(fhPointer); free(shPointer); return tempString; }
static void _test_stream_byte_complex_out(struct peak_streams *pool, struct peak_stream **ref, const unsigned int pos) { const unsigned int len = strsize(test_stream_strings[pos]); assert(!strcmp((*ref)->buf, test_stream_strings[pos])); peak_stream_release(pool, ref, len); }
static size_t parse_target(void *buf, struct target *target) { size_t offset; target->src = (char *) buf; offset = strsize(buf); return offset; }
static void _test_stream_byte_complex_in(struct peak_streams *pool, struct peak_stream **ref, const unsigned int pos) { const unsigned int len = strsize(test_stream_strings[pos]); assert(peak_stream_claim(pool, ref, len)); memcpy((uint8_t *)(*ref)->buf + (*ref)->len - len, test_stream_strings[pos], len); }
static size_t parse_filter(void *buf, struct target *target) { size_t offset = read_entry_nr(buf, &target->nr) - buf; if (target->nr) { int i; target->filter = malloc(sizeof(*target->filter) * target->nr); for(i=0; i < target->nr; i++) { struct filter *filter = &target->filter[i]; filter->pattern = (char *) buf + offset; offset += strsize(buf + offset); filter->dest = (char *) buf + offset; offset += strsize(buf + offset); } } return offset; }
efi_char8_t* SafeUnicodeStrToAsciiStr(const efi_char16_t* source, efi_char8_t* destination) { efi_char8_t* ret; ASSERT(destination != NULL); // // ASSERT if Source is long than PcdMaximumUnicodeStringLength. // Length tests are performed inside StrLen(). // ASSERT(strsize16(source)!=0); // // Source and Destination should not overlap // ASSERT((efi_uintn_t)((efi_char16_t *)destination-source) > strlen16(source)); ASSERT((efi_uintn_t)((efi_char8_t *)source-destination) > strlen16(source)); ret = destination; while(*source != '\0') { // // If any non-ascii characters in Source then replace it with '?'. // if(*source < 0x80) *destination = (efi_char8_t)*source; else { *destination = '?'; // Surrogate pair check. if((*source >= 0xD800) && (*source <= 0xDFFF)) source++; } destination++; source++; } *destination = '\0'; // // ASSERT Original Destination is less long than PcdMaximumAsciiStringLength. // Length tests are performed inside AsciiStrLen(). // ASSERT(strsize(ret)!=0); return ret; }
static int FontFaceParser_ParseValue(LCUI_CSSParserContext ctx) { FontFaceParserContext data; switch (*ctx->cur) { case '}': case ';': break; default: CSSParser_GetChar(ctx); return 0; } CSSParser_EndBuffer(ctx); data = GetParserContext(ctx); switch (data->key) { case KEY_FONT_FAMILY: if (data->face->font_family) { free(data->face->font_family); } data->face->font_family = malloc(strsize(ctx->buffer)); if (!data->face->font_family) { return -ENOMEM; } strtrim(data->face->font_family, ctx->buffer, " \""); break; case KEY_FONT_STYLE: FontFace_ParseFontStyle(data->face, ctx->buffer); break; case KEY_FONT_WEIGHT: FontFace_ParseFontWeight(data->face, ctx->buffer); break; case KEY_SRC: FontFace_ParseSrc(data->face, ctx->buffer, ctx->style.dirname); break; default: break; } data->key = KEY_NONE; if (*ctx->cur != '}') { ctx->rule.state = FFP_STATE_KEY; return 0; } return FontFaceParser_ParseTail(ctx); }
//Convert ascii double to an actual double. long double stringToDouble(char* doubleString, int size = 0){ long double multiplier = .10; long secondHalf; long firstHalf; int secondSize; int firstSize; int j; compTools tools; //Get size if unknown.. if(!size) size = tools.strsize(doubleString); //Get the whole number part of the doubleString. if(getStringTo(doubleString,'.')){ //Get the size of the whole number part of the doubleString, then convert the whole number portion to an actual integer. firstSize = strsize(tempString); firstHalf = stringToInt(tempString,firstSize); //Based on the remaining characters in the doubleString, create the multiplier for the second whole number, which will become our decimal value. for(j = 0; j < size - firstSize - 1; j++) multiplier *= .10; secondHalf = stringToInt(&doubleString[firstSize + 1],size - firstSize); /* Multiply the second whole number (after the . split) by the multiplier in order to get our decimal value. Then add this value to the first whole number. Return, rinse, and repeat. */ if(firstHalf > 0) return firstHalf + (secondHalf * multiplier); return firstHalf - (secondHalf * multiplier); } else{ //This is not a double, so return 0; return stringToInt(doubleString,size); } }
/* OnUnknowFileType() */ void CFileNameOpenDialog::OnUnknowFileType(LPCSTR lpcszFileName,CDibCtrl& pDibCtrl,LPCSTR lpcszLibraryName,LPSTR lpszInfo,int nInfoSize) { BOOL bIsRecognizedFileType = FALSE; if(CAudioPlayer::IsSupportedFormat(lpcszFileName)) { CAudioInfo audioInfo(lpcszFileName); long lMinutes = 0L; long lSeconds = 0L; audioInfo.GetLength(lMinutes,lSeconds); if(striright(lpcszFileName,MP3_EXTENSION)==0) { _snprintf(lpszInfo, nInfoSize-1 , "Title: %s\r\n" "Artist: %s\r\n" "Album: %s\r\n" "Genre: %s\r\n" "Year: %d\r\n" "Track: %d\r\n" "Bitrate: %d kbps\r\n" "Frequency: %ld khz\r\n" "Duration: %02d:%02d\r\n" "Size: %s\r\n" "MPEG Layer: %s\r\n" "MPEG Version: %s\r\n" "Channel Mode: %s\r\n" , audioInfo.GetTitle(), audioInfo.GetArtist(), audioInfo.GetAlbum(), audioInfo.GetGenre(), audioInfo.GetYear(), audioInfo.GetTrack(), audioInfo.GetBitRate(), audioInfo.GetFrequency(), lMinutes,lSeconds, strsize((double)audioInfo.GetFileSize()), audioInfo.GetLayer(), audioInfo.GetVersion(), audioInfo.GetChannelMode() ); bIsRecognizedFileType = TRUE; } else if(striright(lpcszFileName,WAV_EXTENSION)==0) { _snprintf(lpszInfo, nInfoSize-1 , "Title: %s\r\n" "Bitrate: %d bit\r\n" "Frequency: %ld khz\r\n" "Duration: %02d:%02d\r\n" "Size: %s\r\n" "Version: %s\r\n" "Channel Mode: %s\r\n" , audioInfo.GetTitle(), audioInfo.GetBitRate(), audioInfo.GetFrequency(), lMinutes,lSeconds, strsize((double)audioInfo.GetFileSize()), audioInfo.GetVersion(), audioInfo.GetChannelMode() ); bIsRecognizedFileType = TRUE; } else if(striright(lpcszFileName,CDA_EXTENSION)==0) { _snprintf(lpszInfo, nInfoSize-1 , "CD Audio Track (%s)\r\n" "Duration: %02d:%02d\r\n" , audioInfo.GetTitle(), lMinutes,lSeconds ); bIsRecognizedFileType = TRUE; } } if(bIsRecognizedFileType) { pDibCtrl.SetTransparent(TRUE,RGB(255,0,255)); pDibCtrl.Load(m_szBitmapFileName,lpcszLibraryName); } else pDibCtrl.Clear(); }
char *str_convert_encoding(int from, int to, const char *str) { /* * And here it should just be a matter of calling glib's g_convert(). * Or so I thought. Alas, they chickened out of the hard part: how to * figure out the size of a zero-terminaded string in any arbitrary * encoding. * Since there is no advantage in using g_convert(), might as well * keep my old implementation. It's worth using their iconv wrapppers * though, because they provide libiconv in systems that don't have it * natively. */ GIConv conv; char *result; char *inbuf, *outbuf; size_t inbpc, outbpc; size_t inbytes, outbytes; size_t inbytesleft, outbytesleft; size_t res; inbytes = strsize(from, str); if (strcasecmp(encoding_name(to), encoding_name(from)) == 0) { result = malloc(inbytes); memcpy(result, str, inbytes); return result; } conv = g_iconv_open(encoding_name(to), encoding_name(from)); if (conv == (GIConv)-1) { fprintf(stderr, "convert_encoding: cannot convert from %s to %s\n", encoding_name(from), encoding_name(to)); return NULL; } inbpc = bytes_per_char(from); outbpc = bytes_per_char(to); /* estimate the converted size */ outbytes = ((double)outbpc / (double)inbpc) * inbytes; /* optimize common cases (tuned for western european languages) */ if (to == UTF_8 && inbpc == 1) outbytes = ceil(1.25 * inbytes); else if (to == UTF_16) outbytes += 2; /* for the BOM */ //printf("inbytes : %i\noutbytes: %i\n", inbytes, outbytes); result = malloc(outbytes); inbuf = (char*)str; inbytesleft = inbytes; outbuf = result; outbytesleft = outbytes; while(1) { res = g_iconv(conv, &inbuf, &inbytesleft, &outbuf, &outbytesleft); if (res == (size_t)-1) { if (errno == E2BIG) { /* Ran out of space, alloc more This code tries hard to avoid the need for a second realloc, while still keeping over-allocation to a minimum */ double done = 1.0 - (double)inbytesleft / (double)inbytes; size_t bytes_written = outbuf - result; size_t newsize = ceil((bytes_written / done) * 1.1); //printf("growing: done=%g%%, old size=%i, new size=%i\n", // 100.0*done, outbytes, newsize); outbytesleft += newsize - outbytes; outbytes = newsize; result = realloc(result, outbytes); outbuf = result + bytes_written; continue; } else { /* Invalid or inconvertible char, skip it Seems better than aborting the conversion... */ fprintf(stderr, "convert_encoding: conversion error at offset %i\n", inbytes-inbytesleft); inbuf += inbpc; inbytesleft = max(inbytesleft - inbpc, 0); outbuf += outbpc; outbytesleft = max(outbytesleft - outbpc, 0); continue; } } break; } //printf("%i of %i bytes unused (wasted %g%%)\n", // outbytesleft, outbytes, 100.0*(double)outbytesleft/(double)outbytes); g_iconv_close(conv); return result; }