Nut/OS  4.10.3
API Reference
history.c
Go to the documentation of this file.
00001 /*
00002  * Copyright 2009 by egnite GmbH
00003  *
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  * 3. Neither the name of the copyright holders nor the names of
00016  *    contributors may be used to endorse or promote products derived
00017  *    from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00023  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00024  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00025  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00026  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00027  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00028  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00029  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00030  * SUCH DAMAGE.
00031  *
00032  * For additional information see http://www.ethernut.de/
00033  */
00034 
00035 /*
00036  * \file gorp/edline/history.c
00037  * \brief Simple edit history.
00038  *
00039  * \verbatim
00040  * $Id$
00041  * \endverbatim
00042  */
00043 
00044 #include <gorp/edline.h>
00045 
00046 #include <stdlib.h>
00047 #include <string.h>
00048 
00053 
00054 #ifndef EDIT_DISABLE_HISTORY
00055 
00064 EDITHISTORY *EditHistoryCreate(int siz)
00065 {
00066     EDITHISTORY *hist = malloc(sizeof(EDITHISTORY));
00067 
00068     if (hist) {
00069         hist->hist_siz = siz;
00070         hist->hist_tab = calloc(siz, sizeof(char *));
00071         if (hist->hist_tab == NULL) {
00072             free(hist);
00073             hist = NULL;
00074         }
00075     }
00076     return hist;
00077 }
00078 
00087 void EditHistoryDestroy(EDITHISTORY *hist)
00088 {
00089     int i;
00090 
00091     if (hist) {
00092         for (i = 0; i < hist->hist_siz; i++) {
00093             if (hist->hist_tab[i]) {
00094                 free(hist->hist_tab[i]);
00095             }
00096         }
00097         free(hist);
00098     }
00099 }
00100 
00109 void EditHistorySet(EDITHISTORY *hist, int idx, char *buf)
00110 {
00111     if (hist) {
00112         if (idx >= 0 && idx < hist->hist_siz) {
00113             if (hist->hist_tab[idx]) {
00114                 free(hist->hist_tab[idx]);
00115                 hist->hist_tab[idx] = NULL;
00116             }
00117             if (buf) {
00118                 hist->hist_tab[idx] = strdup(buf);
00119             }
00120         }
00121     }
00122 }
00123 
00131 int EditHistoryGet(EDITHISTORY *hist, int idx, char *buf, int siz)
00132 {
00133     int rc = 0;
00134     char *cp;
00135 
00136     if (hist) {
00137         if (idx >= 0 && idx < hist->hist_siz) {
00138             cp = hist->hist_tab[idx];
00139             if (cp) {
00140                 rc = strlen(cp) + 1;
00141                 if (buf) {
00142                     if (rc > siz) {
00143                         rc = siz;
00144                     }
00145                     memcpy(buf, cp, rc);
00146                 }
00147             }
00148         }
00149     }
00150     return rc - 1;
00151 }
00152 
00165 void EditHistoryInsert(EDITHISTORY *hist, int idx, char *buf)
00166 {
00167     int i;
00168 
00169     if (hist && buf && *buf) {
00170         i = hist->hist_siz - 1;
00171         if (idx >= 0 && idx < i) {
00172             if (hist->hist_tab[idx]) {
00173                 /* Make sure we have a new contents. */
00174                 if (strcmp(hist->hist_tab[idx], buf)) {
00175                     /* Release the entry on top. */
00176                     if (hist->hist_tab[i]) {
00177                         free(hist->hist_tab[i]);
00178                     }
00179                     /* Move all entries above the insertion point up. */
00180                     while (--i >= idx) {
00181                         hist->hist_tab[i + 1] = hist->hist_tab[i];
00182                     }
00183                 } else {
00184                     /* No change. */
00185                     return;
00186                 }
00187             }
00188             hist->hist_tab[idx] = strdup(buf);
00189         }
00190     }
00191 }
00192 
00193 #endif /* EDIT_DISABLE_HISTORY */
00194