VALUE munge_xpath_namespace( VALUE orig_expr, xmlChar *root_ns ) { VALUE path_bits = rb_str_split( orig_expr, "/" ); VALUE ns_prefix = rb_str_new2( (const char*)root_ns ); VALUE ns_indic = rb_str_new2( ":" ); VALUE slash = rb_str_new2( "/" ); VALUE path_bit, str_idx; VALUE ret_ary = rb_ary_new(); long i; rb_str_append( ns_prefix, ns_indic ); for (i=0; i<RARRAY(path_bits)->len; i++) { path_bit = RARRAY(path_bits)->ptr[i]; if (RSTRING_LEN(path_bit) > 0) { str_idx = rb_funcall( path_bit, rb_intern( "index" ), 1, ns_indic ); if (str_idx == Qnil || str_idx == Qfalse) // didn't find the :, so it looks like we don't have a namespace path_bit = rb_str_plus( ns_prefix, path_bit ); } rb_ary_push( ret_ary, path_bit ); } return rb_ary_join( ret_ary, slash ); }
static VALUE dh_init(int argc, VALUE* argv, VALUE self) { VALUE db_user, db_pass, db_args, db_name, db_host, tds_socket; TDSSOCKET *tds; /* Let's fill our variables... */ rb_scan_args(argc, argv, "2*", &db_user, &db_pass, &db_args); db_args = rb_ary_shift(db_args); if(db_args != Qnil) { db_args = rb_str_split(db_args, ":"); db_name = rb_ary_entry(db_args, 0); db_host = rb_ary_entry(db_args, 1); } if(db_host == Qnil) { db_host = rb_str_new2("localhost"); } /* Get a TDSSOCKET */ tds_socket = tdss_new(db_host, db_user, db_pass); rb_iv_set(self, "@tds_socket", tds_socket); Data_Get_Struct(tds_socket, TDSSOCKET, tds); /* If the user submited a database-name, change to it */ if(db_name != Qnil) { if(tds_submit_query(tds,STR2CSTR(rb_str_concat(rb_str_new2("USE "), db_name))) != TDS_SUCCEED) { rb_raise(rb_eRuntimeError, "SQL-USE failed (1)"); } else { process_results(tds); } } return self; } // dh_init
void Init_nokogiri() { #ifndef __MACRUBY__ xmlMemSetup( (xmlFreeFunc)ruby_xfree, (xmlMallocFunc)ruby_xmalloc, (xmlReallocFunc)ruby_xrealloc, ruby_strdup ); #endif mNokogiri = rb_define_module("Nokogiri"); mNokogiriXml = rb_define_module_under(mNokogiri, "XML"); mNokogiriHtml = rb_define_module_under(mNokogiri, "HTML"); mNokogiriXslt = rb_define_module_under(mNokogiri, "XSLT"); mNokogiriXmlSax = rb_define_module_under(mNokogiriXml, "SAX"); mNokogiriHtmlSax = rb_define_module_under(mNokogiriHtml, "SAX"); rb_const_set( mNokogiri, rb_intern("LIBXML_VERSION"), NOKOGIRI_STR_NEW2(LIBXML_DOTTED_VERSION) ); rb_const_set( mNokogiri, rb_intern("LIBXML_PARSER_VERSION"), NOKOGIRI_STR_NEW2(xmlParserVersion) ); #ifdef NOKOGIRI_USE_PACKAGED_LIBRARIES rb_const_set(mNokogiri, rb_intern("NOKOGIRI_USE_PACKAGED_LIBRARIES"), Qtrue); rb_const_set(mNokogiri, rb_intern("NOKOGIRI_LIBXML2_PATH"), NOKOGIRI_STR_NEW2(NOKOGIRI_LIBXML2_PATH)); rb_const_set(mNokogiri, rb_intern("NOKOGIRI_LIBXSLT_PATH"), NOKOGIRI_STR_NEW2(NOKOGIRI_LIBXSLT_PATH)); rb_const_set(mNokogiri, rb_intern("NOKOGIRI_LIBXML2_PATCHES"), rb_str_split(NOKOGIRI_STR_NEW2(NOKOGIRI_LIBXML2_PATCHES), " ")); rb_const_set(mNokogiri, rb_intern("NOKOGIRI_LIBXSLT_PATCHES"), rb_str_split(NOKOGIRI_STR_NEW2(NOKOGIRI_LIBXSLT_PATCHES), " ")); #else rb_const_set(mNokogiri, rb_intern("NOKOGIRI_USE_PACKAGED_LIBRARIES"), Qfalse); rb_const_set(mNokogiri, rb_intern("NOKOGIRI_LIBXML2_PATH"), Qnil); rb_const_set(mNokogiri, rb_intern("NOKOGIRI_LIBXSLT_PATH"), Qnil); rb_const_set(mNokogiri, rb_intern("NOKOGIRI_LIBXML2_PATCHES"), Qnil); rb_const_set(mNokogiri, rb_intern("NOKOGIRI_LIBXSLT_PATCHES"), Qnil); #endif #ifdef LIBXML_ICONV_ENABLED rb_const_set(mNokogiri, rb_intern("LIBXML_ICONV_ENABLED"), Qtrue); #else rb_const_set(mNokogiri, rb_intern("LIBXML_ICONV_ENABLED"), Qfalse); #endif xmlInitParser(); init_xml_document(); init_html_document(); init_xml_node(); init_xml_document_fragment(); init_xml_text(); init_xml_cdata(); init_xml_processing_instruction(); init_xml_attr(); init_xml_entity_reference(); init_xml_comment(); init_xml_node_set(); init_xml_xpath_context(); init_xml_sax_parser_context(); init_xml_sax_parser(); init_xml_sax_push_parser(); init_xml_reader(); init_xml_dtd(); init_xml_element_content(); init_xml_attribute_decl(); init_xml_element_decl(); init_xml_entity_decl(); init_xml_namespace(); init_html_sax_parser_context(); init_html_sax_push_parser(); init_xslt_stylesheet(); init_xml_syntax_error(); init_html_entity_lookup(); init_html_element_description(); init_xml_schema(); init_xml_relax_ng(); init_nokogiri_io(); init_xml_encoding_handler(); }
/* * call-seq: * * API::Function.new(address, prototype = 'V', return_type = 'L') * * Creates and returns an API::Function object. This object is similar to an * API object, except that instead of a character function name you pass a * function pointer address as the first argument, and there's no associated * DLL file. * * Once you have your API::Function object you can then call it the same way * you would an API object. * * Example: * * require 'win32/api' * include Win32 * * LoadLibrary = API.new('LoadLibrary', 'P', 'L') * GetProcAddress = API.new('GetProcAddress', 'LP', 'L') * * # Play a system beep * hlib = LoadLibrary.call('user32') * addr = GetProcAddress.call(hlib, 'MessageBeep') * func = Win32::API::Function.new(addr, 'L', 'L') * func.call(0) */ static VALUE func_init(int argc, VALUE* argv, VALUE self){ Win32API* ptr; int i; VALUE v_address, v_proto, v_return; rb_scan_args(argc, argv, "12", &v_address, &v_proto, &v_return); Data_Get_Struct(self, Win32API, ptr); // Convert a string prototype to an array of characters if(rb_respond_to(v_proto, rb_intern("split"))) v_proto = rb_str_split(v_proto, ""); // Convert a nil or empty prototype to 'V' (void) automatically if(NIL_P(v_proto) || RARRAY_LEN(v_proto) == 0){ v_proto = rb_ary_new(); rb_ary_push(v_proto, rb_str_new2("V")); } // Set an arbitrary limit of 20 parameters if(20 < RARRAY_LEN(v_proto)) rb_raise(rb_eArgError, "too many parameters: %d\n", RARRAY_LEN(v_proto)); // Set the default return type to 'L' (DWORD) if(NIL_P(v_return)) v_return = rb_str_new2("L"); ptr->function = (FARPROC)NUM2LONG(v_address); // Push the numeric prototypes onto our int array for later use. for(i = 0; i < RARRAY_LEN(v_proto); i++){ SafeStringValue(RARRAY_PTR(v_proto)[i]); switch(*(char*)StringValuePtr(RARRAY_PTR(v_proto)[i])){ case 'L': ptr->prototype[i] = _T_LONG; break; case 'P': ptr->prototype[i] = _T_POINTER; break; case 'I': case 'B': ptr->prototype[i] = _T_INTEGER; break; case 'V': ptr->prototype[i] = _T_VOID; break; case 'K': ptr->prototype[i] = _T_CALLBACK; break; case 'S': ptr->prototype[i] = _T_STRING; break; default: rb_raise(cAPIProtoError, "Illegal prototype '%s'", StringValuePtr(RARRAY_PTR(v_proto)[i]) ); } } // Store the return type for later use. // Automatically convert empty strings or nil to type void. if(NIL_P(v_return) || RSTRING_LEN(v_return) == 0){ v_return = rb_str_new2("V"); ptr->return_type = _T_VOID; } else{ SafeStringValue(v_return); switch(*RSTRING_PTR(v_return)){ case 'L': ptr->return_type = _T_LONG; break; case 'P': ptr->return_type = _T_POINTER; break; case 'I': case 'B': ptr->return_type = _T_INTEGER; break; case 'V': ptr->return_type = _T_VOID; break; case 'S': ptr->return_type = _T_STRING; break; default: rb_raise(cAPIProtoError, "Illegal return type '%s'", RSTRING_PTR(v_return) ); } } rb_iv_set(self, "@address", v_address); rb_iv_set(self, "@prototype", v_proto); rb_iv_set(self, "@return_type", v_return); return self; }
/* * call-seq: * Win32::API.new(function, prototype='V', return='L', dll='kernel32') * * Creates and returns a new Win32::API object. The +function+ is the name * of the Windows function. * * The +prototype+ is the function prototype for +function+. This can be a * string or an array of characters. The possible valid characters are 'I' * (integer), 'L' (long), 'V' (void), 'P' (pointer), 'K' (callback) or 'S' * (string). * * The default is void ('V'). * * Constant (const char*) strings should use 'S'. Pass by reference string * buffers should use 'P'. The former is faster, but cannot be modified. * * The +return+ argument is the return type for the function. The valid * characters are the same as for the +prototype+. The default is 'L' (long). * * The +dll+ is the name of the DLL file that the function is exported from. * The default is 'kernel32'. * * If the function cannot be found then an API::Error is raised (a subclass * of RuntimeError). * * Example: * * require 'win32/api' * include Win32 * * buf = 0.chr * 260 * len = [buf.length].pack('L') * * GetUserName = API.new('GetUserName', 'PP', 'I', 'advapi32') * GetUserName.call(buf, len) * * puts buf.strip */ static VALUE api_init(int argc, VALUE* argv, VALUE self) { HMODULE hLibrary; FARPROC fProc; Win32API* ptr; int i; char* first = "A"; char* second = "W"; VALUE v_proc, v_proto, v_return, v_dll; rb_scan_args(argc, argv, "13", &v_proc, &v_proto, &v_return, &v_dll); Data_Get_Struct(self, Win32API, ptr); // Convert a string prototype to an array of characters if(rb_respond_to(v_proto, rb_intern("split"))) v_proto = rb_str_split(v_proto, ""); // Convert a nil or empty prototype to 'V' (void) automatically if(NIL_P(v_proto) || RARRAY_LEN(v_proto) == 0){ v_proto = rb_ary_new(); rb_ary_push(v_proto, rb_str_new2("V")); } // Set an arbitrary limit of 20 parameters if(20 < RARRAY_LEN(v_proto)) rb_raise(rb_eArgError, "too many parameters: %d\n", RARRAY_LEN(v_proto)); // Set the default dll to 'kernel32' if(NIL_P(v_dll)) v_dll = rb_str_new2("kernel32"); // Set the default return type to 'L' (DWORD) if(NIL_P(v_return)) v_return = rb_str_new2("L"); SafeStringValue(v_dll); SafeStringValue(v_proc); hLibrary = LoadLibrary(TEXT(RSTRING_PTR(v_dll))); // The most likely cause of failure is a bad DLL load path if(!hLibrary){ rb_raise(cAPILoadError, "LoadLibrary() function failed for '%s': %s", RSTRING_PTR(v_dll), StringError(GetLastError()) ); } ptr->library = hLibrary; /* Attempt to get the function. If it fails, try again with an 'A' * appended. If that fails, try again with a 'W' appended. If that * still fails, raise an API::LoadLibraryError. */ fProc = GetProcAddress(hLibrary, TEXT(RSTRING_PTR(v_proc))); // Skip the ANSI and Wide function checks for MSVCRT functions. if(!fProc){ if(strstr(RSTRING_PTR(v_dll), "msvcr")){ rb_raise( cAPILoadError, "Unable to load function '%s'", RSTRING_PTR(v_proc) ); } else{ VALUE v_ascii = rb_str_new3(v_proc); v_ascii = rb_str_cat(v_ascii, first, 1); fProc = GetProcAddress(hLibrary, TEXT(RSTRING_PTR(v_ascii))); if(!fProc){ VALUE v_unicode = rb_str_new3(v_proc); v_unicode = rb_str_cat(v_unicode, second, 1); fProc = GetProcAddress(hLibrary, TEXT(RSTRING_PTR(v_unicode))); if(!fProc){ rb_raise( cAPILoadError, "Unable to load function '%s', '%s', or '%s'", RSTRING_PTR(v_proc), RSTRING_PTR(v_ascii), RSTRING_PTR(v_unicode) ); } else{ rb_iv_set(self, "@effective_function_name", v_unicode); } } else{ rb_iv_set(self, "@effective_function_name", v_ascii); } } } else{ rb_iv_set(self, "@effective_function_name", v_proc); } ptr->function = fProc; // Push the numeric prototypes onto our int array for later use. for(i = 0; i < RARRAY_LEN(v_proto); i++){ SafeStringValue(RARRAY_PTR(v_proto)[i]); switch(*(char*)StringValuePtr(RARRAY_PTR(v_proto)[i])){ case 'L': ptr->prototype[i] = _T_LONG; break; case 'P': ptr->prototype[i] = _T_POINTER; break; case 'I': case 'B': ptr->prototype[i] = _T_INTEGER; break; case 'V': ptr->prototype[i] = _T_VOID; break; case 'K': ptr->prototype[i] = _T_CALLBACK; break; case 'S': ptr->prototype[i] = _T_STRING; break; default: rb_raise(cAPIProtoError, "Illegal prototype '%s'", StringValuePtr(RARRAY_PTR(v_proto)[i]) ); } } // Store the return type for later use. // Automatically convert empty strings or nil to type void. if(NIL_P(v_return) || RSTRING_LEN(v_return) == 0){ v_return = rb_str_new2("V"); ptr->return_type = _T_VOID; } else{ SafeStringValue(v_return); switch(*RSTRING_PTR(v_return)){ case 'L': ptr->return_type = _T_LONG; break; case 'P': ptr->return_type = _T_POINTER; break; case 'I': case 'B': ptr->return_type = _T_INTEGER; break; case 'V': ptr->return_type = _T_VOID; break; case 'S': ptr->return_type = _T_STRING; break; default: rb_raise(cAPIProtoError, "Illegal return type '%s'", RSTRING_PTR(v_return) ); } } rb_iv_set(self, "@dll_name", v_dll); rb_iv_set(self, "@function_name", v_proc); rb_iv_set(self, "@prototype", v_proto); rb_iv_set(self, "@return_type", v_return); return self; }
VALUE string_spec_rb_str_split(VALUE self, VALUE str) { return rb_str_split(str, ","); }
VALUE ss_str_split(VALUE self, VALUE str) { return rb_str_split(str, ","); }
void strb_InitializeSdlFont(void) { if (TTF_Init()) { rb_raise_sdl_ttf_error(); } fontFileInfos = ALLOC(FontFileInfo); fontFileInfos->rbFontNameSymbol = Qundef; fontFileInfos->rbFileNameSymbol = Qundef; fontFileInfos->ttcIndex = -1; fontFileInfos->next = NULL; FontFileInfo* currentInfo = fontFileInfos; (void)currentInfo; #ifdef WIN32 HKEY hKey; TCHAR* regPath = _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"); if (SUCCEEDED(RegOpenKeyEx(HKEY_LOCAL_MACHINE, regPath, 0, KEY_READ, &hKey))) { DWORD fontNameBuffMaxLength; DWORD fileNameBuffMaxByteLength; RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &fontNameBuffMaxLength, &fileNameBuffMaxByteLength, NULL, NULL); TCHAR fontNameBuff[fontNameBuffMaxLength + 1]; BYTE fileNameByteBuff[fileNameBuffMaxByteLength]; for (DWORD dwIndex = 0; ;dwIndex++) { ZeroMemory(fontNameBuff, sizeof(fontNameBuff)); ZeroMemory(fileNameByteBuff, sizeof(fileNameByteBuff)); DWORD fontNameBuffLength = sizeof(fontNameBuff) / sizeof(TCHAR); DWORD fileNameBuffByteLength = fileNameBuffMaxByteLength; LONG result = RegEnumValue(hKey, dwIndex, fontNameBuff, &fontNameBuffLength, NULL, NULL, fileNameByteBuff, &fileNameBuffByteLength); TCHAR* fileNameBuff = (TCHAR*)fileNameByteBuff; DWORD fileNameBuffLength = _tcslen(fileNameBuff); if (result == ERROR_SUCCESS) { const TCHAR* ext = &(fileNameBuff[fileNameBuffLength - 3]); if (tolower(ext[0]) == _T('t') && tolower(ext[1]) == _T('t') && (tolower(ext[2]) == _T('f') || tolower(ext[2]) == _T('c'))) { TCHAR* fontName = fontNameBuff; const TCHAR* fileName = fileNameBuff; // A TTF font name must end with ' (TrueType)'. fontName[fontNameBuffLength - 11] = _T('\0'); for (int i = fileNameBuffLength - 1; 0 <= i; i--) { if (fileName[i] == _T('\\')) { fileName += i + 1; break; } } int length = WideCharToMultiByte(CP_UTF8, 0, fontName, -1, NULL, 0, NULL, NULL); char fontNameUTF8[length]; WideCharToMultiByte(CP_UTF8, 0, fontName, -1, fontNameUTF8, length, NULL, NULL); volatile VALUE rbFontName = rb_str_new2(fontNameUTF8); length = WideCharToMultiByte(CP_ACP, 0, fileName, -1, NULL, 0, NULL, NULL); char fileNameANSI[length]; WideCharToMultiByte(CP_ACP, 0, fileName, -1, fileNameANSI, length, NULL, NULL); volatile VALUE rbFileName = rb_str_new2(fileNameANSI); if (strchr(StringValueCStr(rbFontName), '&')) { volatile VALUE rbArr = rb_str_split(rbFontName, "&"); const int arrLength = RARRAY_LEN(rbArr); int ttcIndex = 0; for (int i = 0; i < arrLength; i++) { volatile VALUE rbFontName = rb_ary_entry(rbArr, i); rb_funcall(rbFontName, rb_intern("strip!"), 0); if (0 < RSTRING_LEN(rbFontName)) { volatile VALUE rbFontNameSymbol = rb_str_intern(rbFontName); volatile VALUE rbFileNameSymbol = rb_str_intern(rbFileName); ADD_INFO(currentInfo, rbFontNameSymbol, rbFileNameSymbol, ttcIndex); ttcIndex++; } } } else { volatile VALUE rbFontNameSymbol = rb_str_intern(rbFontName); volatile VALUE rbFileNameSymbol = rb_str_intern(rbFileName); ADD_INFO(currentInfo, rbFontNameSymbol, rbFileNameSymbol, -1); } } } else { break; } } RegCloseKey(hKey); } else { rb_raise(strb_GetStarRubyErrorClass(), "Win32API error: %d", (int)GetLastError()); } TCHAR szWindowsFontDirPath[MAX_PATH + 1]; if (FAILED(SHGetFolderPath(NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT, szWindowsFontDirPath))) { rb_raise(strb_GetStarRubyErrorClass(), "Win32API error: %d", (int)GetLastError()); } int length = WideCharToMultiByte(CP_UTF8, 0, szWindowsFontDirPath, -1, NULL, 0, NULL, NULL); char szWindowsFontDirPathUTF8[length]; WideCharToMultiByte(CP_UTF8, 0, szWindowsFontDirPath, -1, szWindowsFontDirPathUTF8, length, NULL, NULL); volatile VALUE rbWindowsFontDirPath = rb_str_new2(szWindowsFontDirPathUTF8); rbWindowsFontDirPathSymbol = rb_str_intern(rbWindowsFontDirPath); #endif }