PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PEBLUtility.h
Go to the documentation of this file.
1//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3// Name: utility/PEBLUtility.h
4// Purpose: Miscellaneous Utility Functions used in PEBL
5// Author: Shane T. Mueller, Ph.D.
6// Copyright: (c) 2003-2026 Shane T. Mueller <smueller@obereed.net>
7// License: GPL 2
8//
9//
10//
11// This file is part of the PEBL project.
12//
13// PEBL is free software; you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation; either version 2 of the License, or
16// (at your option) any later version.
17//
18// PEBL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with PEBL; if not, write to the Free Software
25// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27#ifndef __PEBLUTILITY_H__
28#define __PEBLUTILITY_H__
29
34
35#include "../devices/PKeyboard.h"
36//#include "../apps/Globals.h"
37#include "Defs.h"
38#include <string>
39
40
41#define JSMN_HEADER
42#include "jsmn.h"
43
44
45#ifdef PEBL_WINDOWS
46#include <windows.h>
47typedef struct PROCESS_INFORMATION;
48
49#endif
50
51class Variant;
52enum PEBLVideoMode: unsigned int;
53enum PEBLVideoDepth: unsigned int;
54namespace PEBLUtility
55{
56
57
58 std::string ToUpper(const std::string & text);
59 std::string ToLower(const std::string & text);
60
61
64 pDouble StringToPDouble(const char * mystring);
65
66 pDouble Log2(pDouble val);
67 pInt Round(pDouble val);
68 pDouble Round(pDouble val, pInt prec);
70
71 Variant Tokenize(const char* line, char separator);
72
73
74 PEBL_Keycode TranslateString(const std::string & letters);
75 // PEBLKey TranslateStringOLD(const std::string & let)
76
77 // std::string TranslateKeyCode(const PEBLKey key, int modkeys);
78 std::string TranslateKeycode(const PEBL_Keycode key, int modkeys);
79
80 // Pre-initialize keycode lookup tables to avoid lazy-init delay on first keypress
81 // Call this during environment initialization to prevent timing measurement artifacts
83
84
87
90
91 std::string ShiftSwitch(int modkeys, std::string lower, std::string upper);
92
93 PEBLVideoMode GetVideoMode(std::string modeline);
94 PEBLVideoDepth GetVideoDepth(std::string depthline);
95
96
97 Variant GetDirectoryListing(std::string path);
98 Variant IsDirectory(std::string path);
99 Variant FileExists(std::string path);
100 Variant MakeDirectory(std::string path);
101 Variant DeleteMyFile(std::string path);
104 Variant SetWorkingDirectory(std::string path);
105
106 const std::string StripFile(const std::string & file);
107 const std::string GetBaseFileName(const std::string & file);
108 Variant LaunchFile(std::string file);
109 Variant SystemCall(std::string path, std::string args);
110#ifdef PEBL_WINDOWS
111 PROCESS_INFORMATION SystemCallAndReturn(std::string path, std::string args);
112#endif
113 bool is_utf8(const std::string str);
114 void strrev(char*p);
115 void strrev_utf8(char*p);
116 std::string strrev(std::string p);
117 std::string strrev_utf8(std::string p);
118
119
120 std::string MD5File(const std::string & filename);
121 std::string MD5String(const std::string & text);
122
123 void CopyToClipboard(const std::string & text);
124 Variant ExtractJSONObject(const std::string & text,
125 int remaining,
126 jsmntok_t **t, int start, int end);
127
128 Variant ParseJSON(const std::string &text);
129
130 // Unicode script detection functions
131 // Returns ISO 15924 4-letter script code (e.g., "Arab", "Hebr", "Hani", "Thai")
132 // Returns NULL/empty string for Latin/unknown scripts
133 std::string DetectScript(const std::string & text);
134
135 // Returns true if the script code is right-to-left (Arabic, Hebrew)
136 bool IsRTLScript(const std::string & script);
137
138 // Font selection based on language code (2-letter ISO 639-1) or script code (4-letter ISO 15924)
139 // Returns appropriate font filename for the given language/script
140 // fontType: 0=sans-serif, 1=monospace, 2=serif
141 std::string GetFontForLanguageOrScript(const std::string & code, int fontType);
142
143 // Property chain resolution for nested property access (e.g., obj.prop1.prop2)
144 // Recursively traverses the property chain and returns the final value
145 Variant ResolvePropertyChain(Variant obj, const std::string& propertyChain);
146
147 // Property chain assignment for nested property access (e.g., obj.prop1.prop2 <- value)
148 // Recursively traverses to the parent object and sets the final property
149 void SetPropertyChain(Variant obj, const std::string& propertyChain, Variant value);
150
151
152}
153
154
155#endif
#define pInt
Definition Defs.h:8
#define pDouble
Definition Defs.h:7
PEBLVideoDepth
Definition Globals.h:69
PEBLVideoMode
Definition Globals.h:58
PEBL_Keycode
Definition PKeyboard.h:78
Variant SystemCall(std::string path, std::string args)
pDouble StringToPDouble(const char *mystring)
Variant ResolvePropertyChain(Variant obj, const std::string &propertyChain)
Variant LaunchFile(std::string file)
void strrev(char *p)
Variant GetHomeDirectory()
PEBL_Keycode TranslateString(const std::string &letters)
PEBLVideoDepth GetVideoDepth(std::string depthline)
const std::string GetBaseFileName(const std::string &file)
bool is_utf8(const std::string str)
pDouble Log2(pDouble val)
void SetPropertyChain(Variant obj, const std::string &propertyChain, Variant value)
pDouble RandomUniform()
pDouble RandomNormal()
const std::string StripFile(const std::string &file)
std::string TranslateKeycode(const PEBL_Keycode key, int modkeys)
void InitializeKeycodeLookups()
std::string ToUpper(const std::string &text)
Variant SetWorkingDirectory(std::string path)
void strrev_utf8(char *p)
std::string DetectScript(const std::string &text)
Variant DeleteMyFile(std::string path)
Variant ParseJSON(const std::string &text)
std::string MD5String(const std::string &text)
Variant ExtractJSONObject(const std::string &text, int remaining, jsmntok_t **t, int start, int end)
bool IsRTLScript(const std::string &script)
std::string MD5File(const std::string &filename)
Variant FileExists(std::string path)
void CopyToClipboard(const std::string &text)
std::string ShiftSwitch(int modkeys, std::string lower, std::string upper)
pInt Round(pDouble val)
Variant Tokenize(const char *line, char separator)
Variant GetDirectoryListing(std::string path)
std::string GetFontForLanguageOrScript(const std::string &code, int fontType)
Variant GetWorkingDirectory()
pInt Truncate(pDouble val)
Variant MakeDirectory(std::string path)
std::string ToLower(const std::string &text)
Variant IsDirectory(std::string path)
PEBLVideoMode GetVideoMode(std::string modeline)