Borbin the 🐱

  • Replace substring in substrings with Regular expressions (regex)

    📅 21. April 2019 · Software

    A regular expression defines a search pattern for strings. Replace is possible for text matches, but repeated substring replace in substrings is difficult. Especially if the text you want to replace is also outside of the initial substrings. Using cascaded match evaluators, this replacements gets very easy. For example to replace quoted text withing square brackets.
    Please note the middle text "text" in this example which must not be replaced.

    ["text1"] "text" ["text2"]
    Begin[Pre"1txet"Post]End "text" Begin[Pre"2txet"Post]End

    First the text is splitted by the primary substrings defined by square brackets.
    Then these substrings are recomposed according to the business logic. In this example the string gets enclosed in a "Pre" / "Post" and then reverted. The primary substrings gets enclosed in "Begin" / "End".

    string inputText = "[\"text1\"] "text" [\"text2\"]";
    
    string replacedText = Regex.Replace(
        inputText,
        $"(\\[)(.*?)(\\])",
        arrayMatch =>
        {
            return 
            $"Begin" +
            arrayMatch.Groups[1].Value +
            Regex.Replace(
                arrayMatch.Groups[2].Value,
                $"(\".*?\")+",
                contentMatch =>
                {
                    return $"Pre" + new string(contentMatch.Groups[1].Value.Reverse().ToArray()) + "Post";
                }) +
    
            arrayMatch.Groups[3].Value +
            $"End";
        });


    Or modify a text section to add a link from a markdown link to the following picture:

    from:
      [Interactive Panorama title](link.htm)
      ![](bild.jpg)

    to:
      [Interactive Panorama title](link.htm)
      ![](bild.jpg)


    Using PowerShell:

        [string]$content = [System.IO.File]::ReadAllText($FileName)
    
        # using '(?s)', the dot '.' matches the newline and allows for multiline replace 
        [string]$StringsRegex = "(?s)(?<section>\[Interactive Panorama.*?\]\((?<link>.*?)\).*?)(?<bild>\!\[\]\(.*?\))"
    
        $updatedContent = $content -replace $StringsRegex, {
            $match = $_
            $section = $match.Groups["section"].Value
            $link = $match.Groups["link"].Value
            $bild = $match.Groups["bild"].Value
    
            # already replaced?
            if(-not $section.Contains("<"))
            {    
                # insert link
                # '$section$bild' -> 'section<a href="$link">$bild</a>'
                "$section<a href=""$link"">$bild</a>
            }
        }
    
        if($updated)
        {
            [string]$outFile = FileName + ".updated"
            [IO.File]::WriteAllText($outFile, $updatedContent)
        }
  • use TLS1.2

    📅 24. Februar 2018 · Software

    If you found that your code to download from a website started failing with an error like:

    The request was aborted: Could not create SSL/TLS secure channel.

    or

    Ausnahme beim Aufrufen von "DownloadFile" mit 2 Argument(en):  "Die Anfrage wurde abgebrochen: Es konnte kein 
    geschützter SSL/TLS-Kanal erstellt werden."
    + $webclient.DownloadFile($file, $toZipFile)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : WebException

    then your webserver security is updated and now requires TLS1.2.
    Tls is a security protocol to transfer data over the Internet. To solve this issue, simply set the security protocol to Tls1.2.

    For example in PowerShell:

    $webclient = New-Object System.Net.WebClient
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    $webclient.DownloadFile($file, $toZipFile)
  • Encrypt your data

    📅 30. Januar 2014 · Software

    Sending data over the Internet is not safe. A safe connections takes effort and resources.
    If you manage to get your mail send via encryption, you forgot the National Security Agencies (in short NSA) that have access to the mail servers and scan your data before its gets send encrypted over the Internet. Move your data up to an online storage leaves them in a container that is not yours alone.

    But if you encrypt and decrypt data on your computer first with a strong 256bit encryption key, you can leave any security agency in the dark.
    These Windows Powershell script functions makes it easy to encrypt and decrypt your files.
    Since it is a symmetrical cipher, the same password is used for encryption and decryption.
    If you need to pass your large secure data to another receiver, pass on the script with the new secret password and sentences using an unsymmetrical cipher or an alternative transport (mail / USB stick). The reward is a secure data transmission.

    For example encrypt a file with the script:

    Encrypt-File "C:\data\mydata.zip"

    Send this encrypted file to an online storage or send via mail.
    Receive the file and decrypt with the same script password:

    Decrypt-File "C:\download\mydata.zip"


    If you intent to encrypt/decrypt pictures, you can use the script plugins in cPicture.

    Download

    Encrypt-Decrypt Script
    (remove the trailing .txt from the file once it is downloaded)

    Note

    Change the secret phrases and password in the script.
    Select the script function you want to use at the bottom of the script or add the script to an existing upload/download process.

  • 'Encountered an improper argument.'

    📅 11. März 2013 · Software

    Ever run into this error message?

    The error message 'Encountered an improper argument.' is from an uncaught exception in WindowProc.

    In:

    LRESULT CWinApp::ProcessWndProcException(CException* e, const MSG* pMsg)
        e->ReportError(MB_ICONEXCLAMATION|MB_SYSTEMMODAL, nIDP);

    the error message is nIDP = AFX_IDP_INTERNAL_FAILURE:

    #define AFX_IDP_INTERNAL_FAILURE        0xF108      // general failure

    Usually you don't end up here. But there are cases where it happens. I have one example which was difficult to debug: An existing MFC based app upgraded to a ribbon control using a release build with static linking. Dynamic linked release builds and all debug builds are not affected.


    It all starts at the InitInstance()of your app when you load the main frame:

    pFrame->LoadFrame(IDR_MAINFRAME,
      WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
      NULL);

    Then all the way down the call stack:

    BOOL CFrameWndEx::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle, CWnd* pParentWnd, CCreateContext* pContext)
    
    
    BOOL CFrameWnd::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle,
      CWnd* pParentWnd, CCreateContext* pContext)
    
    BOOL CFrameWnd::Create(LPCTSTR lpszClassName,
      LPCTSTR lpszWindowName,
      DWORD dwStyle,
      const RECT& rect,
      CWnd* pParentWnd,
      LPCTSTR lpszMenuName,
      DWORD dwExStyle,
      CCreateContext* pContext)
    
    BOOL CWnd::CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName,
      LPCTSTR lpszWindowName, DWORD dwStyle,
      int x, int y, int nWidth, int nHeight,
      HWND hWndParent, HMENU nIDorHMenu, LPVOID lpParam)

    until you reach:

    ISOLATION_AWARE_INLINE HWND IsolationAwarePrivatenCv IsolationAwareCreateWindowExW(_In_ DWORD dwExStyle,_In_opt_ LPCWSTR lpClassName,_In_opt_ LPCWSTR lpWindowName,_In_ DWORD dwStyle,_In_ int X,_In_ int Y,_In_ int nWidth,_In_ int nHeight,_In_opt_ HWND hWndParent,_In_opt_ HMENU hMenu,_In_opt_ HINSTANCE hInstance,_In_opt_ LPVOID lpParam)

    At this point you suddenly end up in the exception handler of the WndProc:

    LRESULT AFXAPI AfxCallWndProc(CWnd* pWnd, HWND hWnd, UINT nMsg,
      WPARAM wParam = 0, LPARAM lParam = 0)

    All class names, handles and sizes look alright. The last message is 5, which is a WM_SIZE message with the right lParam value. Usually you get a debug break and know why the exception was raised, but here it seemed a mystery. But a quick compare to a sample app from the templates revealed the solution: The ribbon required additional resources in order to run in a static release build in the .rc file:

    #if !defined(_AFXDLL)
      #include "l.DEU\afxprint.rc"         // Ressourcen für Drucken/Seitenansicht
      #include "l.DEU\afxribbon.rc"        // Ressourcen für MFC-Menüband und -Steuerleiste
    #endif

    (Remove the language prefix 'l.DEU\' with your language code if you are building another language.)

  • Web page with inline image

    📅 7. März 2013 · Software

    To add an image to a web page you use the img tag:

    <img src="your-image.png" alt="your-image" />


    But you need to use a binary file.

    If you work on an embedded system, a html control with restrictions or simply need one plain html file, you can use images inline. The picture data will be encoded with text characters (base64) and added as a class:

    <style type="text/css">
        .my-picture
        {
            background-image: url(data:image/png;base64, BASE64 DATA );
            height: 24px;
            width: 24px;
            display: block;
        }
    </style>

    Later on, you reference the picture class to display the image:

    <div class="my-picture"></div>



    sample

    Here is a complete HTML sample:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>base64 image/png</title>
        <style type="text/css">
            .back-button
            {
                background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAADb0lEQVRIx52WT2hcVRTGf+fORJMgbYVEWv9uhEBaEpoMEem8DEKtm0qrWLCLItJFFyJIG5CIC12ni3ZhVRCKUEqJ1EVduNHUlzupm5k4Bptu1FUZwRhNTTMhNe9+LuYlDclMk/Rs3uXdc7/z3nfOd841mliUzz+GWbdBL3AQ6BTsAszgDjADjAkqSDd9sfhvIxxb/2IwiloEXcDbVn/eBaaAKjCXuu0E9gA9wA5Jv2F2EbjlvV9qGqBQKLgQwiGDYeAPpMsym3TOVeM4Dg18d5vUCxyX2fPAWZck1+IbN5Y3BCgUCi4kyRBwFLjki8ULbMOifP4tMzsl6bqTPoonJv4DcCu0hBAOAUcx+8zgS7ZrZqPAOTN7KTh3JIqi1tUAgq6UlksmXR0vFhe2i++9XxR8g/QpMATsBbC0Wj42eGrc+zcbHR4YGDAzywA2Pz+/PD09rQcFG8znvxDcw+wDl5ZiF9LlZgdCCC8mSTKcJMlJILPZ38jsipk9B/S6tM7vymyykXMul3saOAecAGpAsilf0hQwa9CXTUX0s3Ouug44A+SBkTRX79Rqte82owfAZTIzCmESeDkLdAIb6jwF/xB4RtIZ4FZbW1tHf39/M9xauVxeAIjjWINRVBV0ZAW77L5C19IyAuwHZszstVQfVi+6VQ2tXY8CX62hac7MdjnA6b5juqdW4JF1irdN1hsyAVjW4J+0t6xauVz+NZfLnaGujb2SvgbGgXsPoH5hnfB2CuaywF/Ak4VCwa3Lww/AMjBiZqeBP2u12vdbSXJqewxmHTAG9IQQdq/dLZVKSalUioHXgQBcaG9vP9Hd3W1b6EsdQB8w5gQVYEfaFTdYqVS6DZwGrgJPtLa2tmyhL+1L50c5i3RT8LvBceDbxv42IakkKZPNZpe3QM8xSVUzqxhAFEX7gU+QPsds1Hu/yENYFEWPmnRYMIzZu977H126Nw2cNbNTBq9GUdT2UODwCvWCOG8hVFbbtfd+ySXJNUnXgfdMemPb40A6DLwP/CQYHZ+YWNw4Mg8caAnOHQGGTPpFZleQplwmMxPHsRpWSz2hx5BeAM4LRovF4lLToZ9Oon1IJ83sWWAWmARuI91ZEVE69PuATklVzC5aCJWVL28aYE2gx4Eeq4McFHRYem0RzFk98JigbFAZ9/7vRjj/A6BAc6F41EmIAAAAAElFTkSuQmCC);
                height: 24px;
                width: 24px;
                display: block;
            }
        </style>
    </head>
    <body>
        <p>This is a back button</p>
        <div class="back-button"></div>
    </body>
    </html>

    To convert between picture files and base64 data, you can use these PowerShell lines

    From a file (for example 'p24.png') to base64 data:

    $bin = Get-Content p24.png -Encoding Byte
    $str = [System.Convert]::ToBase64String($bin)

    From base64 data to a file (for example 'p24.png')

    $str = "your BASE64 DATA"
    $bin = [System.Convert]::FromBase64String($str)
    Set-Content p24.png -Encoding Byte -Value $bin

    Simple as that, but keep the image size small.

← Neuere Beiträge Seite 4 von 6 Ältere Beiträge →
ÜBER

Jürgen E
Principal Engineer, Villager, and the creative mind behind lots of projects:
Windows Photo Explorer (cpicture-blog), Android apps AI code rpn calculator and Stockroom, vrlight, 3DRoundview and my github


Blog-Übersicht Chronologisch

KATEGORIEN

Auto • Fotografie • Motorrad • Paintings • Panorama • Software • Querbeet


Erstellt mit BitBlog!