static int add_menu_item (HMENU menu, widget_value *wv, HMENU item) { UINT fuFlags; char *out_string, *p, *q; int return_value; size_t nlen, orig_len; USE_SAFE_ALLOCA; if (menu_separator_name_p (wv->name)) { fuFlags = MF_SEPARATOR; out_string = NULL; } else { if (wv->enabled) fuFlags = MF_STRING; else fuFlags = MF_STRING | MF_GRAYED; if (wv->key != NULL) { out_string = SAFE_ALLOCA (strlen (wv->name) + strlen (wv->key) + 2); p = stpcpy (out_string, wv->name); p = stpcpy (p, "\t"); strcpy (p, wv->key); } else out_string = (char *)wv->name; /* Quote any special characters within the menu item's text and key binding. */ nlen = orig_len = strlen (out_string); if (unicode_append_menu) { /* With UTF-8, & cannot be part of a multibyte character. */ for (p = out_string; *p; p++) { if (*p == '&') nlen++; } } #ifndef NTGUI_UNICODE else { /* If encoded with the system codepage, use multibyte string functions in case of multibyte characters that contain '&'. */ for (p = out_string; *p; p = _mbsinc (p)) { if (_mbsnextc (p) == '&') nlen++; } } #endif /* !NTGUI_UNICODE */ if (nlen > orig_len) { p = out_string; out_string = SAFE_ALLOCA (nlen + 1); q = out_string; while (*p) { if (unicode_append_menu) { if (*p == '&') *q++ = *p; *q++ = *p++; } #ifndef NTGUI_UNICODE else { if (_mbsnextc (p) == '&') { _mbsncpy (q, p, 1); q = _mbsinc (q); } _mbsncpy (q, p, 1); p = _mbsinc (p); q = _mbsinc (q); } #endif /* !NTGUI_UNICODE */ } *q = '\0'; } if (item != NULL) fuFlags = MF_POPUP; else if (wv->title || wv->call_data == 0) { /* Only use MF_OWNERDRAW if GetMenuItemInfo is usable, since we can't deallocate the memory otherwise. */ if (get_menu_item_info) { out_string = (char *) local_alloc (strlen (wv->name) + 1); strcpy (out_string, wv->name); #ifdef MENU_DEBUG DebPrint ("Menu: allocating %ld for owner-draw", out_string); #endif fuFlags = MF_OWNERDRAW | MF_DISABLED; } else fuFlags = MF_DISABLED; } /* Draw radio buttons and tickboxes. */ else if (wv->selected && (wv->button_type == BUTTON_TYPE_TOGGLE || wv->button_type == BUTTON_TYPE_RADIO)) fuFlags |= MF_CHECKED; else fuFlags |= MF_UNCHECKED; } if (unicode_append_menu && out_string) { /* Convert out_string from UTF-8 to UTF-16-LE. */ int utf8_len = strlen (out_string); WCHAR * utf16_string; if (fuFlags & MF_OWNERDRAW) utf16_string = local_alloc ((utf8_len + 1) * sizeof (WCHAR)); else utf16_string = SAFE_ALLOCA ((utf8_len + 1) * sizeof (WCHAR)); utf8to16 ((unsigned char *)out_string, utf8_len, utf16_string); return_value = unicode_append_menu (menu, fuFlags, item != NULL ? (UINT_PTR) item : (UINT_PTR) wv->call_data, utf16_string); #ifndef NTGUI_UNICODE /* Fallback does not apply when always UNICODE */ if (!return_value) { /* On W9x/ME, Unicode menus are not supported, though AppendMenuW apparently does exist at least in some cases and appears to be stubbed out to do nothing. out_string is UTF-8, but since our standard menus are in English and this is only going to happen the first time a menu is used, the encoding is of minor importance compared with menus not working at all. */ return_value = AppendMenu (menu, fuFlags, item != NULL ? (UINT_PTR) item: (UINT_PTR) wv->call_data, out_string); /* Don't use Unicode menus in future, unless this is Windows NT or later, where a failure of AppendMenuW does NOT mean Unicode menus are unsupported. */ if (osinfo_cache.dwPlatformId != VER_PLATFORM_WIN32_NT) unicode_append_menu = NULL; } #endif /* NTGUI_UNICODE */ if (unicode_append_menu && (fuFlags & MF_OWNERDRAW)) local_free (out_string); } else { return_value = AppendMenu (menu, fuFlags, item != NULL ? (UINT_PTR) item : (UINT_PTR) wv->call_data, out_string ); } /* This must be done after the menu item is created. */ if (!wv->title && wv->call_data != 0) { if (set_menu_item_info) { MENUITEMINFO info; memset (&info, 0, sizeof (info)); info.cbSize = sizeof (info); info.fMask = MIIM_DATA; /* Set help string for menu item. Leave it as a pointer to a Lisp_String until it is ready to be displayed, since GC can happen while menus are active. */ if (!NILP (wv->help)) { /* We use XUNTAG below because in a 32-bit build --with-wide-int we cannot pass a Lisp_Object via a DWORD member of MENUITEMINFO. */ /* As of Jul-2012, w32api headers say that dwItemData has DWORD type, but that's a bug: it should actually be ULONG_PTR, which is correct for 32-bit and 64-bit Windows alike. MSVC headers get it right; hopefully, MinGW headers will, too. */ eassert (STRINGP (wv->help)); info.dwItemData = (ULONG_PTR) XUNTAG (wv->help, Lisp_String); } if (wv->button_type == BUTTON_TYPE_RADIO) { /* CheckMenuRadioItem allows us to differentiate TOGGLE and RADIO items, but is not available on NT 3.51 and earlier. */ info.fMask |= MIIM_TYPE | MIIM_STATE; info.fType = MFT_RADIOCHECK | MFT_STRING; info.dwTypeData = out_string; info.fState = wv->selected ? MFS_CHECKED : MFS_UNCHECKED; } set_menu_item_info (menu, item != NULL ? (UINT_PTR) item : (UINT_PTR) wv->call_data, FALSE, &info); } } SAFE_FREE (); return return_value; }
static Lisp_Object simple_dialog_show (struct frame *f, Lisp_Object contents, Lisp_Object header) { int answer; UINT type; Lisp_Object lispy_answer = Qnil, temp = XCAR (contents); type = MB_YESNO; /* Since we only handle Yes/No dialogs, and we already checked is_simple_dialog, we don't need to worry about checking contents to see what type of dialog to use. */ /* Use Unicode if possible, so any language can be displayed. */ if (unicode_message_box) { WCHAR *text; const WCHAR *title; USE_SAFE_ALLOCA; if (STRINGP (temp)) { char *utf8_text = SSDATA (ENCODE_UTF_8 (temp)); /* Be pessimistic about the number of characters needed. Remember characters outside the BMP will take more than one utf16 word, so we cannot simply use the character length of temp. */ int utf8_len = strlen (utf8_text); text = SAFE_ALLOCA ((utf8_len + 1) * sizeof (WCHAR)); utf8to16 ((unsigned char *)utf8_text, utf8_len, text); } else { text = (WCHAR *)L""; } if (NILP (header)) { title = L"Question"; type |= MB_ICONQUESTION; } else { title = L"Information"; type |= MB_ICONINFORMATION; } answer = unicode_message_box (FRAME_W32_WINDOW (f), text, title, type); SAFE_FREE (); } else { const char *text, *title; /* Fall back on ANSI message box, but at least use system encoding so questions representable by the system codepage are encoded properly. */ if (STRINGP (temp)) text = SSDATA (ENCODE_SYSTEM (temp)); else text = ""; if (NILP (header)) { title = "Question"; type |= MB_ICONQUESTION; } else { title = "Information"; type |= MB_ICONINFORMATION; } answer = MessageBox (FRAME_W32_WINDOW (f), text, title, type); } if (answer == IDYES) lispy_answer = build_string ("Yes"); else if (answer == IDNO) lispy_answer = build_string ("No"); else Fsignal (Qquit, Qnil); for (temp = XCDR (contents); CONSP (temp); temp = XCDR (temp)) { Lisp_Object item, name, value; item = XCAR (temp); if (CONSP (item)) { name = XCAR (item); value = XCDR (item); } else { name = item; value = Qnil; } if (!NILP (Fstring_equal (name, lispy_answer))) { return value; } } Fsignal (Qquit, Qnil); return Qnil; }
inline void utf8_to_wchar_impl< 2 >( const std::string & in , std::wstring & out ) { utf8to16( in.begin() , in.end() , std::back_inserter(out) ); }
// // Supported commands are: // <dict.file> e <word> - find exact match, if nothing found, dump list // <dict.file> l <word> - dump list // <dict.file> n <offset> - dump next word list starting from <offset> // <dict.file> p <offset> - dump previous word list, ending at offset <offset> // <dict.file> x <offset> - dump article at address x // // Output format: // list result // "list" // <word>\t<short translation>\n // <word>\t<short translation>\n // ... // <starting_offset>\t<ending_offset>\n // // exact result // "match"\n // <translation> // int main(int argc, char* argv[]) { // Set console codepage to UTF8 on windows #ifdef _MSC_VER SetConsoleOutputCP(CP_UTF8); #endif // Expecting <exec name> <dictionary file> <word> if (argc < 4) { print_usage(); return ERR_INVALID_ARGUMENT; } char* command = (char*) argv[2]; // dictionary file name, fancy names aren't supported char* filename = (char*) argv[1]; // UTF 16 version of the search string (assuming input is UTF8) int search_len; uint16_t* searchUTF16 = utf8to16((uint8_t*) argv[3], search_len); // Open dictionary file f = fopen(filename, "rb"); if (!f) { printf("Failed to open file: %s", filename); return ERR_FILE_NOT_FOUND; } // Read header doread(&header, sizeof(header)); // check magic (PRSPDICT ascii) int MAGIC[8] = {0x50, 0x52, 0x53, 0x50, 0x44, 0x49, 0x43, 0x54}; for (int i = 0; i < 8; i++) { if (header.magic[i] != MAGIC[i]) { printf("Invalid file magic"); return ERR_INVALID_MAGIC; } } // Check dicitonary version if (header.version_lo != 0 || header.version_hi != 1) { printf("Unsupported dictionary version: %d.%d", header.version_hi, header.version_lo); return ERR_UNSUPPORTED_VERSION; } switch (command[0]) { case 'e': // e <word> - find exact match, if nothing found, dump list if (!find_exact_match(header.offset_radix, searchUTF16, search_len)) { find_best_match(searchUTF16, search_len); } break; // l <word> - dump list case 'l': find_best_match(searchUTF16, search_len); break; // n <offset> - dump next word list starting from <offset> case 'n': dump_word_list(atoi(argv[3]), NEXT); break; // p <offset> - dump previous word list, ending at offset <offset> case 'p': dump_word_list(atoi(argv[3]), PREV); break; // x <offset> - dump article at address x case 'x': dump_article(atoi(argv[3])); break; default: print_usage(); } fclose(f); return 0; }
/* alias ----- Will create a Macintosh HFS specific alias at the path pointed to by dest. The alias will point to the file located at target. The target must exist. The return value is zero upon success. If unsuccessful, a non-zero value will be returned and both errno and mac_errno should be consulted. */ int alias (const char *target, const char *dest) { char *rtarget; char *destparentdir, *destname; #ifdef HAVE_CORESERVICES char *targetsuffix; UniChar *udestname; FSRef *fstarget, *fsdest, *fsnewfile; FSCatalogInfo *fscdestinfo, *fsctargetinfo; FileInfo destinfo, targetinfo; AliasHandle aliasrec; u_int32_t infomap = 0; int resfrk; u_int8_t targetisdir = 0; #endif u_int8_t destisdir = 0; size_t len = 0; #ifndef HAVE_CORESERVICES /* this function is for creating macintosh aliases. if we don't have the CoreServices API it's impossible, so why fool ourselves, let's just return error now and save the cpu cycles for something else */ errno = EIO; /* returning a generic I/O error */ return errno; #endif /* As of this writing, this function is not complete. Things left to complete are to check the target to see if it has a custom icon (if so, copy the custom to the alias file so that they appear the same. Also, copy custom icons of bundles to the alias. Another very important thing left to complete is to retire the process of making the alias manually to a last resort if we cannot create the alias by sending an apple event to the finder. */ /* check for null parameters and return accordingly */ if (!target || !dest) return 0; #ifdef HAVE_CORESERVICES rtarget = (char *)malloc(PATH_MAX); if (!rtarget) return errno; /* ENOMEM */ if (!resolve_alias(target, rtarget) || mac_errno) return errno; /* convert the target path into an FSRef */ fstarget = (FSRef *)malloc(sizeof(FSRef)); if (!fstarget) return errno; /* ENOMEM */ mac_errno = FSPathMakeRef(rtarget, fstarget, &targetisdir); if (mac_errno) return 0; fsdest = (FSRef *)malloc(sizeof(FSRef)); if (!fsdest) return errno; /* ENOMEM */ /* convert the destination into an FSRef so that we can test if it exists and if it is a directory (in which we should save the new alias file inside the directory specified) */ mac_errno = FSPathMakeRef(dest, fsdest, &destisdir); #endif /* if the destination is not a directory, we'll disassemble the path we were given for the destination into a parent directory and a name */ if (!destisdir) { /* obtain the name of the destination file */ len = strlen(dest); destname = (char *)dest + len; while (destname > dest && *(destname - 1) != '/') destname--, len--; /* obtain the parent directory of the destination file */ destparentdir = (char *)malloc(len); snprintf(destparentdir, len ? len + 1 : 3, "%s", len ? dest : "./"); } else { /* the parent directory should be the destination we were passed */ destparentdir = (char *)dest; /* and the name should be the real name of the target */ destname = (char *)rtarget + strlen(rtarget); while (destname > rtarget && *(destname - 1) != '/') destname--; } #ifdef HAVE_CORESERVICES /* attempt to convert the parent directory into an FSRef */ mac_errno = FSPathMakeRef(destparentdir, fsdest, 0); if (mac_errno) return 0; /* infomap is an integer telling FSCreateResFile which settings from the FSCatalogInfo paramater you want to set */ infomap |= kFSCatInfoFinderInfo; /* if the target is not a directory, get its file type/creator */ if (!targetisdir) { fsctargetinfo = (FSCatalogInfo *)malloc(sizeof(FSCatalogInfo)); if (!fsctargetinfo) return errno; /* ENOMEM */ /* get the target files information */ mac_errno = FSGetCatalogInfo(fstarget, infomap, fsctargetinfo, 0,0,0); if (mac_errno) return 0; /* I don't know why Apple did not declare the structure member 'finderInfo' as an FileInfo type (it is an array of 16 single bytes). This makes it impossible to address elements of the finderInfo directly. So, we move it into an FileInfo data type we allocated */ memmove(&targetinfo, fsctargetinfo->finderInfo, sizeof(FileInfo)); /* this is not really necessary for the creation of the file, since a null type or creator will be transposed to '????', but should any functions try to display the type/creator, it is much friendlier to display '????' than an empty string (which hardcore mac people will understand better */ if (!targetinfo.fileType) { targetinfo.fileType = '\?\?\?\?'; targetinfo.fileCreator = '\?\?\?\?'; } } else { /* folders natively return null as their file type and creator but to make an alias appear as a folder, we use the following settings */ targetinfo.fileType = 'fldr'; targetinfo.fileCreator = 'MACS'; /* there is one exception, when the folder ends in .app it is an application package and should get the following settings */ targetsuffix = suffix(rtarget); if (!strcmp(targetsuffix, "app")) { targetinfo.fileType = 'fapa'; targetinfo.fileCreator = '\?\?\?\?'; *--targetsuffix = '\0'; /* this will cut off the .app from the name */ } } fscdestinfo = (FSCatalogInfo *)malloc(sizeof(FSCatalogInfo)); if (!fscdestinfo) return errno; /* ENOMEM */ /* set the file type, creator, and alias flag for the file */ destinfo.fileType = targetinfo.fileType; destinfo.fileCreator = targetinfo.fileCreator; destinfo.finderFlags = kIsAlias; memcpy(fscdestinfo->finderInfo, &destinfo, sizeof(FileInfo)); fsnewfile = (FSRef *)malloc(sizeof(FSRef)); if (!fsnewfile) return errno; /* ENOMEM */ udestname = (UniChar *)malloc(PATH_MAX); len = utf8to16(destname, udestname, PATH_MAX); if (len < 0) return errno; /* caller: consult both errno and mac_errno */ /* attempt to create the resource file (fails if it already exists) */ FSCreateResFile(fsdest, len, udestname, infomap, fscdestinfo, fsnewfile, 0); free(udestname); /* test for error creating the file */ mac_errno = ResError(); if (mac_errno) return 0; /* attempt to open the resource fork of the file now */ resfrk = FSOpenResFile(fsnewfile, fsRdWrPerm); /* test for errors */ mac_errno = ResError(); if (mac_errno) return 0; /* attempt to create an alias record to save in the resource file */ mac_errno = FSNewAlias(0, fstarget, &aliasrec); if (mac_errno) return CloseResFile(resfrk), 0; /* add the alias record to the resource fork */ AddResource((Handle)aliasrec, rAliasType, 0, 0); /* test for errors */ mac_errno = ResError(); if (mac_errno) return CloseResFile(resfrk), 0; /* write the resource fork data */ WriteResource((Handle)aliasrec); /* test for errors */ mac_errno = ResError(); if (mac_errno) return CloseResFile(resfrk), 0; /* clean up */ CloseResFile(resfrk); #endif /* HAVE_CORESERVICES */ /* return success */ return 0; }
int main(int argc, char* argv[]) { try { if (argc < 3) throw std::invalid_argument("usage:\nstep2 input_file_name start_position labels_file_name\n"); const auto data = Read(argv[1]); const auto pos = std::atoi(argv[2]) - 1; const auto nMax = data.front().size() == 5 ? 36 : data.front().size() == 6 ? 49 : 0; assert(nMax > 0); std::vector<int> label; if (argc < 4) { label.resize(12); label[0] = 1; label[1] = 2; for (size_t i = 2, sz = label.size(); i < sz; ++i) label[i] = label[i - 1] + label[i - 2]; } else { const auto rr = Reader<int>::Read(argv[3], false); for (const auto &r : rr) std::copy(r.cbegin(), r.cend(), std::back_inserter(label)); } const std::string fileName = "статистика второго шага.txt"; std::ofstream outp(utf8to16(fileName)); if (!outp) throw std::ios_base::failure(std::string("Cannot write to \"") + fileName + "\""); for (size_t labelCount = 7, sz = label.size(); labelCount <= sz; ++labelCount) { std::cout << labelCount << "..."; std::map<int, std::vector<size_t>> matchCounter; for (size_t currentPos = pos, sz = data.size(); currentPos < sz; ++currentPos) { Grouper<uint8_t>::GroupCounter groupCounter2; { Grouper<uint8_t>::GroupCounter groupCounter; for (auto it = label.cbegin(), end = std::next(label.cbegin(), labelCount); it != end; ++it) { const int pos = static_cast<int>(currentPos) - *it; if (pos >= 0 && pos < static_cast<int>(data.size())) Grouper<uint8_t>::Group(groupCounter, data[pos], 2, pos); } for (const auto &p : groupCounter) { if (p.second.size() < 2) continue; for (uint8_t i = p.first.front() > 1 ? p.first.front() - 1 : 1, in = p.first.front() < nMax ? p.first.front() + 1 : nMax; i < in; ++i) { for (uint8_t j = p.first.back() > 1 ? p.first.back() - 1 : 1, jn = p.first.back() < nMax ? p.first.back() + 1 : nMax; j < jn; ++j) { groupCounter2.emplace(Item({ i, j }), decltype(groupCounter2)::mapped_type()); } } } } Grouper<uint8_t>::GroupCounter currentGroupCounter; Grouper<uint8_t>::Group(currentGroupCounter, data[currentPos], 2, static_cast<int>(currentPos)); const auto matched = std::accumulate(currentGroupCounter.cbegin(), currentGroupCounter.cend(), 0, [&groupCounter2](int init, const decltype(currentGroupCounter)::value_type &item) {return init + (groupCounter2.find(item.first) != groupCounter2.end()); }); matchCounter[matched].push_back(currentPos + 1); } outp << labelCount << std::endl; for (auto it = matchCounter.crbegin(), end = matchCounter.crend(); it != end; ++it) { outp << it->first << ": "; std::copy(it->second.cbegin(), it->second.cend(), std::ostream_iterator<size_t>(outp, " ")); outp << std::endl; } outp << std::endl; outp.flush(); std::cout << " done" << std::endl; } } catch (const std::exception &ex) { std::cerr << ex.what() << std::endl; return 1; } return 0; }