/* * this creates an arbitrary mapping from positions to colors given as * (r,g,b) triples * * the colormap size is set to the length of the vectors * * the Rs, Gs, and Bs are VALUEs from 0 to 1 representing the * intensity of the color component */ OBJ_PTR c_convert_to_colormap(OBJ_PTR fmkr, FM* p, OBJ_PTR Rs, OBJ_PTR Gs, OBJ_PTR Bs, int *ierr) { long r_len, g_len, b_len; double *r_ptr = Vector_Data_for_Read(Rs, &r_len, ierr); if (*ierr != 0) RETURN_NIL; double *g_ptr = Vector_Data_for_Read(Gs, &g_len, ierr); if (*ierr != 0) RETURN_NIL; double *b_ptr = Vector_Data_for_Read(Bs, &b_len, ierr); if (*ierr != 0) RETURN_NIL; if (r_len <= 0 || r_len != g_len || b_len != g_len) { RAISE_ERROR("Sorry: vectors for convert_to_colormap must all be of " "same length", ierr); RETURN_NIL; } int i, j, buff_len = r_len * 3; unsigned char *buff; buff = ALLOC_N_unsigned_char(buff_len); for (i = 0, j = 0; j < r_len; j++) { buff[i++] = ROUND(r_ptr[j]*255); buff[i++] = ROUND(g_ptr[j]*255); buff[i++] = ROUND(b_ptr[j]*255); } OBJ_PTR lookup = String_New((char *)buff, buff_len); free(buff); OBJ_PTR result = Array_New(2); Array_Store(result, 0, Integer_New(r_len-1), ierr); Array_Store(result, 1, lookup, ierr); if (*ierr != 0) RETURN_NIL; return result; }
void _Symbol_Init_AllocName ( Symbol * symbol, byte * name, int32 allocType ) { if ( name ) { byte* sname = name ? String_New ( name, allocType ) : 0 ; _Symbol_Init ( symbol, sname ) ; } }
byte * _CfrTil_UsingToString ( ) { Buffer * buffer = Buffer_New ( BUFFER_SIZE ) ; byte * b = Buffer_Data ( buffer ) ; strcpy ( ( char* ) b, "" ) ; _Tree_Map_State_2 ( _Q_->OVT_CfrTil->Namespaces->Lo_List, USING, ( MapSymbolFunction2 ) _Namespace_Symbol_Print, 0, ( int32 ) b ) ; b = String_New ( ( byte* ) b, TEMPORARY ) ; Buffer_SetAsUnused ( buffer ) ; return b ; }
def(void, Init, Server_Client *client, Logger *logger) { this->logger = logger; this->client = client; this->conn = &client->socket.conn; this->resp = String_New(2048); this->method = HTTP_Method_Get; this->version = HTTP_Version_1_0; this->persistent = false; this->path = String_New(0); /* paramTest and paramTest2 must not be longer than 256 bytes, * otherwise an `HTTP_Query_ExceedsPermittedLengthException' * exception will be thrown. */ this->paramTest = String_New(256); this->paramTest2 = String_New(256); this->server = HTTP_Server_New(this->conn, 2048, 4096); HTTP_Server_BindRequest(&this->server, HTTP_Server_OnRequest_For(this, ref(OnRequest))); HTTP_Server_BindRequestInfo(&this->server, HTTP_OnRequestInfo_For(this, ref(OnRequestInfo))); HTTP_Server_BindQueryParameter(&this->server, HTTP_OnParameter_For(this, ref(OnQueryParameter))); HTTP_Server_BindBodyParameter(&this->server, HTTP_OnParameter_For(this, ref(OnBodyParameter))); HTTP_Server_BindRespond(&this->server, HTTP_Server_OnRespond_For(this, ref(OnRespond))); String strFd = Integer_ToString(this->conn->ch.id); Logger_Info(this->logger, $("Incoming connection (fd=%)."), strFd.rd); String_Destroy(&strFd); }
void _CfrTil_FinishSourceCode ( Word * word ) { // keep a LambdaCalculus LO_Define0 created SourceCode value if ( ! word->SourceCode ) word->SourceCode = String_New ( _Q_->OVT_CfrTil->SourceCodeScratchPad, DICTIONARY ) ; Lexer_SourceCodeOff ( _Context_->Lexer0 ) ; SetState ( _Q_->OVT_CfrTil, SOURCE_CODE_INITIALIZED, false ) ; if ( GetState ( _Q_->OVT_CfrTil, SOURCE_CODE_MODE ) ) { //word->DebugWordList = _CfrTil_->DebugWordList ; _CfrTil_->DebugWordList = 0 ; //_dllist_New ( CFRTIL ) ; } }
overload def(void, Init) { this->status = HTTP_Status_Unset; this->closed = true; this->resp = String_New(ref(BufferSize)); this->host = CarrierString_New(); this->port = 80; this->onResponseInfo = HTTP_OnResponseInfo_Empty(); this->onHeader = HTTP_OnHeader_Empty(); this->socket = SocketClient_New(Socket_Protocol_TCP, Socket_Option_Blocking | Socket_Option_CloseOnExec); }
static sdef(void, onData, Instance inst) { ref(Process) *procItem = inst.addr; printf("onData fd=%i\n", procItem->entry->ch->id); fflush(stdout); String s = String_New(1024); s.len = Channel_Read(procItem->entry->ch, s.buf, String_GetSize(s)); if (String_Equals(s.rd, $("Hello World\n"))) { System_out($("Received 'Hello World'.\n")); } else { System_out($("Received unexpected data.")); } String_Destroy(&s); }
static OBJ_PTR c_create_colormap(FM *p, bool rgb_flag, int length, int num_pts, double *ps, double *c1s, double *c2s, double *c3s, int *ierr) { int i; if (ps[0] != 0.0 || ps[num_pts-1] != 1.0) { RAISE_ERROR("Sorry: first control point for create colormap must be " "at 0.0 and last must be at 1.0", ierr); RETURN_NIL; } for (i = 1; i < num_pts; i++) { if (ps[i-1] > ps[i]) { RAISE_ERROR("Sorry: control points for create colormap must be " "increasing from 0 to 1", ierr); RETURN_NIL; } } int j, buff_len = length * 3, hival = length - 1; unsigned char *buff; buff = ALLOC_N_unsigned_char(buff_len); for (j = 0, i = 0; j < length; j++) { double x = j; x /= (length-1); double c1, c2, c3, r, g, b; c1 = linear_interpolate(num_pts, ps, c1s, x); c2 = linear_interpolate(num_pts, ps, c2s, x); c3 = linear_interpolate(num_pts, ps, c3s, x); if (rgb_flag) { r = c1; g = c2; b = c3; } else convert_hls_to_rgb(c1, c2, c3, &r, &g, &b); buff[i++] = ROUND(hival * r); buff[i++] = ROUND(hival * g); buff[i++] = ROUND(hival * b); } OBJ_PTR lookup = String_New((char *)buff, buff_len); free(buff); OBJ_PTR result = Array_New(2); Array_Store(result, 0, Integer_New(hival), ierr); Array_Store(result, 1, lookup, ierr); if (*ierr != 0) RETURN_NIL; return result; }
static sdef(void, onData, Instance inst) { ref(Process) *procItem = inst.addr; printf("onData fd=%i\n", procItem->entry->ch->id); fflush(stdout); String s = String_New(1024); for (;;) { s.len = Channel_Read(procItem->entry->ch, s.buf, String_GetSize(s)); if (s.len == 0) { break; } System_out($("read data='")); System_out(s.rd); System_out($("'\n")); } String_Destroy(&s); }
OBJ_PTR c_private_create_monochrome_image_data(OBJ_PTR fmkr, FM *p, OBJ_PTR table, int first_row, int last_row, int first_column, int last_column, double boundary, bool reversed, int *ierr) { long num_cols, num_rows; double **data = Table_Data_for_Read(table, &num_cols, &num_rows, ierr); if (*ierr != 0) RETURN_NIL; if (first_column < 0) first_column += num_cols; if (first_column < 0 || first_column >= num_cols) RAISE_ERROR_i("Sorry: invalid first_column specification (%i)", first_column, ierr); if (last_column < 0) last_column += num_cols; if (last_column < 0 || last_column >= num_cols) RAISE_ERROR_i("Sorry: invalid last_column specification (%i)", last_column, ierr); if (first_row < 0) first_row += num_rows; if (first_row < 0 || first_row >= num_rows) RAISE_ERROR_i("Sorry: invalid first_row specification (%i)", first_row, ierr); if (last_row < 0) last_row += num_rows; if (last_row < 0 || last_row >= num_rows) RAISE_ERROR_i("Sorry: invalid last_row specification (%i)", last_row, ierr); int i, j, k; int width = last_column - first_column + 1; int height = last_row - first_row + 1; int bytes_per_row = (width+7)/8; int sz = bytes_per_row * 8 * height; if (sz <= 0) RAISE_ERROR_ii("Sorry: invalid data specification: width (%i) " "height (%i)", width, height, ierr); if (*ierr != 0) RETURN_NIL; // to simplify the process, do it in two stages: first get the // values and then pack the bits char *buff = ALLOC_N_char(sz); for (k = 0, i = first_row; i <= last_row; i++) { double *row = data[i]; for (j = first_column; j <= last_column; j++) { double val = row[j]; buff[k++] = (reversed)? (val <= boundary) : (val > boundary); } for (j = last_column+1; j < bytes_per_row * 8; j++) { buff[k++] = 0; } } int num_bytes = (sz+7) >> 3; char *bits = ALLOC_N_char(num_bytes), c = 0; int num_bits = num_bytes << 3; for (i = 0, k = -1; i < num_bits; i++) { int bit = (i < sz)? buff[i] : 0; int which_bit = i & 7; if (which_bit != 0) c |= bit << (7-which_bit); else { if (k >= 0) bits[k] = c; k++; c = bit << 7; } } bits[k] = c; OBJ_PTR result = String_New(bits, num_bytes); free(bits); free(buff); return result; }
OBJ_PTR c_private_create_image_data(OBJ_PTR fmkr, FM *p, OBJ_PTR table, int first_row, int last_row, int first_column, int last_column, double min_val, double max_val, int max_code, int if_below_range, int if_above_range, int *ierr) { long num_cols, num_rows; double **data = Table_Data_for_Read(table, &num_cols, &num_rows, ierr); if (*ierr != 0) RETURN_NIL; if (first_column < 0) first_column += num_cols; if (first_column < 0 || first_column >= num_cols) RAISE_ERROR_i("Sorry: invalid first_column specification (%i)", first_column, ierr); if (last_column < 0) last_column += num_cols; if (last_column < 0 || last_column >= num_cols) RAISE_ERROR_i("Sorry: invalid last_column specification (%i)", last_column, ierr); if (first_row < 0) first_row += num_rows; if (first_row < 0 || first_row >= num_rows) RAISE_ERROR_i("Sorry: invalid first_row specification (%i)", first_row, ierr); if (last_row < 0) last_row += num_rows; if (last_row < 0 || last_row >= num_rows) RAISE_ERROR_i("Sorry: invalid last_row specification (%i)", last_row, ierr); if (min_val >= max_val) RAISE_ERROR_gg("Sorry: invalid range specification: min %g max %g", min_val, max_val, ierr); if (max_code <= 0 || max_code > 255) RAISE_ERROR_i("Sorry: invalid max_code specification (%i)", max_code, ierr); if (if_below_range < 0 || if_below_range > 255) RAISE_ERROR_i("Sorry: invalid if_below_range specification (%i)", if_below_range, ierr); if (if_above_range < 0 || if_above_range > 255) RAISE_ERROR_i("Sorry: invalid if_above_range specification (%i)", if_above_range, ierr); int i, j, k; int width = last_column - first_column + 1; int height = last_row - first_row + 1; int sz = width * height; if (sz <= 0) RAISE_ERROR_ii("Sorry: invalid data specification: width (%i) " "height (%i)", width, height, ierr); if (*ierr != 0) RETURN_NIL; char *buff = ALLOC_N_char(sz); for (k = 0, i = first_row; i <= last_row; i++) { double *row = data[i]; for (j = first_column; j <= last_column; j++) { double val = row[j]; if (val < min_val) buff[k++] = if_below_range; else if (val > max_val) buff[k++] = if_above_range; else { val = max_code * (val - min_val)/(max_val - min_val); buff[k++] = ROUND(val); } } } OBJ_PTR result = String_New(buff, sz); free(buff); return result; }
#import "Builder.h" #define self Builder rsdef(self, new, Terminal *term, Logger *logger, Deps *deps) { return (self) { .term = term, .deps = deps, .logger = logger, .output = String_New(0), .cc = String_Clone($("/usr/bin/clang")), .inclhdr = String_New(0), .runtime = String_New(0), .manifest = false, .library = false, .dbgsym = false, .std = String_Clone($("gnu99")), .blocks = true, .optmlevel = 0, .verbose = false, .link = StringArray_New(0), .mappings = MappingArray_New(0), .linkpaths = StringArray_New(0), .workers = CPU_getCores() }; } def(void, destroy) { String_Destroy(&this->output); String_Destroy(&this->cc); String_Destroy(&this->inclhdr);
def(void, Get, String prefix, ListingItem *item, String url) { this->location.len = 0; HTTP_Client client; URL_Parts parts = URL_Parse(url); HTTP_Client_Init(&client, parts.host); HTTP_Client_Events events; events.onVersion = (HTTP_OnVersion) EmptyCallback(); events.onHeader = (HTTP_OnHeader) Callback(this, ref(OnHeader)); HTTP_Client_SetEvents(&client, events); HTTP_Client_Request(&client, HTTP_Client_CreateRequest( parts.host, parts.path)); Logger_Debug(this->logger, $("URL: %"), url); URL_Parts_Destroy(&parts); HTTP_Client_FetchResponse(&client); size_t cnt = 0; bool error = false; if (this->location.len > 0) { if (cnt > 3) { Logger_Error(this->logger, $("Redirection loop.")); goto out; } cnt++; Logger_Debug(this->logger, $("Redirecting...")); call(Get, prefix, item, this->location); goto out; } String full = call(BuildPath, prefix, item, scall(GetMediaExtension, parts.path)); Logger_Debug(this->logger, $("Destination: %"), full); File file; File_Open(&file, full, FileStatus_Create | FileStatus_Truncate | FileStatus_WriteOnly); String_Destroy(&full); BufferedStream output; BufferedStream_Init(&output, File_AsStream(&file)); BufferedStream_SetOutputBuffer(&output, 128 * 1024); u64 got = 0; s64 total = HTTP_Client_GetLength(&client); size_t prevPercent = 100; String buf = String_New(HTTP_Client_ReadChunkSize); Terminal_ProgressBar pbar; Terminal_ProgressBar_Init(&pbar, &term); while (HTTP_Client_Read(&client, &buf)) { got += buf.len; try { BufferedStream_Write(&output, buf.buf, buf.len); } clean catch(File, WritingFailed) { error = true; excBreak; } finally { } tryEnd; size_t percent = (size_t) ((float) got / (float) total * 100); if (prevPercent != percent) { float gotmb = (float) got / 1024 / 1024; float totalmb = (float) total / 1024 / 1024; String strGot = Float_ToString(gotmb, 0.01, '.'); String strTotal = Float_ToString(totalmb, 0.01, '.'); String msg = String_Format($("Received % MiB of % MiB"), strGot, strTotal); String_Destroy(&strTotal); String_Destroy(&strGot); Terminal_ProgressBar_Render(&pbar, percent, msg); String_Destroy(&msg); } prevPercent = percent; } Terminal_ProgressBar_Clear(&pbar); String_Destroy(&buf); if (error) { /* Don't flush when closing the file. */ BufferedStream_Reset(&output); } BufferedStream_Close(&output); BufferedStream_Destroy(&output); out: HTTP_Client_Destroy(&client); if (error) { throw(DownloadFailed); } }