Changes between Version 12 and Version 13 of DevelopersFAQ


Ignore:
Timestamp:
Aug 20, 2007, 5:09:44 AM (17 years ago)
Author:
guest
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DevelopersFAQ

    v12 v13  
    6969
    7070----
    71 
    72 == Coding Questions == #coding-questions
    73 
    74 === Is the code below OK? Did I account for the terminating nulls correctly? ===
    75 
    76 {{{
    77 //BldFullPathName returns the fullpath of a file or Nullstr
    78 CHAR *FullPathName = BldFullPathName(CHAR *pathname, CHAR
    79 *filename)
    80 {
    81 
    82 CHAR newname[CCHMAXPATH] = Nullstr;
    83 INT c = 0;
    84 
    85 c = strlen(pathname);
    86 if (c > 0) {
    87 memcpy(newname, pathname, c + 1);
    88 if (newname[c] != '\\')
    89 newname[c++] = '\\';
    90 }
    91 strcpy(newname + c, filename);
    92 return newname;
    93 }
    94 
    95 //BldQuotedFullPathName returns the quoted fullpath of a file or ""
    96 CHAR *FullPathName = BldQuotedFullPathName(CHAR *pathname,
    97 CHAR *filename)
    98 {
    99 
    100 CHAR newname[CCHMAXPATH] = '\"';
    101 INT c = 0;
    102 
    103 c = strlen(pathname);
    104 if (c > 0) {
    105 memcpy(newname + 1, pathname, c + 2);
    106 if (newname[c + 1] != '\\')
    107 newname[c + 2] = '\\';
    108 strcpy(newname + c + 3, filename);
    109 }
    110 else
    111 strcpy(newname + 1, filename)
    112 strcat(newname, '\"')
    113 return newname;
    114 }
    115 }}}
    116 
    117 Not quite. The caller must pass a pointer to the buffer. Keep in mind
    118 that local variable disappear when the function returns.
    119 
    120 
    121 {{{
    122 PSZ BldFullPathName(PSZ fullPathName, PSZ dirname, PSZ filename);
    123 }}}
    124 
    125 
    126 
    127 {{{
    128 CHAR newname[CCHMAXPATH] = Nullstr;
    129 }}}
    130 
    131 
    132 This will give you an error because NullStr is a pointer.
    133 
    134 
    135 {{{
    136 CHAR newname[CCHMAXPATH];
    137 }}}
    138 
    139 
    140 Is sufficent since you are always going to copy something. The return is
    141 
    142 
    143 {{{
    144 return fullPathName
    145 }}}
    146 
    147 
    148 with the appropriate name changes.
    149 
    150 
    151 {{{
    152 //BldQuotedFullPathName returns the quoted fullpath of a file or "" CHAR
    153 }}}
    154 
    155 
    156 The quotes need to be optional and the return buffer needs to be passed as
    157 above.
    158 
    159 
    160 {{{
    161 PSZ BldQuotedFullPathName(PSZ fullPathName, PSZ pathname, PSZ filename)
    162 }}}
    163 
    164 
    165 You also need to add the calls to needs_quotes() a stuff the quotes only
    166 if needed. This is what the existing inline code does.
    167 
    168 === What is the purpose of quoting functions? ===
    169 
    170 Its purpose is to replace repeated occurances of code of the form
    171 
    172 
    173 {{{
    174 sprintf(cl, "%s %s%s%s", dcd->info->delete,
    175 (needs_quoting(dcd->arcname)) ? "\"" : NullStr,
    176 dcd->arcname,
    177 (needs_quoting(dcd->arcname)) ? "\"" : NullStr);
    178 }}}
    179 
    180 
    181 and possibly
    182 
    183 
    184 {{{
    185 runemf2(SEPARATEKEEP | WINDOWED | MAXIMIZED,
    186 hwnd, NULL, NULL, "%s %s%s%s", ad->info->test,
    187 needs_quoting(ad->arcname) ? "\"" : NullStr,
    188 ad->arcname,
    189 needs_quoting(ad->arcname) ? "\"" : NullStr);
    190 }}}
    191 
    192 
    193 === Where is the best place to do a check for? ===
    194 
    195 Since the check always needs to be done, it's generally better to do it in
    196 the function.
    197 
    19871----
    19972
     
    22699
    227100----
     101
     102== Coding Questions == #coding-questions
     103
     104=== Is the code below OK? Did I account for the terminating nulls correctly? ===
     105
     106{{{
     107//BldFullPathName returns the fullpath of a file or Nullstr
     108CHAR *FullPathName = BldFullPathName(CHAR *pathname, CHAR
     109*filename)
     110{
     111
     112CHAR newname[CCHMAXPATH] = Nullstr;
     113INT c = 0;
     114
     115c = strlen(pathname);
     116if (c > 0) {
     117memcpy(newname, pathname, c + 1);
     118if (newname[c] != '\\')
     119newname[c++] = '\\';
     120}
     121strcpy(newname + c, filename);
     122return newname;
     123}
     124
     125//BldQuotedFullPathName returns the quoted fullpath of a file or ""
     126CHAR *FullPathName = BldQuotedFullPathName(CHAR *pathname,
     127CHAR *filename)
     128{
     129
     130CHAR newname[CCHMAXPATH] = '\"';
     131INT c = 0;
     132
     133c = strlen(pathname);
     134if (c > 0) {
     135memcpy(newname + 1, pathname, c + 2);
     136if (newname[c + 1] != '\\')
     137newname[c + 2] = '\\';
     138strcpy(newname + c + 3, filename);
     139}
     140else
     141strcpy(newname + 1, filename)
     142strcat(newname, '\"')
     143return newname;
     144}
     145}}}
     146
     147Not quite. The caller must pass a pointer to the buffer. Keep in mind
     148that local variable disappear when the function returns.
     149
     150
     151{{{
     152PSZ BldFullPathName(PSZ fullPathName, PSZ dirname, PSZ filename);
     153}}}
     154
     155
     156
     157{{{
     158CHAR newname[CCHMAXPATH] = Nullstr;
     159}}}
     160
     161
     162This will give you an error because NullStr is a pointer.
     163
     164
     165{{{
     166CHAR newname[CCHMAXPATH];
     167}}}
     168
     169
     170Is sufficent since you are always going to copy something. The return is
     171
     172
     173{{{
     174return fullPathName
     175}}}
     176
     177
     178with the appropriate name changes.
     179
     180
     181{{{
     182//BldQuotedFullPathName returns the quoted fullpath of a file or "" CHAR
     183}}}
     184
     185
     186The quotes need to be optional and the return buffer needs to be passed as
     187above.
     188
     189
     190{{{
     191PSZ BldQuotedFullPathName(PSZ fullPathName, PSZ pathname, PSZ filename)
     192}}}
     193
     194
     195You also need to add the calls to needs_quotes() a stuff the quotes only
     196if needed. This is what the existing inline code does.
     197
     198=== What is the purpose of quoting functions? ===
     199
     200Its purpose is to replace repeated occurances of code of the form
     201
     202
     203{{{
     204sprintf(cl, "%s %s%s%s", dcd->info->delete,
     205(needs_quoting(dcd->arcname)) ? "\"" : NullStr,
     206dcd->arcname,
     207(needs_quoting(dcd->arcname)) ? "\"" : NullStr);
     208}}}
     209
     210
     211and possibly
     212
     213
     214{{{
     215runemf2(SEPARATEKEEP | WINDOWED | MAXIMIZED,
     216hwnd, NULL, NULL, "%s %s%s%s", ad->info->test,
     217needs_quoting(ad->arcname) ? "\"" : NullStr,
     218ad->arcname,
     219needs_quoting(ad->arcname) ? "\"" : NullStr);
     220}}}
     221
     222
     223=== Where is the best place to do a check for? ===
     224
     225Since the check always needs to be done, it's generally better to do it in
     226the function.
     227
     228
     229----