/* _WinMain: * Entry point for Windows GUI programs, hooked by a macro in alwin.h, * which makes it look as if the application can still have a normal * main() function. */ int _WinMain(void *_main, void *hInst, void *hPrev, char *Cmd, int nShow) { int (*mainfunc) (int argc, char *argv[]) = (int (*)(int, char *[]))_main; char *argbuf; char *cmdline; char **argv; int argc; int argc_max; int i, q; (void)hInst; (void)hPrev; (void)Cmd; (void)nShow; /* can't use parameter because it doesn't include the executable name */ cmdline = GetCommandLine(); i = strlen(cmdline) + 1; argbuf = al_malloc(i); memcpy(argbuf, cmdline, i); argc = 0; argc_max = 64; argv = al_malloc(sizeof(char *) * argc_max); if (!argv) { al_free(argbuf); return 1; } i = 0; /* parse commandline into argc/argv format */ while (argbuf[i]) { while ((argbuf[i]) && (isspace(argbuf[i]))) i++; if (argbuf[i]) { if ((argbuf[i] == '\'') || (argbuf[i] == '"')) { q = argbuf[i++]; if (!argbuf[i]) break; } else q = 0; argv[argc++] = &argbuf[i]; if (argc >= argc_max) { argc_max += 64; argv = al_realloc(argv, sizeof(char *) * argc_max); if (!argv) { al_free(argbuf); return 1; } } while ((argbuf[i]) && ((q) ? (argbuf[i] != q) : (!isspace(argbuf[i])))) i++; if (argbuf[i]) { argbuf[i] = 0; i++; } } } argv[argc] = NULL; /* call the application entry point */ i = mainfunc(argc, argv); al_free(argv); al_free(argbuf); return i; }
VALUE TCPClient::tcp_recv(int max_buffer) { RAGE_CHECK_DISPOSED_RET(disposed, Qnil); char *buffer = nullptr; VALUE ret_str; int result = 0; if (max_buffer == 0) { buffer = new char[RAGE_CLIENT_TCP_MAX_BUFFER]; char *data = nullptr; size_t max_len = 0, i, pos = 0; do { memset(buffer, 0, RAGE_CLIENT_TCP_MAX_BUFFER); result = recv(sock, buffer, RAGE_CLIENT_TCP_MAX_BUFFER, 0); if (result > 0) { data = (char*)al_realloc(data, max_len + result); for (i = 0; i < result; i++) data[pos++] = buffer[i]; max_len += result; } else if (result == 0) { disconnect(); } } while(result > 0); ret_str = rb_str_new(data, max_len); if (data != nullptr) al_free(data); } else { buffer = new char[max_buffer]; memset(buffer, 0, max_buffer); result = recv(sock, buffer, max_buffer, 0); if (result > 0) { ret_str = rb_str_new(buffer, result); } else ret_str = Qnil; } return ret_str; }
ALLEGRO_SAMPLE *_al_load_voc_f(ALLEGRO_FILE *file) { AL_VOC_DATA *vocdata; ALLEGRO_SAMPLE *sample = NULL; size_t pos = 0; /* where to write in the buffer */ size_t read = 0; /*bytes read during last operation */ char* buffer; size_t bytestoread = 0; bool endofvoc = false; vocdata = al_malloc(sizeof(AL_VOC_DATA)); memset(vocdata, 0, sizeof(*vocdata)); /* * Open file and populate VOC DATA, then create a buffer for the number of * samples of the frst block. * Iterate on the following blocks till EOF or terminator block */ vocdata = voc_open(file); if (!vocdata) return NULL; ALLEGRO_DEBUG("channels %d\n", vocdata->channels); ALLEGRO_DEBUG("word_size %d\n", vocdata->sample_size); ALLEGRO_DEBUG("rate %d\n", vocdata->samplerate); ALLEGRO_DEBUG("first_block_samples %d\n", vocdata->samples); ALLEGRO_DEBUG("first_block_size %d\n", vocdata->samples * vocdata->sample_size); /* * Let's allocate at least the first block's bytes; */ buffer = al_malloc(vocdata->samples * vocdata->sample_size); if (!buffer) { return NULL; } /* * We now need to iterate over data blocks till either we hit end of file * or we find a terminator block. */ bytestoread = vocdata->samples * vocdata->sample_size; while(!endofvoc && !al_feof(vocdata->file)) { uint32_t blocktype = 0; uint32_t x = 0, len = 0; read = al_fread(vocdata->file, buffer, bytestoread); pos += read; READNBYTES(vocdata->file, blocktype, 1, NULL); // read next block type if (al_feof(vocdata->file)) break; switch (blocktype) { case 0:{ /* we found a terminator block */ endofvoc = true; break; } case 2:{ /*we found a continuation block: unlikely but handled */ x = 0; bytestoread = 0; READNBYTES(vocdata->file, bytestoread, 2, NULL); READNBYTES(vocdata->file, x, 1, NULL); bytestoread += x<<16; /* increase subsequently storage */ buffer = al_realloc(buffer, sizeof(buffer) + bytestoread); break; } case 1: // we found a NEW data block starter, I assume this is wrong case 8: // and let the so far read sample data correctly in the case 9:{ // already allocated buffer. endofvoc = true; break; } case 3: /* we found a pause block */ case 4: /* we found a marker block */ case 5: /* we found an ASCII c-string block */ case 6: /* we found a repeat block */ case 7:{ /* we found an end repeat block */ /* all these blocks will be skipped */ unsigned int ii; len = 0; x = 0; READNBYTES(vocdata->file, len, 2, NULL); READNBYTES(vocdata->file, x, 1, NULL); len += x<<16; // this is the length what's left to skip */ for (ii = 0; ii < len ; ++ii) { al_fgetc(vocdata->file); } bytestoread = 0; //should let safely check for the next block */ break; } default: break; } } sample = al_create_sample(buffer, pos, vocdata->samplerate, _al_word_size_to_depth_conf(vocdata->sample_size), _al_count_to_channel_conf(vocdata->channels), true); if (!sample) al_free(buffer); voc_close(vocdata); return sample; }
static bool init_nine_patch_side(NINE_PATCH_SIDE *ps, ALLEGRO_BITMAP *bmp, int vertical) { const int len = vertical ? al_get_bitmap_height(bmp) : al_get_bitmap_width(bmp); int i, s, t, n, z; ALLEGRO_COLOR c; int alloc = 8; ps->m = al_malloc(alloc * sizeof(*ps->m)); for (i = 1, s = -1, t = 0, n = 0, z = -1; i < len; ++i) { int zz; uint8_t r, g, b, a; c = vertical ? al_get_pixel(bmp, 0, i) : al_get_pixel(bmp, i, 0); al_unmap_rgba(c, &r, &g, &b, &a); if (i == len - 1) zz = -1; else if (r == 0 && g == 0 && b == 0 && a == 255) zz = 0; else if (a == 0 || r + g + b + a == 255 * 4) zz = 1; else return false; if (z != zz) { if (s != -1) { ps->m[n].offset = s; ps->m[n].length = i - s; if (z == 0) { ps->m[n].ratio = 1; t += ps->m[n].length; } else { ps->m[n].ratio = 0; } ++n; } s = i; z = zz; } if (n == alloc) { alloc *= 2; ps->m = al_realloc(ps->m, alloc * sizeof(*ps->m)); } } if (n != alloc) { ps->m = al_realloc(ps->m, n * sizeof(*ps->m)); } ps->count = n; ps->fix = len - 2 - t; for (i = 0; i < n; ++i) { if (ps->m[i].ratio) ps->m[i].ratio = ps->m[i].length / (float) t; } return true; }