Skip to content

Instantly share code, notes, and snippets.

@z4none
z4none / strutil.h
Last active April 20, 2022 09:52
[c++ string startswith / endswith]
//
bool starts_with(const std::string & str, const std::string & sub, bool ignore_case=false)
{
int str_len = str.size();
int sub_len = sub.size();
if (str_len < sub_len) return false;
if (ignore_case)
{
@z4none
z4none / setUrlParams.js
Created March 29, 2020 16:29
set url params
// set url params
// if params not exists in url, then add params
// if params exists in url, then replace
function setUrlParams(url, params) {
var parts = url.split("?");
var path = parts[0]
var old_params = {};
if (parts.length == 1);
else {
var items = parts[1].split("&");
@z4none
z4none / log_entry_admin.py
Created March 28, 2020 12:07
view django models.LogEntry in admin, disable create / edit / delete
class LogEntryAdmin(admin.ModelAdmin):
list_display = ('action_time', 'user', 'object_repr', 'action_flag')
list_per_page = 20
list_display_links = None
search_fields = ['=user__username', ]
fieldsets = [(None, {'fields': ()}), ]
def has_add_permission(self, request):
return False
@z4none
z4none / ifregex.py
Created March 28, 2020 07:01
django if regex tag
class IfRegexNode(Node):
def __init__(self, var1, var2, nodelist_true, nodelist_false, negate):
self.var1, self.var2 = Variable(var1), Variable(var2)
self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false
self.negate = negate
def __repr__(self):
return "<IfRegexNode>"
def render(self, context):
@z4none
z4none / main.cpp
Created January 6, 2020 15:28
MFC conversion
USES_CONVERSION;
A2CT(str);
CT2A(str);
@z4none
z4none / main.cpp
Created January 6, 2020 08:11
MFC enable dialog controls
void EnableDlgItem (CWnd *dlg, int items[], BOOL bEnable)
{
int i = 0, item ;
while ((item = items[i++]) != 0)
{
CWnd *pControl = dlg->GetDlgItem(item) ;
if (pControl != NULL)
pControl->EnableWindow(bEnable) ;
}
@z4none
z4none / .npmrc
Created September 4, 2019 14:41
speedup npm install in china
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
phantomjs_cdnurl=https://npm.taobao.org/mirrors/phantomjs/
electron_mirror=https://npm.taobao.org/mirrors/electron/
registry=https://registry.npm.taobao.org
@z4none
z4none / auth.py
Created September 4, 2019 02:22
django rest framework token
import datetime
from django.conf import settings
from rest_framework.authentication import TokenAuthentication
from rest_framework import exceptions
from django.utils.translation import ugettext_lazy as _
from django.core.cache import cache
EXPIRE_MINUTES = getattr(settings, 'REST_FRAMEWORK_TOKEN_EXPIRE_MINUTES', 1)
@z4none
z4none / microtime.c
Created June 11, 2019 14:41
Get the current time in microseconds in C.
#include <sys/time.h>
/**
* Returns the current time in microseconds.
*/
long getMicrotime(){
struct timeval currentTime;
gettimeofday(&currentTime, NULL);
return currentTime.tv_sec * (int)1e6 + currentTime.tv_usec;
}
int width = m_view_size.width();
int height = m_view_size.height();
uint8_t * data = new uint8_t[width * height * 4];
m_gles2->ReadPixels(m_context->pp_resource(), 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
cv::Mat rgbFrame, outputFrame;
cv::Mat glFrame = cv::Mat(height, width, CV_8UC4, data);
cv::cvtColor(glFrame, rgbFrame, CV_BGRA2RGB);
cv::flip(rgbFrame, outputFrame, 0);