| Правила | Регистрация | Пользователи | Сообщения за день |  Справка по форуму | Файлообменник |

Вернуться   Форум DWG.RU > Программное обеспечение > Программирование > функции для работы с ini-файлами

функции для работы с ini-файлами

Ответ
Поиск в этой теме
Непрочитано 28.07.2006, 15:55 #1
функции для работы с ini-файлами
ivspec
 
Регистрация: 08.12.2005
Сообщений: 17

Может кто поделиться функцями для работы с ini-файлами
Просмотров: 5261
 
Непрочитано 28.07.2006, 19:07
#2
Alan

CAD
 
Регистрация: 28.08.2003
Киев
Сообщений: 1,835
<phrase 1=


Почитай "САПР на базе..." стр.104
__________________
По теории майский жук летать не может.
Но он этого не знает. И летает...
Alan вне форума  
 
Непрочитано 28.07.2006, 19:17
#3
C1


 
Регистрация: 13.06.2005
Сообщений: 314


Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Public Function GetFromFile(Section As String, Key As String, FullName As String) As String
If Not FileExists(FullName) Then Exit Function
Dim buffer As String
buffer = Space$(2048)
GetPrivateProfileString Section, Key, "", buffer, Len(buffer) - 1, FullName
GetFromFile = Left$(buffer, InStr(buffer, Chr(0)) - 1)
End Function

Public Sub SetToFile(Section As String, Key As String, Value As String, FullName As String)
WritePrivateProfileString Section, Key, Value, FullName
End Sub
C1 вне форума  
 
Непрочитано 08.08.2006, 10:06
#4
Cleper

проектирование систем безопасности
 
Регистрация: 08.08.2006
г. Иваново
Сообщений: 30
<phrase 1=


Посмотри здесь http://autolisp.ru/dwlsp/21 это то что тебе нужно
Cleper вне форума  
 
Непрочитано 08.08.2006, 11:43
#5
ShaggyDoc

Thượng Tá Quân Đội Nhân Dân Việt Nam
 
Регистрация: 14.03.2005
44d32'44"С, 33d26'51"В
Сообщений: 13,381


