utils.cpp

来自「Shorthand是一个强大的脚本语言」· C++ 代码 · 共 136 行

CPP
136
字号
///////////////////////////////////////////////////////////////////////////////
// $Header: /shorthand/src/utils.cpp 5     1/09/03 7:14p Arm $
//-----------------------------------------------------------------------------
// Project: ShortHand interpreter
// Author: Andrei Remenchuk <andrei@remenchuk.com>
//-----------------------------------------------------------------------------
// utils.cpp: miscellaneous utility functions
///////////////////////////////////////////////////////////////////////////////
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <errno.h>
#ifdef WIN32
#include <windows.h>
#include <malloc.h>
#endif

#include "utils.h"
#include "cstring.h"
#include "shorthand.h"

#undef putenv

int i_max(int a, int b) { return a > b ? a : b; }
int i_min(int a, int b) { return a < b ? a : b; }


static char* home_dir = NULL;

const char* get_home_dir()
{
    if (home_dir != NULL) return home_dir;


#ifdef WIN32
    char* path = "c:/";
    home_dir = strdup(path);
#else
    home_dir = "/tmp/";
#endif
    return home_dir;
}



/**
 * Java-style hash function.
 */
unsigned int hash_code(const char* s)
{
    if (s == NULL || *s == '\0') return 0;
    int n = strlen(s)-1;
    int i = 0;
    int hash = 0;
    while(s[i]) {
        hash += s[i]*(31^(n-1));
        n--;
        i++;
    }
    return hash;
}

const char* sh_getenv(const char* name)
{
    const char* v = getenv(name);
    if (v == NULL) v = "";
    return v;
}

void putenf(const char* name, const char* format, ...)
{
    va_list args; va_start(args, format);
    int nlen = strlen(name);
    int w = vawidth(format, &args) + nlen + 2;
    char* b = (char*) malloc(w);
    memcpy(b, name, nlen);
    b[nlen] = '=';
    vsprintf(b+nlen+1, format, args);
    safe_putenv(b);
}

const char* safe_getenv(const char* name, string& value)
{
#ifdef WIN32
    char* buffer = (char*) alloca(128);
    int l = GetEnvironmentVariable(name, buffer, 128);
    if (l > 128)
    {
        buffer = (char*) alloca(l+1);
        l = GetEnvironmentVariable(name, buffer, l);
    }
    if (l == 0) value = "";
    else value = buffer;
#else
    value = getenv(name);
#endif
    return value.cstr();
}

void safe_putenv(const char* spec)
{
#ifdef WIN32
    /*
        C runtime-library function putenv() on Windows contains bug.
        When you use it from DLL and try to replace existing variables, 
        it tries to free something using free() which results in GPF in most cases.
        SetEnvironment() from Win32 API works fine. 
    */
    if (spec == NULL) return;
    char* copy = (char*) alloca(strlen(spec)+1);
    char* name, *value;
    strcpy(copy, spec);
    char* eq = strchr(copy, '=');
    if (eq == NULL)
    {
        name = copy;
        value = "";
    }
    else
    {
        *eq = '\0';
        name = copy;
        value = eq+1;
    }
    TRACE((3, "SetEnvironmentVariable: %s = %s\n", name, value));
    SetEnvironmentVariable(name, value);


#else
    ::putenv(strdup(spec));
#endif
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?