📄 misc.c
字号:
free (petc->sections); free (petc); return 0;}int GUIAPI GetValueFromEtc (GHANDLE hEtc, const char* pSection, const char* pKey, char* pValue, int iLen){ int i; ETC_S *petc = (ETC_S*) hEtc; PETCSECTION psect; if (!petc || !pValue) return -1; for (i=0; i<petc->section_nr; i++) { psect = petc->sections + i; if (!psect) continue; if (strcmp (psect->name, pSection) == 0) { break; } } if (i >= petc->section_nr) return ETC_SECTIONNOTFOUND; for (i=0; i<psect->key_nr; i++) { if (strcmp (psect->keys[i], pKey) == 0) { break; } } if (i >= psect->key_nr) return ETC_KEYNOTFOUND; if (iLen > 0) { /* get value */ strncpy (pValue, psect->values[i], iLen); } else { /* set value */ free (psect->values[i]); psect->values[i] = strdup(pValue); } return ETC_OK;}int GUIAPI GetIntValueFromEtc (GHANDLE hEtc, const char* pSection, const char* pKey, int* value){ int ret; char szBuff [51]; ret = GetValueFromEtc (hEtc, pSection, pKey, szBuff, 50); if (ret < 0) { return ret; } *value = strtol (szBuff, NULL, 0); if ((*value == LONG_MIN || *value == LONG_MAX) && errno == ERANGE) return ETC_INTCONV; return ETC_OK;}/* Function: GetValueFromEtcFile(const char* pEtcFile, const char* pSection, * const char* pKey, char* pValue, int iLen); * Parameter: * pEtcFile: etc file path name. * pSection: Section name. * pKey: Key name. * pValue: The buffer will store the value of the key. * iLen: The max length of value string. * Return: * int meaning * ETC_FILENOTFOUND The etc file not found. * ETC_SECTIONNOTFOUND The section is not found. * ETC_EKYNOTFOUND The Key is not found. * ETC_OK OK. */int GUIAPI GetValueFromEtcFile(const char* pEtcFile, const char* pSection, const char* pKey, char* pValue, int iLen){ FILE* fp; char tempSection [ETC_MAXLINE + 2]; if (!(fp = fopen(pEtcFile, "r"))) return ETC_FILENOTFOUND; if (pSection) if (etc_LocateSection (fp, pSection, NULL) != ETC_OK) { fclose (fp); return ETC_SECTIONNOTFOUND; } if (etc_LocateKeyValue (fp, pKey, pSection != NULL, pValue, iLen, NULL, tempSection) != ETC_OK) { fclose (fp); return ETC_KEYNOTFOUND; } fclose (fp); return ETC_OK;}/* Function: GetIntValueFromEtcFile(const char* pEtcFile, const char* pSection, * const char* pKey); * Parameter: * pEtcFile: etc file path name. * pSection: Section name. * pKey: Key name. * Return: * int meaning * ETC_FILENOTFOUND The etc file not found. * ETC_SECTIONNOTFOUND The section is not found. * ETC_EKYNOTFOUND The Key is not found. * ETC_OK OK. */int GUIAPI GetIntValueFromEtcFile(const char* pEtcFile, const char* pSection, const char* pKey, int* value){ int ret; char szBuff [51]; ret = GetValueFromEtcFile (pEtcFile, pSection, pKey, szBuff, 50); if (ret < 0) return ret; *value = strtol (szBuff, NULL, 0); if ((*value == LONG_MIN || *value == LONG_MAX) && errno == ERANGE) return ETC_INTCONV; return ETC_OK;}static int etc_CopyAndLocate (FILE* etc_fp, FILE* tmp_fp, const char* pSection, const char* pKey, char* tempSection){ if (pSection && etc_LocateSection (etc_fp, pSection, tmp_fp) != ETC_OK) return ETC_SECTIONNOTFOUND; if (etc_LocateKeyValue (etc_fp, pKey, pSection != NULL, NULL, 0, tmp_fp, tempSection) != ETC_OK) return ETC_KEYNOTFOUND; return ETC_OK;}static int etc_FileCopy (FILE* sf, FILE* df){ char line [ETC_MAXLINE + 1]; while (fgets (line, ETC_MAXLINE + 1, sf) != NULL) if (fputs (line, df) == EOF) { return ETC_FILEIOFAILED; } return ETC_OK;}/* Function: SetValueToEtcFile(const char* pEtcFile, const char* pSection, * const char* pKey, char* pValue); * Parameter: * pEtcFile: etc file path name. * pSection: Section name. * pKey: Key name. * pValue: Value. * Return: * int meaning * ETC_FILENOTFOUND The etc file not found. * ETC_TMPFILEFAILED Create tmp file failure. * ETC_OK OK. */int GUIAPI SetValueToEtcFile (const char* pEtcFile, const char* pSection, const char* pKey, char* pValue){ FILE* etc_fp; FILE* tmp_fp; int rc; char tempSection [ETC_MAXLINE + 2];#ifndef HAVE_TMPFILE char tmp_nam [256]; sprintf (tmp_nam, "/tmp/mg-etc-tmp-%x", time(NULL)); if ((tmp_fp = fopen (tmp_nam, "w+")) == NULL) return ETC_TMPFILEFAILED;#else if ((tmp_fp = tmpfile ()) == NULL) return ETC_TMPFILEFAILED;#endif if (!(etc_fp = fopen (pEtcFile, "r+"))) { fclose (tmp_fp);#ifndef HAVE_TMPFILE unlink (tmp_nam);#endif if (!(etc_fp = fopen (pEtcFile, "w"))) { return ETC_FILEIOFAILED; } fprintf (etc_fp, "[%s]\n", pSection); fprintf (etc_fp, "%s=%s\n", pKey, pValue); fclose (etc_fp); return ETC_OK; } switch (etc_CopyAndLocate (etc_fp, tmp_fp, pSection, pKey, tempSection)) { case ETC_SECTIONNOTFOUND: fprintf (tmp_fp, "\n[%s]\n", pSection); fprintf (tmp_fp, "%s=%s\n", pKey, pValue); break; case ETC_KEYNOTFOUND: fprintf (tmp_fp, "%s=%s\n\n", pKey, pValue); fprintf (tmp_fp, "%s\n", tempSection); break; default: fprintf (tmp_fp, "%s=%s\n", pKey, pValue); break; } if ((rc = etc_FileCopy (etc_fp, tmp_fp)) != ETC_OK) goto error; // replace etc content with tmp file content // truncate etc content first fclose (etc_fp); if (!(etc_fp = fopen (pEtcFile, "w"))) { fclose (tmp_fp);#ifndef HAVE_TMPFILE unlink (tmp_nam);#endif return ETC_FILEIOFAILED; } rewind (tmp_fp); rc = etc_FileCopy (tmp_fp, etc_fp);error: fclose (etc_fp); fclose (tmp_fp);#ifndef HAVE_TMPFILE unlink (tmp_nam);#endif return rc;}/****************************** Ping and Beep *********************************/void GUIAPI Ping(void){ putchar ('\a'); fflush (stdout);}#ifndef __NOLINUX__#include <linux/kd.h>#include <asm/param.h>void GUIAPI Tone (int frequency_hz, int duration_ms){ /* FIXME: Tone will not work in X Window */ long argument = (1190000 / frequency_hz) | ((duration_ms / (1000/HZ)) << 16); ioctl (0, KDMKTONE, (long) argument);}#endif/****************************** Drawing Helpers *******************************/void GUIAPI Draw3DUpFrame(HDC hDC, int l, int t, int r, int b, gal_pixel fillc){ r--; b--; SetPenColor(hDC, GetWindowElementColor (WEC_3DFRAME_RIGHT_INNER)); Rectangle(hDC, l, t, r, b); SetPenColor(hDC, GetWindowElementColor (WEC_3DFRAME_LEFT_INNER)); Rectangle(hDC, l, t, r - 1, b - 1); SetPenColor(hDC, GetWindowElementColor (WEC_3DFRAME_LEFT_OUTER)); MoveTo(hDC, l, b); LineTo(hDC, r, b); LineTo(hDC, r, t); SetPenColor(hDC, GetWindowElementColor (WEC_3DFRAME_RIGHT_OUTER)); MoveTo(hDC, l + 1, b - 1); LineTo(hDC, l + 1, t + 1); LineTo(hDC, r - 1, t + 1); if (fillc != 0) { SetBrushColor(hDC, fillc); FillBox(hDC, l + 2, t + 2, r - l - 3, b - t - 3); }}void GUIAPI Draw3DDownFrame(HDC hDC, int l, int t, int r, int b, gal_pixel fillc){ SetPenColor(hDC, GetWindowElementColor (WEC_3DFRAME_LEFT_OUTER)); MoveTo(hDC, l, b); LineTo(hDC, l, t); LineTo(hDC, r, t); SetPenColor(hDC, GetWindowElementColor (WEC_3DFRAME_LEFT_INNER)); MoveTo(hDC, l + 1, b - 1); LineTo(hDC, l + 1, t + 1); LineTo(hDC, r - 1, t + 1); SetPenColor(hDC, GetWindowElementColor (WEC_3DFRAME_RIGHT_INNER)); MoveTo(hDC, l + 1, b - 1); LineTo(hDC, r - 1, b - 1); LineTo(hDC, r - 1, t + 1); SetPenColor(hDC, GetWindowElementColor (WEC_3DFRAME_RIGHT_OUTER)); MoveTo(hDC, l, b); LineTo(hDC, r, b); LineTo(hDC, r, t); if (fillc != 0) { SetBrushColor(hDC, fillc); FillBox(hDC, l + 2, t + 2, r - l - 3, b - t - 3); }}void GUIAPI Draw3DUpThinFrame(HDC hDC, int l, int t, int r, int b, gal_pixel fillc){ SetPenColor(hDC, GetWindowElementColor (WEC_3DFRAME_RIGHT)); MoveTo(hDC, l, b); LineTo(hDC, r, b); LineTo(hDC, r, t); SetPenColor(hDC, GetWindowElementColor (WEC_3DFRAME_LEFT)); MoveTo(hDC, l, b); LineTo(hDC, l, t); LineTo(hDC, r, t); if (fillc != 0) { SetBrushColor(hDC, fillc); FillBox(hDC, l + 1, t + 1, r - l - 2, b - t - 2); }}void GUIAPI Draw3DDownThinFrame (HDC hDC, int l, int t, int r, int b, gal_pixel fillc){ SetPenColor(hDC, GetWindowElementColor (WEC_3DFRAME_LEFT)); MoveTo(hDC, l, b); LineTo(hDC, r, b); LineTo(hDC, r, t); SetPenColor(hDC, GetWindowElementColor (WEC_3DFRAME_RIGHT)); MoveTo(hDC, l, b); LineTo(hDC, l, t); LineTo(hDC, r, t); if (fillc != 0) { SetBrushColor(hDC, fillc); FillBox(hDC, l + 1, t + 1, r - l - 2, b - t - 2); }}void GUIAPI Draw3DBorder (HDC hdc, int l, int t, int r, int b){ SetPenColor(hdc, GetWindowElementColor (WEC_3DFRAME_LEFT)); Rectangle (hdc, l + 1, t + 1, r - 1, b - 1); SetPenColor(hdc, GetWindowElementColor (WEC_3DFRAME_RIGHT)); Rectangle (hdc, l, t, r - 2, b - 2);}void GUIAPI DisabledTextOut (HDC hDC, int x, int y, const char* szText){ SetBkMode (hDC, BM_TRANSPARENT); SetTextColor (hDC, GetWindowElementColor (WEC_3DFRAME_LEFT)); TextOut (hDC, x + 1, y + 1, szText); SetTextColor (hDC, GetWindowElementColor (WEC_3DFRAME_RIGHT)); TextOut (hDC, x, y, szText);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -