winregistry.c

Go to the documentation of this file.
00001 
00002 /*  $Id: winregistry.c 1254 2006-04-12 20:27:14Z sethdill $    */
00003 
00004 /******************************************************************************
00005 
00006     UserLand Frontier(tm) -- High performance Web content management,
00007     object database, system-level and Internet scripting environment,
00008     including source code editing and debugging.
00009 
00010     Copyright (C) 1992-2004 UserLand Software, Inc.
00011 
00012     This program is free software; you can redistribute it and/or modify
00013     it under the terms of the GNU General Public License as published by
00014     the Free Software Foundation; either version 2 of the License, or
00015     (at your option) any later version.
00016 
00017     This program is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020     GNU General Public License for more details.
00021 
00022     You should have received a copy of the GNU General Public License
00023     along with this program; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 
00026 ******************************************************************************/
00027 
00028 #include "frontier.h"
00029 #include "standard.h"
00030 
00031 #include "error.h"
00032 #include "memory.h"
00033 #include "strings.h"
00034 #include "lang.h"
00035 #include "winregistry.h"
00036 
00037 boolean setRegKeyString (Handle regkey, bigstring subkey, bigstring itemname, bigstring bsval) {
00038     HKEY hkey;
00039     DWORD what;
00040     char key[256];
00041     char item[256];
00042     char val[256];
00043     char * itemptr;
00044 
00045     if ((subkey == NULL) || (bsval == NULL))
00046         return (false);
00047 
00048     if (stringlength (subkey) == 0)
00049         return (false);
00050 
00051     copyptocstring (subkey, key);
00052     copyptocstring (bsval, val);
00053 
00054     if (itemname != NULL) {
00055         itemptr = item;
00056         copyptocstring (itemname, item);
00057         }
00058     else
00059         itemptr = NULL;
00060 
00061     if (RegCreateKeyEx ((HKEY) regkey, key, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &what) == ERROR_SUCCESS) {
00062         
00063         RegSetValueEx (hkey, itemptr, 0, REG_SZ, val, strlen (val)+1);
00064             
00065         RegCloseKey (hkey);
00066 
00067         return (true);
00068         }
00069 
00070     return (false);
00071     } /*setRegKeyString*/
00072 
00073 boolean setRegKeyLong (Handle regkey, bigstring subkey, bigstring itemname, unsigned long val) {
00074     HKEY hkey;
00075     DWORD what;
00076     char key[256];
00077     char item[256];
00078     char * itemptr;
00079 
00080     if (subkey == NULL)
00081         return (false);
00082 
00083     if (stringlength (subkey) == 0)
00084         return (false);
00085 
00086     copyptocstring (subkey, key);
00087 
00088     if (itemname != NULL) {
00089         itemptr = item;
00090         copyptocstring (itemname, item);
00091         }
00092     else
00093         itemptr = NULL;
00094 
00095     if (RegCreateKeyEx ((HKEY) regkey, key, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &what) == ERROR_SUCCESS) {
00096         
00097         RegSetValueEx (hkey, itemptr, 0, REG_DWORD_LITTLE_ENDIAN, (unsigned char *) &val, sizeof(long));
00098             
00099         RegCloseKey (hkey);
00100 
00101         return (true);
00102         }
00103 
00104     return (false);
00105     } /*setRegKeyLong*/
00106 
00107 boolean setRegKeyBinary (Handle regkey, bigstring subkey, bigstring itemname, unsigned char * val, unsigned long len) {
00108     HKEY hkey;
00109     DWORD what;
00110     char key[256];
00111     char item[256];
00112     char * itemptr;
00113 
00114     if ((subkey == NULL) || (val == NULL))
00115         return (false);
00116 
00117     if (stringlength (subkey) == 0)
00118         return (false);
00119 
00120     copyptocstring (subkey, key);
00121 
00122     if (itemname != NULL) {
00123         itemptr = item;
00124         copyptocstring (itemname, item);
00125         }
00126     else
00127         itemptr = NULL;
00128 
00129     if (RegCreateKeyEx ((HKEY) regkey, key, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &what) == ERROR_SUCCESS) {
00130         
00131         RegSetValueEx (hkey, itemptr, 0, REG_BINARY, val, len);
00132             
00133         RegCloseKey (hkey);
00134 
00135         return (true);
00136         }
00137 
00138     return (false);
00139     } /*setRegKeyBinary*/
00140 
00141 boolean getRegKeyInfo (Handle regkey, bigstring subkey, bigstring itemname, unsigned long *regType, unsigned long * len) {
00142     HKEY hkey;
00143     char key[256];
00144     char item[256];
00145     char * itemptr;
00146     long err;
00147 
00148     if ((subkey == NULL) || (regType == NULL) || (len == NULL))
00149         return (false);
00150 
00151     if (stringlength (subkey) == 0)
00152         return (false);
00153 
00154     copyptocstring (subkey, key);
00155 
00156     if (itemname != NULL) {
00157         itemptr = item;
00158         copyptocstring (itemname, item);
00159         }
00160     else
00161         itemptr = NULL;
00162 
00163     if (RegOpenKeyEx ((HKEY) regkey, key, 0, KEY_READ, &hkey) == ERROR_SUCCESS) {
00164         
00165         *len = 0;
00166 
00167         err = RegQueryValueEx (hkey, itemptr, NULL, regType, NULL, len);
00168             
00169         RegCloseKey (hkey);
00170 
00171         if (err != ERROR_SUCCESS)
00172             return (false);
00173 
00174         return (true);
00175         }
00176 
00177     return (false);
00178     } /*getRegKeyInfo*/
00179 
00180 boolean getRegKeyString (Handle regkey, bigstring subkey, bigstring itemname, bigstring bsval) {
00181     HKEY hkey;
00182     char key[256];
00183     char item[256];
00184     char * itemptr;
00185     DWORD regtype;
00186     long err;
00187     DWORD len;
00188 
00189     if ((subkey == NULL) || (bsval == NULL))
00190         return (false);
00191 
00192     if (stringlength (subkey) == 0)
00193         return (false);
00194 
00195     copyptocstring (subkey, key);
00196 
00197     if (itemname != NULL) {
00198         itemptr = item;
00199         copyptocstring (itemname, item);
00200         }
00201     else
00202         itemptr = NULL;
00203 
00204     if (RegOpenKeyEx ((HKEY) regkey, key, 0, KEY_READ, &hkey) == ERROR_SUCCESS) {
00205         
00206         len = sizeof(bigstring) - 1;
00207 
00208         err = RegQueryValueEx (hkey, itemptr, NULL, &regtype, stringbaseaddress(bsval), &len);
00209             
00210         RegCloseKey (hkey);
00211 
00212         if (err != ERROR_SUCCESS)
00213             return (false);
00214 
00215         if (regtype != REG_SZ)
00216             return (false);
00217 
00218         setstringlength (bsval, len - 1);
00219 
00220         return (true);
00221         }
00222 
00223     return (false);
00224     } /*getRegKeyString*/
00225 
00226 boolean getRegKeyLong (Handle regkey, bigstring subkey, bigstring itemname, unsigned long * val) {
00227     HKEY hkey;
00228     char key[256];
00229     char item[256];
00230     char * itemptr;
00231     DWORD regtype;
00232     long err;
00233     DWORD len;
00234 
00235     if ((subkey == NULL) || (val == NULL))
00236         return (false);
00237 
00238     if (stringlength (subkey) == 0)
00239         return (false);
00240 
00241     copyptocstring (subkey, key);
00242 
00243     if (itemname != NULL) {
00244         itemptr = item;
00245         copyptocstring (itemname, item);
00246         }
00247     else
00248         itemptr = NULL;
00249 
00250     if (RegOpenKeyEx ((HKEY) regkey, key, 0, KEY_READ, &hkey) == ERROR_SUCCESS) {
00251         
00252         len = sizeof(long);
00253 
00254         err = RegQueryValueEx (hkey, itemptr, NULL, &regtype, (unsigned char *) val, &len);
00255             
00256         RegCloseKey (hkey);
00257 
00258         if (err != ERROR_SUCCESS)
00259             return (false);
00260 
00261         if (regtype != REG_DWORD_LITTLE_ENDIAN)
00262             return (false);
00263 
00264         if (len != sizeof(long))
00265             return (false);
00266 
00267         return (true);
00268         }
00269 
00270     return (false);
00271     } /*getRegKeyLong*/
00272 
00273 boolean getRegKeyBinary (Handle regkey, bigstring subkey, bigstring itemname, unsigned char * val, unsigned long  * len) {
00274     HKEY hkey;
00275     char key[256];
00276     char item[256];
00277     char * itemptr;
00278     DWORD regtype;
00279     long err;
00280 
00281     if ((subkey == NULL) || (val == NULL) || (len == NULL))
00282         return (false);
00283 
00284     if (stringlength (subkey) == 0)
00285         return (false);
00286 
00287     copyptocstring (subkey, key);
00288 
00289     if (itemname != NULL) {
00290         itemptr = item;
00291         copyptocstring (itemname, item);
00292         }
00293     else
00294         itemptr = NULL;
00295 
00296     if (RegOpenKeyEx ((HKEY) regkey, key, 0, KEY_READ, &hkey) == ERROR_SUCCESS) {
00297         
00298         err = RegQueryValueEx (hkey, itemptr, NULL, &regtype, val, len);
00299             
00300         RegCloseKey (hkey);
00301 
00302         if (err != ERROR_SUCCESS)
00303             return (false);
00304 
00305         if (regtype != REG_BINARY)
00306             return (false);
00307 
00308         return (true);
00309         }
00310 
00311     return (false);
00312     } /*getRegKeyBinary*/
00313 
00314 #ifdef PIKE
00315 #ifndef OPMLEDITOR
00316 #define ProgramKey BIGSTRING ("\x16" "Software\\UserLand\\Pike")
00317 #else //OPMLEDITOR
00318 #define ProgramKey BIGSTRING ("\x16" "Software\\Scripting.com\\OPML")
00319 #endif  //OPMLEDITOR
00320 #else
00321 #define ProgramKey BIGSTRING ("\x1a" "Software\\UserLand\\Frontier")
00322 #endif
00323 
00324 boolean setProfileString (bigstring itemname, bigstring bsval) {
00325     return (setRegKeyString ((Handle) HKEY_CURRENT_USER, ProgramKey, itemname, bsval)); 
00326     } /*setProfileString*/
00327 
00328 boolean setProfileLong (bigstring itemname, unsigned long val) {
00329     return (setRegKeyLong ((Handle) HKEY_CURRENT_USER, ProgramKey, itemname, val)); 
00330     } /*setProfileLong*/
00331 
00332 boolean setProfileData (bigstring itemname, unsigned char * val, unsigned long len) {
00333     return (setRegKeyBinary ((Handle) HKEY_CURRENT_USER, ProgramKey, itemname, val, len));
00334     } /*setProfileData*/
00335 
00336 boolean getProfileString (bigstring itemname, bigstring bsval) {
00337     return (getRegKeyString ((Handle) HKEY_CURRENT_USER, ProgramKey, itemname, bsval)); 
00338     } /*getProfileString*/
00339 
00340 boolean getProfileLong (bigstring itemname, unsigned long * val) {
00341     return (getRegKeyLong ((Handle) HKEY_CURRENT_USER, ProgramKey, itemname, val)); 
00342     } /*getProfileLong*/
00343 
00344 boolean getProfileData (bigstring itemname, unsigned char * val, unsigned long * len) {
00345     return (getRegKeyBinary ((Handle) HKEY_CURRENT_USER, ProgramKey, itemname, val, len));
00346     } /*getProfileData*/
00347 
00348 
00349 static boolean setRegKeyValue (bigstring bskey, bigstring bsitem, bigstring bsval) {
00350 
00351     HKEY hkey;
00352     DWORD what;
00353     char key[256];
00354     char item[256];
00355     char val[256];
00356     char * itemptr;
00357 
00358     if ((bskey == NULL) || (bsval == NULL))
00359         return (false);
00360 
00361     if (stringlength (bskey) == 0)
00362         return (false);
00363 
00364     copyptocstring (bskey, key);
00365     copyptocstring (bsval, val);
00366 
00367     if (bsitem != NULL) {
00368         itemptr = item;
00369         copyptocstring (bsitem, item);
00370         }
00371     else
00372         itemptr = NULL;
00373 
00374     if (RegCreateKeyEx (HKEY_CLASSES_ROOT, key, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &what) == ERROR_SUCCESS) {
00375         
00376         RegSetValueEx (hkey, itemptr, 0, REG_SZ, val, strlen (val)+1);
00377             
00378         RegCloseKey (hkey);
00379 
00380         return (true);
00381         }
00382 
00383     return (false);
00384     }
00385 
00386 
00387 /* This function is a single line registration of the extension, its content type, its filetype and its description
00388 and the default icon and shell open command needed.  
00389 
00390   extension - is the .extension to register - must specify and will be a key in HKEY_CLASSES_ROOT
00391   filetype - this is the name of the other key in HKEY_CLASSES_ROOT and the defualt value of the extensions key - must be specified 
00392   contenttype - this is optional - if specified will value of the value "Content Type" for the extensions key
00393   filedescription - this is optional - if not specified the function will not create the filetype key also 
00394                     iconfile, iconid, & shellopen are ignored.  If it is specified the filetype key is created with
00395                     the default value set to this filedescription.
00396   iconfile - this is optional and only used if filedescription is specified.  If specified is the filename that contians
00397                     the icon for this filetype.  a DefaultIcon subkey will be created and its value will be set to this
00398                     value followed be a comma and the value of iconid. 
00399   iconid - optional - see above
00400   shellopen - optional - if specified creates a shell - open - command subkey and sets its value to this parameter.
00401 */
00402 
00403 boolean registerFileType (bigstring extension, bigstring filetype, bigstring contenttype, bigstring filedescription, bigstring iconfile, short iconid, bigstring shellopen) {
00404     bigstring sval, skey;
00405 
00406     if ((extension == NULL) || (filetype == NULL))
00407         return (false);
00408 
00409     if (stringlength (extension) < 2)
00410         return (false);
00411 
00412     if (stringlength (filetype) == 0)
00413         return (false);
00414 
00415     /* Register with the Registry */
00416     setRegKeyValue (extension, NULL, filetype);
00417 
00418     if (contenttype != NULL) {
00419         setRegKeyValue (extension, BIGSTRING ("\x0c" "Content Type"), contenttype);
00420         }
00421 
00422     if (filedescription == NULL)
00423         return (true);              /* nothing more to do */
00424 
00425     setRegKeyValue (filetype, NULL, filedescription);
00426 
00427     if (iconfile != NULL) {
00428         copystring (iconfile, sval);
00429         pushchar (',', sval);
00430         pushint (iconid, sval);
00431 
00432         copystring (filetype, skey);
00433         pushstring (BIGSTRING ("\x0c" "\\DefaultIcon"), skey);
00434 
00435         setRegKeyValue (skey, NULL, sval);
00436         }
00437 
00438     if (shellopen != NULL) {
00439         copystring (filetype, skey);
00440         pushstring (BIGSTRING ("\x13" "\\shell\\open\\command"), skey);
00441 
00442         setRegKeyValue (skey, NULL, shellopen);
00443         }
00444 
00445     return (true);
00446     }
00447 
00448 
00449 static boolean winregpullhkeyfromtext (Handle htext, HKEY *hkey) {
00450 
00451     /*
00452     7.0.2b1 Radio PBS: get the base key from the text string. Remove
00453     it from the text string.
00454 
00455     If a string is HKCU/foo/bar/, then htext becomes foo\bar\, and *hkey
00456     becomes HKEY_CLASSES_USER.
00457     */
00458 
00459     short ixslash;
00460     bigstring bsbase;
00461     boolean fl = false;
00462     
00463     ixslash = textpatternmatch (*htext, gethandlesize (htext), BIGSTRING ("\x01\\"), true);
00464 
00465     if (ixslash == -1) /*didn't find?*/
00466         return (false);
00467 
00468     pullfromhandle (htext, 0, ixslash, stringbaseaddress (bsbase));
00469 
00470     pullfromhandle (htext, 0, 1, nil); /*remove leading /*/
00471 
00472     setstringlength (bsbase, ixslash);
00473 
00474     allupper (bsbase); /*case insensitive*/
00475 
00476     if ((equalstrings (bsbase, STR_P_HKCU_SHORT)) || (equalstrings (bsbase, STR_P_HKCU_LONG))) {
00477         *hkey = HKEY_CURRENT_USER;
00478         fl = true;
00479         }
00480 
00481     if ((equalstrings (bsbase, STR_P_HKLM_SHORT)) || (equalstrings (bsbase, STR_P_HKLM_LONG))) {
00482         *hkey = HKEY_LOCAL_MACHINE;
00483         fl = true;
00484         }
00485 
00486     if ((equalstrings (bsbase, STR_P_HKCR_SHORT)) || (equalstrings (bsbase, STR_P_HKCR_LONG))) {
00487         *hkey = HKEY_CLASSES_ROOT;
00488         fl = true;
00489         }
00490 
00491     if ((equalstrings (bsbase, STR_P_HKU_SHORT)) || (equalstrings (bsbase, STR_P_HKU_LONG))) {
00492         *hkey = HKEY_USERS;
00493         fl = true;
00494         }
00495 
00496     if ((equalstrings (bsbase, STR_P_HKCC_SHORT)) || (equalstrings (bsbase, STR_P_HCC_LONG))) {
00497         *hkey = HKEY_CURRENT_CONFIG;
00498         fl = true;
00499         }
00500     
00501     return (fl);
00502     } /*winregpullhkeyfromtext*/
00503 
00504 
00505 static boolean winregiskey (Handle h) {
00506 
00507     /*
00508     7.0.2.b1 Radio PBS: return true if the string specifies a key.
00509     Otherwise, the string specifies a value.
00510     */
00511 
00512     char ch;
00513     long lentext = gethandlesize (h);
00514 
00515     ch = (*h) [lentext - 1];
00516 
00517     if (ch == '\\')
00518         return (true);
00519 
00520     return (false);
00521     } /*winregiskey*/
00522 
00523 
00524 static boolean winregtypestringtotype (bigstring bstype, DWORD *regtype) {
00525 
00526     /*
00527     7.0.2b1 Radio PBS: Given a string identifying a type, return the numerical type.
00528     */
00529 
00530     bigstring uppertype;
00531     boolean fl = false;
00532 
00533     copystring (bstype, uppertype);
00534 
00535     allupper (uppertype); /*case insensitive*/
00536 
00537     if (equalstrings (bstype, STR_P_REG_BINARY)) {
00538 
00539         *regtype = REG_BINARY;
00540 
00541         fl = true;
00542         } /*if*/
00543 
00544     if (equalstrings (bstype, STR_P_REG_DWORD)) {
00545 
00546         *regtype = REG_DWORD;
00547 
00548         fl = true;
00549         } /*if*/
00550 
00551     if (equalstrings (bstype, STR_P_REG_DWORD_BIG_ENDIAN)) {
00552 
00553         *regtype = REG_DWORD_BIG_ENDIAN;
00554 
00555         fl = true;
00556         } /*if*/
00557 
00558     if (equalstrings (bstype, STR_P_REG_EXPAND_SZ)) {
00559 
00560         *regtype = REG_EXPAND_SZ;
00561 
00562         fl = true;
00563         } /*if*/
00564 
00565     if (equalstrings (bstype, STR_P_REG_LINK)) {
00566 
00567         *regtype = REG_LINK;
00568 
00569         fl = true;
00570         } /*if*/
00571 
00572     if (equalstrings (bstype, STR_P_REG_MULTI_SZ)) {
00573 
00574         *regtype = REG_MULTI_SZ;
00575 
00576         fl = true;
00577         } /*if*/
00578 
00579     if (equalstrings (bstype, STR_P_REG_NONE)) {
00580 
00581         *regtype = REG_NONE;
00582 
00583         fl = true;
00584         } /*if*/
00585 
00586     if (equalstrings (bstype, STR_P_REG_RESOURCE_LIST)) {
00587 
00588         *regtype = REG_RESOURCE_LIST;
00589 
00590         fl = true;
00591         } /*if*/
00592 
00593     if (equalstrings (bstype, STR_P_REG_SZ)) {
00594 
00595         *regtype = REG_SZ;
00596 
00597         fl = true;
00598         } /*if*/
00599 
00600     if (equalstrings (bstype, STR_P_REG_BINARY)) {
00601 
00602         *regtype = REG_BINARY;
00603 
00604         fl = true;
00605         } /*if*/
00606 
00607     return (fl);
00608     } /*winregtypestringtotype*/
00609 
00610 
00611 static void winregtypetostring (DWORD regtype, bigstring bstype) {
00612 
00613     /*
00614     7.0.2b1 Radio PBS: Given the numerical type of a registry entry, return the string
00615     representing that type.
00616     */
00617 
00618     switch (regtype) {
00619         
00620         case REG_BINARY:
00621 
00622             copystring (STR_P_REG_BINARY, bstype);
00623 
00624             break;
00625             
00626         case REG_DWORD:
00627 
00628             copystring (STR_P_REG_DWORD, bstype);
00629 
00630             break;
00631             
00632         case REG_DWORD_BIG_ENDIAN:
00633 
00634             copystring (STR_P_REG_DWORD_BIG_ENDIAN, bstype);
00635  
00636             break;
00637 
00638         case REG_EXPAND_SZ:
00639 
00640             copystring (STR_P_REG_EXPAND_SZ, bstype);
00641 
00642             break;
00643             
00644         case REG_LINK:
00645 
00646             copystring (STR_P_REG_LINK, bstype);
00647 
00648             break;
00649             
00650         case REG_MULTI_SZ:
00651 
00652             copystring (STR_P_REG_MULTI_SZ, bstype);
00653 
00654             break;
00655             
00656         case REG_NONE:
00657 
00658             copystring (STR_P_REG_NONE, bstype);
00659 
00660             break;
00661 
00662         case REG_RESOURCE_LIST:
00663 
00664             copystring (STR_P_REG_RESOURCE_LIST, bstype);
00665 
00666             break;
00667             
00668         case REG_SZ:
00669 
00670             copystring (STR_P_REG_SZ, bstype);
00671 
00672             break;
00673 
00674         default:
00675 
00676             copystring (STR_P_UNKNOWNTYPE, bstype);
00677 
00678             break;
00679         } /*switch*/
00680     } /*winregtypetostring*/
00681 
00682 
00683 static boolean winreggetkeytype (HKEY hkey, Handle h, DWORD *regtype) {
00684 
00685     /*
00686     7.0.2b1 Radio PBS: get the data type of a key.
00687     */
00688 
00689     bigstring subkey;
00690     unsigned long len;
00691 
00692     texthandletostring (h, subkey);
00693 
00694     return (getRegKeyInfo ((Handle) hkey, subkey, emptystring, regtype, &len));
00695     } /*winreggetkeptype*/
00696 
00697 
00698 static boolean winreggetvaluetype (HKEY hkey, Handle h, DWORD *regtype) {
00699 
00700     /*
00701     7.0.2b1 Radio PBS: get the data type of a key.
00702     */
00703 
00704     bigstring subkey, itemname;
00705     unsigned long len;
00706 
00707     texthandletostring (h, subkey);
00708 
00709     pullstringsuffix (subkey, itemname, '\\');
00710 
00711     return (getRegKeyInfo ((Handle) hkey, subkey, itemname, regtype, &len));
00712     } /*winreggetvaluetype*/
00713 
00714 
00715 static boolean winreggetvaluelength (HKEY hkey, Handle h, unsigned long *length) {
00716     
00717     /*
00718     7.0.2b1 Radio PBS: get the size of a registry value.
00719     */
00720 
00721     bigstring subkey, itemname;
00722     DWORD regtype;
00723 
00724     texthandletostring (h, subkey);
00725 
00726     pullstringsuffix (subkey, itemname, '\\');
00727 
00728     return (getRegKeyInfo ((Handle) hkey, subkey, itemname, &regtype, length));
00729     } /*winreggetvaluelength*/
00730 
00731 
00732 boolean winreggettype (Handle h, bigstring bstype) {
00733 
00734     /*
00735     7.0.2b1 Radio PBS: given a string of type "HKCU/foo/bar," get the data type.
00736     */
00737 
00738     HKEY hbasekey = HKEY_CURRENT_USER;
00739     DWORD type = 0;
00740     boolean fl = false;
00741 
00742     if (!winregpullhkeyfromtext (h, &hbasekey))
00743         return (false);
00744 
00745     if (winregiskey (h))
00746 
00747         fl = winreggetkeytype (hbasekey, h, &type);
00748 
00749     else
00750 
00751         fl = winreggetvaluetype (hbasekey, h, &type);
00752 
00753     if (fl)
00754         winregtypetostring (type, bstype);
00755 
00756     return (fl);
00757     } /*winreggettype*/
00758 
00759 
00760 static boolean winreggethandlevalue (Handle regkey, bigstring subkey, bigstring itemname, Handle hval, unsigned long  *len, DWORD *regtype) {
00761  
00762     HKEY hkey;
00763     char key[256];
00764     char item[256];
00765     char * itemptr;
00766     long err;
00767 
00768     if ((subkey == NULL) || (hval == NULL) || (len == NULL))
00769         return (false);
00770 
00771     if (stringlength (subkey) == 0)
00772         return (false);
00773 
00774     copyptocstring (subkey, key);
00775 
00776     if (itemname != NULL) {
00777         itemptr = item;
00778         copyptocstring (itemname, item);
00779         }
00780     else
00781         itemptr = NULL;
00782 
00783     if (RegOpenKeyEx ((HKEY) regkey, key, 0, KEY_READ, &hkey) == ERROR_SUCCESS) {
00784         
00785         lockhandle (hval);
00786 
00787         err = RegQueryValueEx (hkey, itemptr, NULL, regtype, *hval, len);
00788 
00789         unlockhandle (hval);
00790             
00791         RegCloseKey (hkey);
00792 
00793         if (err != ERROR_SUCCESS)
00794             return (false);
00795 
00796         return (true);
00797         }
00798 
00799     return (false);
00800     } /*winreggethandlevalue*/
00801 
00802 
00803 static boolean winreggetvalue (HKEY hkey, Handle h, Handle hvalue, DWORD *regtype, unsigned long * len) {
00804 
00805     /*
00806     7.0.2b1 Radio PBS: get a value from the registry.
00807     */
00808 
00809     bigstring subkey, itemname;
00810     boolean fl = false;
00811     unsigned long lenhandle;
00812 
00813     texthandletostring (h, subkey);
00814 
00815     pullstringsuffix (subkey, itemname, '\\');
00816     
00817     fl = winreggethandlevalue ((Handle) hkey, subkey, itemname, hvalue, len, regtype);
00818 
00819     lenhandle = gethandlesize (hvalue);
00820 
00821     if ((*hvalue) [lenhandle - 1] == '\0') /*pop trailing null terminator*/
00822         sethandlesize (hvalue, lenhandle - 1);
00823 
00824     return (fl);
00825     } /*winreggetvaluetype*/
00826 
00827 
00828 static void winregcoercetofrontiertype (Handle hvalue, DWORD regtype, tyvaluerecord* v) {
00829 
00830     /*
00831     7.0.2b1 Radio PBS: convert a registry value to a Frontier value record, coercing the type.
00832     */
00833 
00834     initvalue (v, novaluetype);
00835 
00836     switch (regtype) {
00837         
00838         case REG_DWORD:
00839         case REG_DWORD_BIG_ENDIAN:
00840 
00841             setlongvalue ((**hvalue), v);
00842 
00843             disposehandle (hvalue); /*7.0.1 (Frontier) PBS: In this case hvalue should be consumed.*/
00844 
00845             break;
00846             
00847         case REG_EXPAND_SZ:
00848         case REG_MULTI_SZ:          
00849         case REG_SZ:
00850         
00851             setheapvalue (hvalue, stringvaluetype, v);
00852 
00853             break;
00854 
00855         case REG_BINARY:
00856         case REG_LINK:
00857         case REG_NONE:
00858         case REG_RESOURCE_LIST:
00859 
00860         default:
00861             
00862             setbinaryvalue (hvalue, '\?\?\?\?', v);
00863 
00864             break;
00865         } /*switch*/
00866     } /*winregcoercetofrontiertype*/
00867 
00868 
00869 boolean winregread (Handle h, tyvaluerecord *v) {
00870 
00871     /*
00872     7.0.2b1 Radio PBS: read a registry value.
00873     */
00874 
00875     HKEY hbasekey = HKEY_CURRENT_USER;
00876     Handle hvalue;
00877     unsigned long length;
00878     DWORD regtype;
00879 
00880     if (!winregpullhkeyfromtext (h, &hbasekey))
00881         return (false);
00882 
00883     if (!winreggetvaluelength (hbasekey, h, &length))
00884         return (false);
00885 
00886     if (!newhandle (length, &hvalue))
00887         return (false);
00888     
00889     if (!winreggetvalue (hbasekey, h, hvalue, &regtype, &length))
00890         return (false);
00891 
00892     winregcoercetofrontiertype (hvalue, regtype, v);
00893 
00894     return (true);
00895     } /*winregread*/
00896 
00897 
00898 static boolean winregdeletekey (HKEY hkey, Handle h) {
00899 
00900     /*
00901     7.0.2b1 Radio PBS: delete a key.
00902     */
00903 
00904     long lentext;
00905     boolean fl = true;
00906 
00907     lentext = gethandlesize (h);
00908 
00909     sethandlesize (h, lentext + 1);
00910 
00911     (*h) [lentext] = '\0'; /*null terminate*/
00912 
00913     lockhandle (h);
00914 
00915     if (RegOpenKeyEx (hkey, *h, 0, KEY_SET_VALUE, &hkey) == ERROR_SUCCESS) {
00916 
00917         if (RegDeleteKey (hkey, *h) == ERROR_SUCCESS) {
00918 
00919             fl = true;
00920 
00921             RegFlushKey (hkey);
00922             } /*if*/
00923 
00924         RegCloseKey (hkey);
00925         } /*if*/
00926 
00927     unlockhandle (h);
00928 
00929     return (fl);
00930     } /*winregdeletekey*/
00931 
00932 
00933 static boolean winregdeletevalue (HKEY hkey, Handle h, bigstring bsitem) {
00934 
00935     /*
00936     7.0.2b1 Radio PBS: delete a value.
00937     */
00938 
00939     long lentext;
00940     char item [256];
00941     char * itemptr;
00942     boolean fl = true;
00943 
00944     lentext = gethandlesize (h);
00945 
00946     sethandlesize (h, lentext + 1);
00947 
00948     (*h) [lentext] = '\0'; /*null terminate*/
00949 
00950     copyptocstring (bsitem, item);
00951 
00952     itemptr = item;
00953 
00954     lockhandle (h);
00955 
00956     if (RegOpenKeyEx (hkey, *h, 0, KEY_SET_VALUE, &hkey) == ERROR_SUCCESS) {
00957 
00958         if (RegDeleteValue (hkey, itemptr) == ERROR_SUCCESS)
00959 
00960             fl = true;
00961 
00962         RegCloseKey (hkey);
00963         } /*if*/
00964 
00965     unlockhandle (h);
00966 
00967     return (fl);
00968     } /*winregdeletevalue*/
00969 
00970 
00971 boolean winregdelete (Handle h) {
00972 
00973     /*
00974     7.0.2b1 Radio PBS: delete from the registry.
00975     */
00976 
00977     HKEY hbasekey = HKEY_CURRENT_USER;
00978     bigstring subkey, bsitem;
00979     boolean fl = false;
00980 
00981     if (!winregpullhkeyfromtext (h, &hbasekey))
00982         return (false);
00983 
00984     if (winregiskey (h))
00985         
00986         fl = winregdeletekey (hbasekey, h);
00987 
00988     else {
00989 
00990         texthandletostring (h, subkey);
00991 
00992         if (!pullstringsuffix (subkey, bsitem, '\\'))
00993             return (false);
00994 
00995         fl = winregdeletevalue (hbasekey, h, bsitem);
00996         } /*else*/
00997 
00998     return (fl);
00999     } /*winregdelete*/
01000 
01001 
01002 static void winregfrontiertypetotype (tyvaluetype type, DWORD *regtype) {
01003 
01004     /*
01005     7.0.2b1 Radio PBS: Given a Frontier type, get the registry type.
01006     */
01007 
01008     switch (type) {
01009         
01010         case binaryvaluetype: /*binary data*/
01011         case addressvaluetype:
01012         case codevaluetype:
01013         case externalvaluetype:
01014         case passwordvaluetype:
01015         case unused2valuetype:
01016         case rectvaluetype:
01017         case rgbvaluetype:
01018         case menuvaluetype:
01019         case patternvaluetype:
01020         case pointvaluetype:
01021         case olddoublevaluetype:
01022         case wordvaluetype:
01023         case pictvaluetype:
01024         case headvaluetype:
01025         case tablevaluetype:
01026         case oldstringvaluetype:
01027         case tokenvaluetype:
01028         case objspecvaluetype:
01029 
01030             *regtype = REG_BINARY;
01031 
01032             break;
01033 
01034         case outlinevaluetype: /*strings*/
01035         case stringvaluetype:
01036         case ostypevaluetype:
01037         case filespecvaluetype:
01038         case aliasvaluetype:
01039         case enumvaluetype:
01040         case scriptvaluetype:
01041         case listvaluetype:
01042         case recordvaluetype:
01043     
01044             *regtype = REG_SZ;
01045 
01046             break;
01047 
01048         case uninitializedvaluetype: /*numbers*/
01049         case novaluetype:
01050         case charvaluetype:
01051         case intvaluetype:
01052         case longvaluetype:
01053         case booleanvaluetype:
01054         case datevaluetype:
01055         case directionvaluetype:
01056         case fixedvaluetype:
01057         case singlevaluetype:
01058         case doublevaluetype:
01059 
01060             *regtype = REG_DWORD;
01061                 
01062             break;
01063 
01064         default: /*default is binary data*/
01065 
01066             *regtype = REG_BINARY;
01067 
01068             break;
01069         } /*switch*/
01070     } /*winregfrontiertypetotype*/
01071 
01072 
01073 static void winregcoercevalue (tyvaluerecord *v, DWORD regtype) {
01074     
01075     /*
01076     7.0.2b1 Radio PBS: coerce a Frontier value to a binary, string, or long,
01077     so it can be written to the registry.
01078     */
01079 
01080     switch (regtype) {
01081     
01082         case REG_DWORD:
01083         case REG_DWORD_BIG_ENDIAN:
01084 
01085             coercetolong (v);
01086 
01087             break;
01088             
01089         case REG_EXPAND_SZ:
01090         case REG_MULTI_SZ:          
01091         case REG_SZ:
01092         
01093             coercetostring (v);
01094 
01095             break;
01096 
01097         case REG_BINARY:
01098         case REG_LINK:
01099         case REG_NONE:
01100         case REG_RESOURCE_LIST:
01101         default:
01102 
01103             coercetobinary (v);
01104         } /*switch*/
01105 
01106     } /*winregcoercevalue*/
01107 
01108 
01109 static boolean winregdowrite (HKEY regkey, Handle h, bigstring itemname, tyvaluerecord *v, DWORD regtype) {
01110 
01111     /*
01112     7.0.2b1 Radio PBS: perform the write to the registry.
01113     */
01114                               
01115     HKEY hkey;
01116     DWORD what;
01117     char item[256];
01118     char * itemptr;
01119     byte *val;
01120     boolean fl = false, flbinary = false, flstring = false;
01121     long lendata, lentext;
01122   
01123     switch (regtype) {
01124 
01125         case REG_DWORD:
01126         case REG_DWORD_BIG_ENDIAN:
01127 
01128             (long*) val = &((*v).data.longvalue);
01129 
01130             lendata = sizeof (long);
01131 
01132             break;
01133             
01134         case REG_EXPAND_SZ:
01135         case REG_MULTI_SZ:          
01136         case REG_SZ: {
01137 
01138             long lentext = gethandlesize ((*v).data.stringvalue);
01139 
01140             sethandlesize ((*v).data.stringvalue, lentext + 1);
01141 
01142             (*((*v).data.stringvalue)) [lentext] = '\0'; /*null terminate*/         
01143 
01144             val = *((*v).data.stringvalue);
01145 
01146             lendata = lentext + 1;
01147 
01148             flstring = true;
01149 
01150             break;
01151             }
01152 
01153         case REG_BINARY:
01154         case REG_LINK:
01155         case REG_NONE:
01156         case REG_RESOURCE_LIST:
01157         default: {
01158                         
01159             /*Pull first four bytes*/
01160 
01161             pullfromhandle ((*v).data.binaryvalue, 0, 4, nil);
01162 
01163             val = *((*v).data.binaryvalue);
01164 
01165             lendata = gethandlesize ((*v).data.binaryvalue);
01166 
01167             if (lendata < 0)
01168                 lendata = 0;
01169 
01170             flbinary = true;
01171 
01172             break;
01173             }
01174         } /*switch*/
01175 
01176 
01177     if (itemname != NULL) {
01178         itemptr = item;
01179         copyptocstring (itemname, item);
01180         }
01181     else
01182         itemptr = NULL;
01183 
01184     lentext = gethandlesize (h);
01185 
01186     sethandlesize (h, lentext + 1);
01187 
01188     (*h) [lentext] = '\0'; /*null terminate*/
01189 
01190     lockhandle (h);
01191 
01192     if (flbinary)
01193         lockhandle ((*v).data.binaryvalue);
01194 
01195     if (flstring)
01196         lockhandle ((*v).data.stringvalue);
01197 
01198     if (RegCreateKeyEx (regkey, *h, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &what) == ERROR_SUCCESS) {
01199         
01200         RegSetValueEx (hkey, itemptr, 0, regtype, val, lendata);
01201             
01202         RegCloseKey (hkey);
01203 
01204         fl = true;
01205         } /*if*/
01206 
01207     unlockhandle (h);
01208 
01209     if (flbinary)
01210         unlockhandle ((*v).data.binaryvalue);
01211 
01212     if (flstring)
01213         unlockhandle ((*v).data.stringvalue);
01214 
01215     return (fl);
01216     } /*winregdowrite*/
01217                               
01218 
01219 boolean winregwrite (Handle h, tyvaluerecord *val, bigstring bstype) {
01220 
01221     /*
01222     7.0.2b1 Radio PBS: write a value to the registry.
01223     */
01224     
01225     DWORD regtype;
01226     HKEY hbasekey = HKEY_CURRENT_USER;
01227     bigstring subkey, bsitem;
01228     tyvaluerecord vcopy;
01229     boolean fl = false;
01230 
01231     if (equalstrings (bstype, emptystring)) /*no type specified*/
01232     
01233         winregfrontiertypetotype ((*val).valuetype, &regtype);
01234 
01235     else
01236         
01237         winregtypestringtotype (bstype, &regtype);
01238 
01239     if (!winregpullhkeyfromtext (h, &hbasekey))
01240         return (false);
01241 
01242     if (!winregiskey (h)) {
01243 
01244         texthandletostring (h, subkey);
01245 
01246         if (!pullstringsuffix (subkey, bsitem, '\\'))
01247             return (false);
01248 
01249         disposehandle (h);
01250         
01251         newtexthandle (subkey, &h); 
01252         } /*if*/
01253 
01254     initvalue (&vcopy, novaluetype);
01255 
01256     if (!copyvaluerecord (*val, &vcopy))
01257         return (false);
01258 
01259     winregcoercevalue (&vcopy, regtype);
01260 
01261     fl = winregdowrite (hbasekey, h, bsitem, &vcopy, regtype);
01262 
01263     disposevaluerecord (vcopy, false);
01264 
01265     return (fl);
01266     } /*winregwrite*/

Generated on Wed May 31 18:20:04 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6