Цитата:
Сообщение от Cleper
Посмотри здесь http://autolisp.ru/dwlsp/21 это то что тебе нужно
Ну, зачем уж садить на свою зашифрованную "иглу". Делется на чистом Автолиспе. Свои функции не привожу (на Лиспе лень оформлять для публикации, использую COM). Вот freeware вариант от Thomas Berger. Недостатки есть, но работает.
Код:
[Выделить все]
(defun ini_readentry (inifile section entry)
  (if (and (= 'STR (type section)) (/= "[" (substr section 1 1)))
    (setq section (strcat "[" section "]"))
  ) ;_ end of if
  (setq section (ini_readsection inifile section))
  (cadr (assoc entry section))
) ;_ end of defun

(defun ini_writeentry (inifile section entry val / ofile ini sec)
  (if (not (findfile inifile))
    (progn (setq ofile (open inifile "w")) (close ofile))
  ) ;_ end of if
  (if (and (= 'STR (type section)) (/= "[" (substr section 1 1)))
    (setq section (strcat "[" section "]"))
  ) ;_ end of if
  (if (setq ofile (findfile inifile))
    (progn
      (setq ini (ini_readini inifile))
      (cond
        ((setq sec (assoc section ini))
         (if (assoc entry (cdr sec))
           (setq sec (cons section
                           (subst (list entry val)
                                  (assoc entry (cdr sec))
                                  (cdr sec)
                           ) ;_ end of subst
                     ) ;_ end of cons
           ) ;_ end of setq
           (setq sec
                  (cons
                    section
                    (reverse (cons (list entry val) (reverse (cdr sec))))
                  ) ;_ end of cons
           ) ;_ end of setq
         ) ;_ end of if
         (setq ini (subst sec (assoc section ini) ini))
         (setq ofile (open ofile "w"))
         (if ofile
           (progn
             (mapcar
               '(lambda (x)
                  (write-line (car x) ofile)
                  (mapcar
                    '(lambda (x)
                       (write-line (strcat (car x) "=" (cadr x)) ofile)
                     ) ;_ end of lambda
                    (cdr x)
                  ) ;_ end of mapcar
                  (write-line "" ofile)
                ) ;_ end of lambda
               ini
             ) ;_ end of mapcar
             (close ofile)
           ) ;_ end of progn
         ) ;_ end of if
        )
        (t
         (setq ofile (open ofile "a"))
         (if ofile
           (progn
             (write-line section ofile)
             (write-line (strcat entry "=" val) ofile)
             (close ofile)
           ) ;_ end of progn
         ) ;_ end of if
        )

      ) ;_ end of cond
    ) ;_ end of progn
  ) ;_ end of if

) ;_ end of defun


(defun ini_readsection (inifile section / ofile line result)
  (if (and (= 'STR (type section)) (/= "[" (substr section 1 1)))
    (setq section (strcat "[" section "]"))
  ) ;_ end of if
  (if (findfile inifile)
    (cdr (assoc section (ini_readini (findfile inifile))))
    (alert (strcat inifile "\nNot found!"))
  ) ;_ end of if
) ;_ end of defun



(defun ini_readini (inifile / ofile line section result)
  (if (findfile inifile)
    (progn
      (setq ofile (open (findfile inifile) "r"))
      (if ofile
        (progn
          (while (and (setq line (read-line ofile))
                      (/= "[" (substr line 1 1))
                 ) ;_ end of and
          ) ;_ end of while
          (while (and line (= "[" (substr line 1 1)))
            (setq section (list line))
            (while (and (setq line (read-line ofile))
                        (/= "[" (substr line 1 1))
                   ) ;_ end of and
              (if (and (/= ";" (substr line 1 1)) (/= "" line))
                (setq section (cons (ini_iniline line "=") section))
              ) ;_ end of if
            ) ;_ end of while
            (setq result (cons (reverse section) result))
          ) ;_ end of while
          (close ofile)
        ) ;_ end of progn
      ) ;_ end of if
    ) ;_ end of progn
    (alert (strcat inifile "\nNot found!"))
  ) ;_ end of if
  (reverse result)
) ;_ end of defun

(defun ini_iniline (line sep / line str1 str2)
  (if (= 'STR (type line))
    (progn
      (setq str1 ""
            str2 ""
      ) ;_ end of setq
      (while (and (/= "" line) (/= sep (substr line 1 1)))
        (setq str1 (strcat str1 (substr line 1 1))
              line (substr line 2)
        ) ;_ end of setq
      ) ;_ end of while
      (if (= sep (substr line 1 1))
        (setq str2 (substr line 2))
      ) ;_ end of if
    ) ;_ end of progn
  ) ;_ end of if
  (list str1 str2)
) ;_ end of defun
Примеры:
Код:
[Выделить все]
(setq *ini_file_name* "c:\\.ru\\_ruInstall\\cad\\LocalData\\ini\\default.ini"
      *ini_section_name* "Setup")
(ini_readentry *ini_file_name* *ini_section_name* "DimScaleByPrintScale") вернула "0"

(ini_writeentry  *ini_file_name* *ini_section_name* "Новая переменная" "Значение новой переменной")
вернула NIL, но записала
(ini_readsection *ini_file_name* *ini_section_name*) вернула
(("DimScaleByPrintScale" "0")
  ("PsvPScaleByPrintScale" "1")
  ("LtScaleByPrintScale" "0")
  ("CurrentLineWeight" "0.500")
  ("BaseLineWeight" "0.500")
  ("LineWeight_0" "0.25")
  ("CurrLineWidth" "0.500")
  ("Unit" "мм")
  ("Scale" "100.000")
  ("SnapMM" "100.0")
  ("TextHeight" " 2.5")
  ("SnapM" "1.0")
  ("LengthPrecisionMM" "-1")
  ("LengthPrecisionM" "3")
  ("LineWidth" "0.5")
  ("DLineWidth" "5")
  ("WallWidth" "640")
  ("Diameter1" "500")
  ("Diameter2" "200")
  ("RoadWidth" "6")
  ("DimRoundMM" "1.0")
  ("DimRoundM" "0.1")
  ("X_axis_North" "1")
  ("Новая переменная" "Значение новой переменной")
)

(ini_readini *ini_file_name* ) вернула
(("[DWGProperties] " ("DWG_UNITS" "мм") ("DWG_SCALE" "1"))
  ("[Setup]" ("DimScaleByPrintScale" "0")
             ("PsvPScaleByPrintScale" "1")
             ("LtScaleByPrintScale" "0")
             ("CurrentLineWeight" "0.500")
             ("BaseLineWeight" "0.500")
             ("LineWeight_0" "0.25")
             ("CurrLineWidth" "0.500")
             ("Unit" "мм")
             ("Scale" "100.000")
             ("SnapMM" "100.0")
             ("TextHeight" " 2.5")
             ("SnapM" "1.0")
             ("LengthPrecisionMM" "-1")
             ("LengthPrecisionM" "3")
             ("LineWidth" "0.5")
             ("DLineWidth" "5")
             ("WallWidth" "640")
             ("Diameter1" "500")
             ("Diameter2" "200")
             ("RoadWidth" "6")
             ("DimRoundMM" "1.0")
             ("DimRoundM" "0.1")
             ("X_axis_North" "1")
             ("Новая переменная" "Значение новой переменной")
  )
  ("[Styles]"
    ("ruCAD.TxtStyleFile" "simplex.shx")
    ("ruCAD.TxtStyleWidth" "0.8")
    ("ruCAD.TxtStyleAngle" "0.0")
    ("Standard.TxtStyleFile" "simplex.shx")
    ("Standard.TxtStyleWidth" "0.8")
    ("Standard.TxtStyleAngle" "0.0")
    ("MONO.TxtStyleFile" "monotxt.shx")
    ("MONO.TxtStyleWidth" "1.0")
    ("MONO.TxtStyleAngle" "15.0")
  )
  ("[HardWare]" ("DisplayPixelSize" "0.4010"))
  ("[SYSVARS]" ("TEXTSIZE" "2.5") ("DIMTXT" "2.0"))
)
ShaggyDoc вне форума  
Ответ
Вернуться   Форум DWG.RU > Программное обеспечение > Программирование > функции для работы с ini-файлами

Размещение рекламы